当我向图中添加图例条目时boxplot prepared
,图例中会出现与箱线图完全相同的副本(可能很大!)。我该如何解决这个问题?作为一种解决方法,我目前正在添加area legend
以覆盖图例图像的样式。
MWE 如下:
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{pgfplots.statistics}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
boxplot/draw direction=y,
boxplot/box extend=0.36450000000000005,
xmin=0.5,
xmax=1.5,
legend style={
legend pos=outer north east,
}
]
\addplot[
fill=blue!50!white!50!white,
draw=blue!50!white,
forget plot,
boxplot prepared={
lower whisker=0.47544910179640715,
lower quartile=0.4670588235294117,
median=0.45895953757225433,
upper quartile=0.45895953757225433,
upper whisker=0.1784269662921348,
draw position=0.7975
},
] coordinates {};
\addplot[
fill=blue!50!white,
draw=blue,
boxplot prepared={
lower whisker=0.5753623188405796,
lower quartile=0.5631205673758864,
median=0.5591549295774647,
upper quartile=0.5438356164383561,
upper whisker=0.18859857482185272,
draw position=1.2025000000000001
},
] coordinates {};
\addlegendentry{Series 1}
\end{axis}
\end{tikzpicture}
\end{document}
答案1
也许不是最漂亮的,但可以创建自定义boxplot legend
样式并将其应用于axis
:
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{pgfplots.statistics}
\pgfplotsset{
/pgfplots/boxplot legend/.style={
legend image code/.code={
\draw [|-|,##1] (0,2mm) --
node[rectangle,minimum size=2.5mm,draw,fill,##1]{}
(0,7mm);
}
}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
boxplot/draw direction=y,
boxplot/box extend=0.36450000000000005,
xmin=0.5,
xmax=1.5,
legend style={
legend pos=outer north east,
},
boxplot legend % <-- added
]
\addplot[
fill=blue!50!white!50!white,
draw=blue!50!white,
forget plot,
boxplot prepared={
lower whisker=0.47544910179640715,
lower quartile=0.4670588235294117,
median=0.45895953757225433,
upper quartile=0.45895953757225433,
upper whisker=0.1784269662921348,
draw position=0.7975
},
] coordinates {};
\addplot[
fill=blue!50!white,
draw=blue,
boxplot prepared={
lower whisker=0.5753623188405796,
lower quartile=0.5631205673758864,
median=0.5591549295774647,
upper quartile=0.5438356164383561,
upper whisker=0.18859857482185272,
draw position=1.2025000000000001
},
] coordinates {};
\addlegendentry{Series 1}
\end{axis}
\end{tikzpicture}
\end{document}