我正在尝试在另一个 y 轴上添加一些数据点以及箱线图。我在 TeX 方面的经验非常有限,我不确定为什么我的轴出现在 x 轴的中间而不是末尾,以及为什么数据点没有使用与我的第一个轴相同的 x 坐标。我的两个 y 轴的标签也是叠加的。
我正在使用示例中的代码来绘制箱线图,因此我仍在努力以自己的方式去理解它。
\documentclass{minimal}
\usepackage{pgfplots}
\usepackage{filecontents}
%testdata.dat index median box_top box_bottom whisker_top whisker_bottom
\begin{filecontents}{testdata.dat}
1 0.63 0.76125 0.4025 0.81833 0.15333 0.55627
2 0.65333 0.71667 0.44333 0.82 0.03333 0.55492
3 0.5075 0.70417 0.37333 0.785 0.14667 0.50746
4 0.53333 0.75375 0.38333 0.82167 0.13833 0.53283
5 0.55667 0.64625 0.33167 0.77667 0 0.48992
6 0.53167 0.70333 0.41333 0.78833 0 0.50817
7 0.57833 0.73417 0.46167 0.81833 0.27667 0.57833
8 0.43 0.58417 0.31917 0.7 0.08333 0.43983
9 0.4375 0.61083 0.32 0.71 0 0.43458
10 0.6475 0.82167 0.54042 0.97333 0.02667 0.63617
11 0.70667 0.90542 0.61083 1.04 0.29333 0.72058
12 0.62 0.80958 0.53167 0.99667 0 0.61792
13 0.73333 0.90583 0.63667 0.98333 0.12333 0.71158
14 0.66833 0.8375 0.58833 0.93667 0.08 0.66208
15 0.54833 0.69167 0.44917 0.80333 0.10333 0.52114
16 0.545 0.72917 0.45375 0.87667 0.07333 0.53933
17 0.6175 0.72583 0.4875 0.87 0.13 0.57375
18 0.66333 0.79917 0.57 0.98333 0.21 0.6495
19 0.79167 0.88417 0.68667 0.96667 0.04333 0.71367
20 0.75833 0.89667 0.69917 1.00667 0.03667 0.73283
21 0.89 0.96917 0.82292 1.12 0.24 0.85858
22 0.79333 0.91417 0.71333 1.03 0.07667 0.76858
\end{filecontents}
\pgfplotsset{
box plot width/.initial=1em,
box plot/.style={
/pgfplots/.cd,
black,
only marks,
mark=-,
mark size=\pgfkeysvalueof{/pgfplots/box plot width},
/pgfplots/error bars/.cd,
y dir=plus,
y explicit,
},
box plot box/.style={
/pgfplots/error bars/draw error bar/.code 2 args={densely dashed%
\draw ##1 -- ++(\pgfkeysvalueof{/pgfplots/box plot width},0pt) |- ##2 -- ++(-\pgfkeysvalueof{/pgfplots/box plot width},0pt) |- ##1 -- cycle;
},
/pgfplots/table/.cd,
y index=2,
y error expr={\thisrowno{3}-\thisrowno{2}},
/pgfplots/box plot
},
box plot top whisker/.style={
/pgfplots/error bars/draw error bar/.code 2 args={%
\pgfkeysgetvalue{/pgfplots/error bars/error mark}%
{\pgfplotserrorbarsmark}%
\pgfkeysgetvalue{/pgfplots/error bars/error mark options}%
{\pgfplotserrorbarsmarkopts}%
\path ##1 -- ##2;
},
/pgfplots/table/.cd,
y index=4,
y error expr={\thisrowno{2}-\thisrowno{4}},
/pgfplots/box plot
},
box plot bottom whisker/.style={
/pgfplots/error bars/draw error bar/.code 2 args={%
\pgfkeysgetvalue{/pgfplots/error bars/error mark}%
{\pgfplotserrorbarsmark}%
\pgfkeysgetvalue{/pgfplots/error bars/error mark options}%
{\pgfplotserrorbarsmarkopts}%
\path ##1 -- ##2;
},
/pgfplots/table/.cd,
y index=5,
y error expr={\thisrowno{3}-\thisrowno{5}},
/pgfplots/box plot
},
box plot median/.style={densely dotted,
/pgfplots/box plot
}
}
\begin{document}
\begin{tikzpicture}
\begin{axis} [
ymin=0,
xtick={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22},
scale only axis,
axis y line*=left,
xlabel=Campagnes,
ylabel=CCRL (mg/L),
box plot width=0.5em,
x=0.5cm,
enlarge x limits=0.05,
y tick label style={
/pgf/number format/.cd,
fixed,
fixed zerofill,
precision=2}
]
\addplot [box plot median] table {testdata.dat};
\addplot [box plot box] table {testdata.dat};
\addplot [box plot top whisker] table {testdata.dat};
\addplot [box plot bottom whisker] table {testdata.dat};
\end{axis}
\begin{axis} [
scale only axis,
axis y line*=right,
axis x line=none,
xtick={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22},
ylabel={Temp\'erature}]
\addplot [only marks,mark=square] coordinates{(1,6.7) (2,9.1) (3,10.7) (4,13.6) (5,14) (6,13.6) (7,15.4) (8,16.9) (9,18.3) (10,18.1) (11,19.1) (12,19.7) (13,19.7) (14,20.6) (15,21) (16,20) (17,20.1) (18,20.4) (19,19.3) (20,18) (21,17.3) (22,15.6)};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
您需要确保两个轴的宽度相同。在这种情况下,这意味着您还需要将x=0.5cm, enlarge x limits=0.05
第一个轴中使用的放入第二个轴的选项中。要获得正确的标签,请设置compat=1.8
或更新:
\documentclass{minimal}
\usepackage{pgfplots}
\usepackage{filecontents}
\pgfplotsset{compat=1.11}
%testdata.dat index median box_top box_bottom whisker_top whisker_bottom
\begin{filecontents}{testdata.dat}
1 0.63 0.76125 0.4025 0.81833 0.15333 0.55627
2 0.65333 0.71667 0.44333 0.82 0.03333 0.55492
3 0.5075 0.70417 0.37333 0.785 0.14667 0.50746
4 0.53333 0.75375 0.38333 0.82167 0.13833 0.53283
5 0.55667 0.64625 0.33167 0.77667 0 0.48992
6 0.53167 0.70333 0.41333 0.78833 0 0.50817
7 0.57833 0.73417 0.46167 0.81833 0.27667 0.57833
8 0.43 0.58417 0.31917 0.7 0.08333 0.43983
9 0.4375 0.61083 0.32 0.71 0 0.43458
10 0.6475 0.82167 0.54042 0.97333 0.02667 0.63617
11 0.70667 0.90542 0.61083 1.04 0.29333 0.72058
12 0.62 0.80958 0.53167 0.99667 0 0.61792
13 0.73333 0.90583 0.63667 0.98333 0.12333 0.71158
14 0.66833 0.8375 0.58833 0.93667 0.08 0.66208
15 0.54833 0.69167 0.44917 0.80333 0.10333 0.52114
16 0.545 0.72917 0.45375 0.87667 0.07333 0.53933
17 0.6175 0.72583 0.4875 0.87 0.13 0.57375
18 0.66333 0.79917 0.57 0.98333 0.21 0.6495
19 0.79167 0.88417 0.68667 0.96667 0.04333 0.71367
20 0.75833 0.89667 0.69917 1.00667 0.03667 0.73283
21 0.89 0.96917 0.82292 1.12 0.24 0.85858
22 0.79333 0.91417 0.71333 1.03 0.07667 0.76858
\end{filecontents}
\pgfplotsset{
box plot width/.initial=1em,
box plot/.style={
/pgfplots/.cd,
black,
only marks,
mark=-,
mark size=\pgfkeysvalueof{/pgfplots/box plot width},
/pgfplots/error bars/.cd,
y dir=plus,
y explicit,
},
box plot box/.style={
/pgfplots/error bars/draw error bar/.code 2 args={densely dashed%
\draw ##1 -- ++(\pgfkeysvalueof{/pgfplots/box plot width},0pt) |- ##2 -- ++(-\pgfkeysvalueof{/pgfplots/box plot width},0pt) |- ##1 -- cycle;
},
/pgfplots/table/.cd,
y index=2,
y error expr={\thisrowno{3}-\thisrowno{2}},
/pgfplots/box plot
},
box plot top whisker/.style={
/pgfplots/error bars/draw error bar/.code 2 args={%
\pgfkeysgetvalue{/pgfplots/error bars/error mark}%
{\pgfplotserrorbarsmark}%
\pgfkeysgetvalue{/pgfplots/error bars/error mark options}%
{\pgfplotserrorbarsmarkopts}%
\path ##1 -- ##2;
},
/pgfplots/table/.cd,
y index=4,
y error expr={\thisrowno{2}-\thisrowno{4}},
/pgfplots/box plot
},
box plot bottom whisker/.style={
/pgfplots/error bars/draw error bar/.code 2 args={%
\pgfkeysgetvalue{/pgfplots/error bars/error mark}%
{\pgfplotserrorbarsmark}%
\pgfkeysgetvalue{/pgfplots/error bars/error mark options}%
{\pgfplotserrorbarsmarkopts}%
\path ##1 -- ##2;
},
/pgfplots/table/.cd,
y index=5,
y error expr={\thisrowno{3}-\thisrowno{5}},
/pgfplots/box plot
},
box plot median/.style={densely dotted,
/pgfplots/box plot
}
}
\begin{document}
\begin{tikzpicture}
\begin{axis} [
ymin=0,
xtick={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22},
scale only axis,
axis y line*=left,
xlabel=Campagnes,
ylabel=CCRL (mg/L),
box plot width=0.5em,
x=0.5cm,
enlarge x limits=0.05,
y tick label style={
/pgf/number format/.cd,
fixed,
fixed zerofill,
precision=2}
]
\addplot [box plot median] table {testdata.dat};
\addplot [box plot box] table {testdata.dat};
\addplot [box plot top whisker] table {testdata.dat};
\addplot [box plot bottom whisker] table {testdata.dat};
\end{axis}
\begin{axis} [
scale only axis,
axis y line*=right,
axis x line=none,
x=0.5cm,
enlarge x limits=0.05,
xtick={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22},
ylabel={Temp\'erature}]
\addplot [only marks,mark=square*, red] coordinates{(1,6.7) (2,9.1) (3,10.7) (4,13.6) (5,14) (6,13.6) (7,15.4) (8,16.9) (9,18.3) (10,18.1) (11,19.1) (12,19.7) (13,19.7) (14,20.6) (15,21) (16,20) (17,20.1) (18,20.4) (19,19.3) (20,18) (21,17.3) (22,15.6)};
\end{axis}
\end{tikzpicture}
\end{document}