![tikzpicture 中的箱线图 - 每个 y 轴刻度两个图](https://linux22.com/image/334817/tikzpicture%20%E4%B8%AD%E7%9A%84%E7%AE%B1%E7%BA%BF%E5%9B%BE%20-%20%E6%AF%8F%E4%B8%AA%20y%20%E8%BD%B4%E5%88%BB%E5%BA%A6%E4%B8%A4%E4%B8%AA%E5%9B%BE.png)
我需要一个箱线图,每个 ytick 上有两个框。有什么想法吗?
\begin{tikzpicture}
\begin{axis} [
title=Scenario 1,
height=6.0cm, width=8.0cm,
xmin=6, xmax=20, xtick={6,8,...,20},
ytick={1,2,3},
yticklabels={Mon, Tue, Wed},
legend style={at={(0.5,-0.2)}, anchor=north, legend columns=-1},
]
\addplot [
boxplot prepared={
upper quartile=15,
lower quartile=8,
upper whisker=19,
lower whisker=7
},
] coordinates {};
\addplot [
boxplot prepared={
upper quartile=17,
lower quartile=10,
upper whisker=19,
lower whisker=7
},
] coordinates {};
\end{axis}
\end{tikzpicture}
答案1
/pgfplots/boxplot/box extend=0.3
也许是这样的,虽然需要一些手动调整。首先通过增加选项来降低框的高度axis
。默认值为 0.8,请根据您的喜好进行调整。
然后,对于每个图,将其设置draw position
为略高于或低于刻度值的某个值。例如,draw position=1.2
如0.8
本例所示。
\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\begin{document}
\begin{tikzpicture}
\begin{axis} [
title=Scenario 1,
height=6.0cm, width=8.0cm,
xmin=6, xmax=20, xtick={6,8,...,20},
ytick={1,2,3},
yticklabels={Mon, Tue, Wed},
legend style={at={(0.5,-0.2)}, anchor=north, legend columns=-1},
ymin=0,ymax=4,
/pgfplots/boxplot/box extend=0.3
]
\addplot+ [
boxplot prepared={
upper quartile=15,
lower quartile=8,
upper whisker=19,
lower whisker=7,
draw position=1.2
},
] coordinates {};
\addplot+ [
boxplot prepared={
upper quartile=17,
lower quartile=10,
upper whisker=19,
lower whisker=7,
draw position=0.8
},
] coordinates {};
\end{axis}
\end{tikzpicture}
\end{document}