![箱线图的奇怪结果](https://linux22.com/image/368221/%E7%AE%B1%E7%BA%BF%E5%9B%BE%E7%9A%84%E5%A5%87%E6%80%AA%E7%BB%93%E6%9E%9C.png)
我正在绘制箱线图。代码如下。有六个垂直框项。除了数字外,每个框的编码相同(实际上是由程序生成的)。所以我无法理解以下结果:
框 2 和框 6 的实际框轮廓为红色而非黑色。框 6 的中线和须线也是虚线而非实线。
这是什么原因造成的?相同的代码怎么会产生不同的结果?
\begin{figure}[H]
\centering
\begin{tikzpicture}
\begin{axis}[width=6cm,height=5cm,
xticklabel style={font=\scriptsize},
yticklabel style={font=\scriptsize},xtick pos=left,
xtick align=outside,ytick pos=bottom,ytick align=outside,
xlabel={Till number},xlabel style={font=\scriptsize},
ylabel={Walk-time},ylabel style={font=\scriptsize},
legend style={font=\scriptsize,at={(rel axis cs:0.50,0.02)},anchor=south},
xtick={1,2,3,4,5,6},
boxplot/draw direction=y,
boxplot/every box/.style={fill=gray!70},
boxplot/every whisker/.style={black,very thick},
boxplot/every median/.style={black,very thick},
];
\addplot+[boxplot prepared={lower whisker=1.32,
lower quartile=2.04,median=2.44,
upper quartile=2.8,upper whisker=5.04}
] coordinates{};
\addplot+[boxplot prepared={lower whisker=2.28,
lower quartile=3.08,median=3.72,
upper quartile=4.24,upper whisker=5.72}
] coordinates{};
\addplot+[boxplot prepared={lower whisker=3.72,
lower quartile=4.68,median=5.081,
upper quartile=5.8,upper whisker=7.04}
] coordinates{};
\addplot+[boxplot prepared={lower whisker=5.08,
lower quartile=5.8,median=6.451,
upper quartile=7.28,upper whisker=9.428}
] coordinates{};
\addplot+[boxplot prepared={lower whisker=6.146,
lower quartile=7.082,median=7.813,
upper quartile=8.648,upper whisker=10.776}
] coordinates{};
\addplot+[boxplot prepared={lower whisker=6.146,
lower quartile=7.082,median=7.813,
upper quartile=8.648,upper whisker=10.776}
] coordinates{};
\end{axis}
\end{tikzpicture}
\end{figure}
答案1
有两件事会起作用。首先,pgfplots
有 s 的概念cycle list
,它本质上是用于连续绘图的样式列表,除非明确定义了样式。例如,下面的代码只是在不同的 y 值处制作了六条直线,产生了以下效果:
\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\begin{document}
\begin{tikzpicture}
\begin{axis}[no markers,samples=2,height=3cm,width=4cm]
\pgfplotsinvokeforeach{1,...,6}{
\addplot {#1};
}
\end{axis}
\end{tikzpicture}
\end{document}
如您所见,第一条线是蓝色,第二条线是红色,等等。第六条线是虚线。
第二件事是你使用\addplot +[..]
而不是\addplot [..]
。+
表示括号中的样式是附加到活动循环列表定义的样式。因此,箱线图的样式设置定义的线条样式是循环列表定义的线条样式的补充。
有两种方法可以解决这个问题。要么删除所有符号+
,这意味着pgfplots
将不使用循环列表,而是恢复为 TikZ,即一条实心黑线,加上您定义的任何样式。
或者您可以修改样式设置以指定线条应为黑色而不是虚线,即draw=black, solid
:
boxplot/every box/.style={fill=gray!70,draw=black,solid},
boxplot/every whisker/.style={black,very thick,solid},
boxplot/every median/.style={black,very thick,solid},
两种选择都会产生这样的结果:
\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\pgfplotsset{compat=1.15} % it's recommended to have a compat-setting
\begin{document}
\begin{tikzpicture}
\begin{axis}[width=6cm,height=5cm,
xticklabel style={font=\scriptsize},
yticklabel style={font=\scriptsize},xtick pos=left,
xtick align=outside,ytick pos=bottom,ytick align=outside,
xlabel={Till number},xlabel style={font=\scriptsize},
ylabel={Walk-time},ylabel style={font=\scriptsize},
legend style={font=\scriptsize,at={(rel axis cs:0.50,0.02)},anchor=south},
xtick={1,2,3,4,5,6},
boxplot/draw direction=y,
boxplot/every box/.style={fill=gray!70,draw=black,solid},
boxplot/every whisker/.style={black,very thick,solid},
boxplot/every median/.style={black,very thick,solid},
];
\addplot+[boxplot prepared={lower whisker=1.32,
lower quartile=2.04,median=2.44,
upper quartile=2.8,upper whisker=5.04}
] coordinates{};
\addplot+[boxplot prepared={lower whisker=2.28,
lower quartile=3.08,median=3.72,
upper quartile=4.24,upper whisker=5.72}
] coordinates{};
\addplot+[boxplot prepared={lower whisker=3.72,
lower quartile=4.68,median=5.081,
upper quartile=5.8,upper whisker=7.04}
] coordinates{};
\addplot+[boxplot prepared={lower whisker=5.08,
lower quartile=5.8,median=6.451,
upper quartile=7.28,upper whisker=9.428}
] coordinates{};
\addplot+[boxplot prepared={lower whisker=6.146,
lower quartile=7.082,median=7.813,
upper quartile=8.648,upper whisker=10.776}
] coordinates{};
\addplot+[boxplot prepared={lower whisker=6.146,
lower quartile=7.082,median=7.813,
upper quartile=8.648,upper whisker=10.776}
] coordinates{};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[width=6cm,height=5cm,
xticklabel style={font=\scriptsize},
yticklabel style={font=\scriptsize},xtick pos=left,
xtick align=outside,ytick pos=bottom,ytick align=outside,
xlabel={Till number},xlabel style={font=\scriptsize},
ylabel={Walk-time},ylabel style={font=\scriptsize},
legend style={font=\scriptsize,at={(rel axis cs:0.50,0.02)},anchor=south},
xtick={1,2,3,4,5,6},
boxplot/draw direction=y,
boxplot/every box/.style={fill=gray!70},
boxplot/every whisker/.style={black,very thick},
boxplot/every median/.style={black,very thick},
];
\addplot[boxplot prepared={lower whisker=1.32,
lower quartile=2.04,median=2.44,
upper quartile=2.8,upper whisker=5.04}
] coordinates{};
\addplot[boxplot prepared={lower whisker=2.28,
lower quartile=3.08,median=3.72,
upper quartile=4.24,upper whisker=5.72}
] coordinates{};
\addplot[boxplot prepared={lower whisker=3.72,
lower quartile=4.68,median=5.081,
upper quartile=5.8,upper whisker=7.04}
] coordinates{};
\addplot[boxplot prepared={lower whisker=5.08,
lower quartile=5.8,median=6.451,
upper quartile=7.28,upper whisker=9.428}
] coordinates{};
\addplot[boxplot prepared={lower whisker=6.146,
lower quartile=7.082,median=7.813,
upper quartile=8.648,upper whisker=10.776}
] coordinates{};
\addplot[boxplot prepared={lower whisker=6.146,
lower quartile=7.082,median=7.813,
upper quartile=8.648,upper whisker=10.776}
] coordinates{};
\end{axis}
\end{tikzpicture}
\end{document}