从 pgfplot 中的图例中删除不需要的条目

从 pgfplot 中的图例中删除不需要的条目

我试图将箱线图与实际点结合起来以显示分布。此外,用于点的标记取决于它所代表的治疗是积极的(标记为 x)还是中性的(标记为三角形)。

这是我的带有模拟数据的最小工作代码:

\documentclass[margin=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepgfplotslibrary{statistics}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
every y tick label/.append style={font=\footnotesize},
every x tick label/.append style={font=\footnotesize},
xmode=log,
yticklabel style={align=center},
ytick={1,2},
yticklabels={Diabetes, Cancer},
boxplot/box extend=0.48,
xlabel={CFU/day},
legend style={at={(0.5,-0.17)},anchor=north,legend cell align=left, draw=none},
%
scatter/classes={%
    a={mark=triangle,draw=black},
    b={mark=x,draw=blue}}]
%
%
\addplot+ [% AAD:
boxplot prepared={
lower whisker= 430000, lower quartile= 1126500015.5,
median= 10000000000,
upper quartile= 34000000000, upper whisker= 60000000000},
black, solid, thick, no marks] coordinates {};
%
\addplot[scatter,only marks,%
    scatter src=explicit symbolic]%
table[y index=0, x index=1,meta=label] {
y x label
1   14500000000 a
1   51000000000 a
1   17000000000 a
1   210000000   a
1   4500000000  a
1   430000  a
1   400000000   a
1   4000000000  a
1   60000000000 b
1   50000000000 b
1   50000000000 b
1   1450000000  b
1   803000031   b
1   10000000000 b
1   18000000000 b
};
%
\addplot+ [% Cancer:
boxplot prepared={
lower whisker= 300000000, lower quartile= 8975000000,
median= 17650000000,
upper quartile= 26325000000, upper whisker= 35000000000},
black, solid, no marks] coordinates {};
%
\addplot[scatter,only marks,%
    scatter src=explicit symbolic]%
table[y index=0, x index=1,meta=label] {
y x label
2   300000000   b
2   35000000000 b
2   15000000000 a
};
\legend{Positive, Neutral}
\end{axis}
\end{tikzpicture}
\end{document}

输出结果如下:

箱线图问题

如您所见,图例很怪异。我希望图例仅显示划分x和的点triangle。我希望它忽略其中的箱线图。

答案1

我猜你正在寻找这样的东西:

在此处输入图片描述

(我擅自更改了标记的颜色和图例的位置,但您可以根据自己的意愿轻松更改)

为了得到这个结果,你需要改变\addplot宏的顺序,前两个是scatter plot,后两个是boxplot。它们之间是命令\legend

\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepgfplotslibrary{statistics}

\begin{document}

\begin{tikzpicture}
\begin{semilogxaxis}[
every tick label/.append style={font=\footnotesize},
ytick={1,2},
yticklabels={Diabetes, Cancer},
xlabel={CFU/day},
legend pos=outer north east,
%
scatter/classes={%
    a={mark=triangle,draw=red},
    b={mark=x,draw=blue}}]
%
\addplot[scatter, only marks,%
         scatter src=explicit symbolic]%
table[y index=0, x index=1,meta=label] {
y   x           label
1   14500000000 a
1   51000000000 a
1   17000000000 a
1   210000000   a
1   4500000000  a
1   430000      a
1   400000000   a
1   4000000000  a
1   60000000000 b
1   50000000000 b
1   50000000000 b
1   1450000000  b
1   803000031   b
1   10000000000 b
1   18000000000 b
};
%
\addplot[scatter, only marks,%
         scatter src=explicit symbolic]%
table[y index=0, x index=1, meta=label] {
y   x           label
2   300000000   b
2   35000000000 b
2   15000000000 a
};
\legend{Positive, Neutral}
%
\addplot[% AAD:
boxplot prepared={
lower whisker= 430000, lower quartile= 1126500015.5,
median= 10000000000,
upper quartile= 34000000000, upper whisker= 60000000000},
black, no marks] coordinates {};

\addplot[% Cancer:
boxplot prepared={
lower whisker = 300000000,   lower quartile= 8975000000, median= 17650000000,
upper quartile= 26325000000, upper whisker = 35000000000},
black, no marks] coordinates {};
\end{semilogxaxis}
\end{tikzpicture}
\end{document}

相关内容