我在查找如何使用forget plot
时遇到了问题tikz
。我只需要 2 个像这样的图例条目:
但会得到图中每个对象的长列表。这帖子解决了这个问题,但似乎与我的代码方式不同。
新代码:
\documentclass{amsart}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=newest}
\pgfplotsset{plot coordinates/math parser=false}
\usetikzlibrary{plotmarks}
\newenvironment{customlegend}[1][]{%
\begingroup
% inits/clears the lists (which might be populated from previous
% axes):
\csname pgfplots@init@cleared@structures\endcsname
\pgfplotsset{#1}%
}{%
% draws the legend:
\csname pgfplots@createlegend\endcsname
\endgroup
}%
% makes \addlegendimage available (typically only available within an
% axis environment):
\def\addlegendimage{\csname pgfplots@addlegendimage\endcsname}
%%--------------------------------
% definition to insert numbers
\pgfkeys{/pgfplots/number in legend/.style={%
/pgfplots/legend image code/.code={%
\node at (0.125,-0.0225){#1}; % <= changed x value
},%
},
}
\pgfplotsset{
every legend to name picture/.style={west}
}
\begin{document}
\definecolor{mycolor1}{rgb}{0.6,0.8,1}
\definecolor{mycolor2}{rgb}{0,0.2,0.6}
\begin{tikzpicture}
\begin{axis}[%
width=4.52083333333333in,
height=3.565625in,
scale only axis,
xmin=0.5, xmax=1.5,
xtick={1},
xticklabels={Direct Care,Housekeeping,Mealtimes,Medication Round,Miscellaneous,Personal Care},
ymin=-2, ymax=5,
ylabel={Normalised cfu (Y) values},
legend style={draw=black,fill=white,legend cell align=left}
]
\addplot [
color=black,
dotted
]
table{
0.825 5
0.825 2
};
;
\addplot [
color=black,
solid
]
table{
0.755 5
0.895 5
};
;
\addplot [
color=black,
solid
]
table{
0.755 2
0.895 2
};
;
\draw[solid, fill=mycolor1, draw=black] (axis cs:0.685, 3) rectangle (axis cs:0.965, 4);
;
\addplot [
color=black,
solid,
line width=1.0pt
]
table{
0.685 3
0.965 3
};
;
\addplot [
color=blue,
only marks,
mark=*,
mark options={solid,fill=mycolor1,draw=black}
]
table{
0.825 3.56
};
;
\addplot [
color=black,
dotted
]
table{
1.175 5
1.175 -2
};
;
\addplot [
color=black,
solid
]
table{
1.105 5
1.245 5
};
;
\addplot [
color=black,
solid
]
table{
1.105 -2
1.245 -2
};
\draw[solid, fill=mycolor2, draw=black] (axis cs:1.035, 0) rectangle (axis cs:1.315, 3);
\addlegendentry{6 ACH};
\addplot [
color=black,
solid,
line width=1.0pt
]
table{
1.035 2
1.315 2
};
\addplot [
color=blue,
only marks,
mark=*,
mark options={solid,fill=mycolor2,draw=black}
]
table{
1.175 1.59
};
\end{axis}
\end{tikzpicture}%
\begin{customlegend}[legend cell align=left, %<= to align cells
legend entries={ % <= in the following there are the entries
4 ACH,
6 ACH
},
legend style={at={(15,8)},font=\footnotesize}] % <= to define position and font legend
% the following are the "images" and numbers in the legend
\addlegendimage{area legend,black,fill=mycolor1}
\addlegendimage{area legend,black,fill=mycolor2}
\end{customlegend}
\end{document}
答案1
您的示例似乎包含许多构成箱线图的自定义绘图指令 - 并且实际的箱线图(似乎与图例相关)是一个\draw
语句。
在这种情况下,似乎应该\addlegendimage
结合使用以\addlegendentry
实际绘制图例 - 并forget plot
提供每一个 \addplot
命令:
\documentclass{amsart}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=newest}
\pgfplotsset{plot coordinates/math parser=false}
\usetikzlibrary{plotmarks}
\begin{document}
\definecolor{mycolor1}{rgb}{0.6,0.8,1}
\definecolor{mycolor2}{rgb}{0,0.2,0.6}
\begin{tikzpicture}
\begin{axis}[%
width=4.52083333333333in,
height=3.565625in,
scale only axis,
xmin=0.5, xmax=1.5,
xtick={1},
xticklabels={Direct Care,Housekeeping,Mealtimes,Medication Round,Miscellaneous,Personal Care},
ymin=-2, ymax=5,
ylabel={Normalised cfu (Y) values},
legend style={draw=black,fill=white,legend cell align=left}
]
\addplot [
forget plot,
color=black,
dotted
]
table{
0.825 5
0.825 2
};
;
\addplot [
forget plot,
color=black,
solid
]
table{
0.755 5
0.895 5
};
;
\addplot [
forget plot,
color=black,
solid
]
table{
0.755 2
0.895 2
};
;
\draw[solid, fill=mycolor1, draw=black] (axis cs:0.685, 3) rectangle (axis cs:0.965, 4);
\addlegendimage{area legend,fill=mycolor1,draw=black}
\addlegendentry{4 ACH};
;
\addplot [
forget plot,
color=black,
solid,
line width=1.0pt
]
table{
0.685 3
0.965 3
};
;
\addplot [
forget plot,
color=blue,
only marks,
mark=*,
mark options={solid,fill=mycolor1,draw=black}
]
table{
0.825 3.56
};
;
\addplot [
forget plot,
color=black,
dotted
]
table{
1.175 5
1.175 -2
};
;
\addplot [
forget plot,
color=black,
solid
]
table{
1.105 5
1.245 5
};
;
\addplot [
forget plot,
color=black,
solid
]
table{
1.105 -2
1.245 -2
};
\draw[solid, fill=mycolor2, draw=black] (axis cs:1.035, 0) rectangle (axis cs:1.315, 3);
\addlegendimage{area legend,fill=mycolor2,draw=black}
\addlegendentry{6 ACH};
\addplot [
forget plot,
color=black,
solid,
line width=1.0pt
]
table{
1.035 2
1.315 2
};
\addplot [
forget plot,
color=blue,
only marks,
mark=*,
mark options={solid,fill=mycolor2,draw=black}
]
table{
1.175 1.59
};
\end{axis}
\end{tikzpicture}%
\end{document}