我尝试过不同的方法来为该图表添加颜色图例,但都失败了或没有对齐。您能提供建议吗?
所以\legend{}
没有提供任何东西,也\addlegendry{}
没有指控。我只想要一个简单的图例,将每种颜色命名为 A、B、C、D、E 类型。
另外,可以在区域的箱线图之间添加额外的空间吗?
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\pgfplotsset{compat=1.8}
% borrowed from <https://tex.stackexchange.com/a/145967/95441>
\pgfmathdeclarefunction{fpumod}{2}{%
\pgfmathfloatdivide{#1}{#2}%
\pgfmathfloatint{\pgfmathresult}%
\pgfmathfloatmultiply{\pgfmathresult}{#2}%
\pgfmathfloatsubtract{#1}{\pgfmathresult}%
% replaced `0' by `5' to make it work for this problem
\pgfmathfloatifapproxequalrel{\pgfmathresult}{#2}{\def\pgfmathresult{5}}{}%
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
legend entries = {A, B,C,D,E},
legend to name={legend},
% name=border,
boxplot/draw direction=y,
ylabel={time (s)},
height=8cm,
boxplot={
% in a formular:
draw position={
1/10+ floor(\plotnumofactualtype/5)
+ 1/5*fpumod(\plotnumofactualtype,5)
},
% that means the box extend must be at most 0.2 :
box extend=0.15,
},
% ... it also means that 1 unit in x controls the width:
x=3cm,
% ... and it means that we should describe intervals:
xtick={0,1,2,...,50},
% x tick label as interval,
xticklabels={%
{Region 1},%
{Region 2},%
{Region 3},%
{Region 4},%
},
x tick label style={
text width=2.5cm,
align=right,
xshift=6ex
},
cycle list={{green},{cyan},{blue},{orange},{red}},
legend pos=north west
]
\addplot table [row sep=\\,y index=0] {
data\\
6\\
51\\
71\\
50\\
125\\
};\legend{A}
\addplot table [row sep=\\,y index=0] {
data\\
8\\
69\\
90\\
70\\
153\\
};
\addplot table [row sep=\\,y index=0] {
data\\
75\\
41\\
30\\
88\\
29\\
};
\addplot table [row sep=\\,y index=0] {
data\\
56\\
80\\
356\\
20\\
36\\
};
\addplot table [row sep=\\,y index=0] {
data\\
68\\
34\\
70\\
32\\
37\\
};
% block 2
\addplot table [row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot table [row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot table [row sep=\\,y index=0] {
data\\
759\\
419\\
309\\
883\\
299\\
};
\addplot table [row sep=\\,y index=0] {
data\\
516\\
480\\
1356\\
200\\
736\\
};
\addplot table [row sep=\\,y index=0] {
data\\
684\\
340\\
700\\
325\\
377\\
};
% block 3
\addplot table [row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot table [row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot table [row sep=\\,y index=0] {
data\\
759\\
419\\
309\\
883\\
299\\
};
\addplot table [row sep=\\,y index=0] {
data\\
516\\
480\\
1356\\
200\\
736\\
};
\addplot table [row sep=\\,y index=0] {
data\\
684\\
340\\
700\\
325\\
377\\
};
% block 4
\addplot table [row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot table [row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot table [row sep=\\,y index=0] {
data\\
759\\
419\\
309\\
883\\
299\\
};
\addplot table [row sep=\\,y index=0] {
data\\
516\\
480\\
1356\\
200\\
736\\
};
\addplot table [row sep=\\,y index=0] {
data\\
684\\
340\\
700\\
325\\
377\\
};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
欢迎!我不确定我是否走在正确的轨道上。据我所知,纯箱线图没有真正的预定义图例,但我可能是错的。(请注意,如果您切换到较新的版本,例如compat=1.16
,输出会发生很大变化,因为它还会显示样本。)无论如何,这legend image code
为这种图例增加了可能性。
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\pgfplotsset{compat=1.8}
% borrowed from <https://tex.stackexchange.com/a/145967/95441>
\pgfmathdeclarefunction{fpumod}{2}{%
\pgfmathfloatdivide{#1}{#2}%
\pgfmathfloatint{\pgfmathresult}%
\pgfmathfloatmultiply{\pgfmathresult}{#2}%
\pgfmathfloatsubtract{#1}{\pgfmathresult}%
% replaced `0' by `5' to make it work for this problem
\pgfmathfloatifapproxequalrel{\pgfmathresult}{#2}{\def\pgfmathresult{5}}{}%
}
\pgfplotsset{boxplot legend/.style={
legend image code/.code={
\draw[#1] (0cm,0cm) rectangle (0.6cm,0.3cm)
(0.3cm,0cm) -- (0.3cm,-0.1cm) (0.1cm,-0.1cm) -- (0.5cm,-0.1cm)
(0.3cm,0.3cm) -- (0.3cm,0.4cm) (0.1cm,0.4cm) -- (0.5cm,0.4cm);
},
}}
\newcounter{iloop}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
legend entries = {A, B,C,D,E},
% legend to name={legend},
% name=border,
boxplot/draw direction=y,
ylabel={time (s)},
height=8cm,width=12cm,
boxplot={
% in a formular:
draw position={
1/10+ floor(\plotnumofactualtype/5)
+ 1/5*fpumod(\plotnumofactualtype,5)
},
% that means the box extend must be at most 0.2 :
box extend=0.15,
},
% ... it also means that 1 unit in x controls the width:
%x=3cm,
% ... and it means that we should describe intervals:
xtick={0,1,2,...,50},
% x tick label as interval,
xticklabels={%
{Region 1},%
{Region 2},%
{Region 3},%
{Region 4},%
},
x tick label style={
text width=2.5cm,
align=right,
xshift=6ex
},
cycle list={{green},{cyan},{blue},{orange},{red}},
legend style={at={(0.02,0.98)},anchor=north west},
]
\setcounter{iloop}{0}
\pgfplotsinvokeforeach{{green},{cyan},{blue},{orange},{red}}
{\addlegendimage{boxplot legend=#1}
\stepcounter{iloop}
\edef\temp{\noexpand\addlegendentry[#1]{\Alph{iloop}}}
\temp}
\addplot table [row sep=\\,y index=0] {
data\\
6\\
51\\
71\\
50\\
125\\
};
%\addlegendentry{A}
\addplot table [row sep=\\,y index=0] {
data\\
8\\
69\\
90\\
70\\
153\\
};
\addplot table [row sep=\\,y index=0] {
data\\
75\\
41\\
30\\
88\\
29\\
};
\addplot table [row sep=\\,y index=0] {
data\\
56\\
80\\
356\\
20\\
36\\
};
\addplot table [row sep=\\,y index=0] {
data\\
68\\
34\\
70\\
32\\
37\\
};
% block 2
\addplot table [row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot table [row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot table [row sep=\\,y index=0] {
data\\
759\\
419\\
309\\
883\\
299\\
};
\addplot table [row sep=\\,y index=0] {
data\\
516\\
480\\
1356\\
200\\
736\\
};
\addplot table [row sep=\\,y index=0] {
data\\
684\\
340\\
700\\
325\\
377\\
};
% block 3
\addplot table [row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot table [row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot table [row sep=\\,y index=0] {
data\\
759\\
419\\
309\\
883\\
299\\
};
\addplot table [row sep=\\,y index=0] {
data\\
516\\
480\\
1356\\
200\\
736\\
};
\addplot table [row sep=\\,y index=0] {
data\\
684\\
340\\
700\\
325\\
377\\
};
% block 4
\addplot table [row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot table [row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot table [row sep=\\,y index=0] {
data\\
759\\
419\\
309\\
883\\
299\\
};
\addplot table [row sep=\\,y index=0] {
data\\
516\\
480\\
1356\\
200\\
736\\
};
\addplot table [row sep=\\,y index=0] {
data\\
684\\
340\\
700\\
325\\
377\\
};
\end{axis}
\end{tikzpicture}
\end{document}
这是一个支持任意文本的版本。
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\pgfplotsset{compat=1.8}
% borrowed from <https://tex.stackexchange.com/a/145967/95441>
\pgfmathdeclarefunction{fpumod}{2}{%
\pgfmathfloatdivide{#1}{#2}%
\pgfmathfloatint{\pgfmathresult}%
\pgfmathfloatmultiply{\pgfmathresult}{#2}%
\pgfmathfloatsubtract{#1}{\pgfmathresult}%
% replaced `0' by `5' to make it work for this problem
\pgfmathfloatifapproxequalrel{\pgfmathresult}{#2}{\def\pgfmathresult{5}}{}%
}
\pgfplotsset{boxplot legend/.style={
legend image code/.code={
\draw[#1] (0cm,-0.1cm) rectangle (0.4cm,0.1cm)
(0.2cm,-0.1cm) -- (0.2cm,-0.2cm) (0.05cm,-0.2cm) -- (0.35cm,-0.2cm)
(0.2cm,0.1cm) -- (0.2cm,0.2cm) (0.05cm,0.2cm) -- (0.35cm,0.2cm);
\path (0cm,0.24cm) (0cm,-0.24cm);
},
}}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
legend entries = {A, B,C,D,E},
% legend to name={legend},
% name=border,
boxplot/draw direction=y,
ylabel={time (s)},
height=8cm,width=12cm,
boxplot={
% in a formular:
draw position={
1/10+ floor(\plotnumofactualtype/5)
+ 1/5*fpumod(\plotnumofactualtype,5)
},
% that means the box extend must be at most 0.2 :
box extend=0.15,
},
% ... it also means that 1 unit in x controls the width:
%x=3cm,
% ... and it means that we should describe intervals:
xtick={0,1,2,...,50},
% x tick label as interval,
xticklabels={%
{Region 1},%
{Region 2},%
{Region 3},%
{Region 4},%
},
x tick label style={
text width=2.5cm,
align=right,
xshift=6ex
},
cycle list={{green},{cyan},{blue},{orange},{red}},
legend style={at={(0.02,0.98)},anchor=north west},
]
\setcounter{iloop}{0}
\foreach \Color/\Text in {green/smth,cyan/purr,blue/pft,orange/hibernate,red/honey}
{\edef\temp{\noexpand\addlegendimage{boxplot legend=\Color}
\noexpand\addlegendentry[\Color]{\Text}}
\temp}
\addplot table [row sep=\\,y index=0] {
data\\
6\\
51\\
71\\
50\\
125\\
};
%\addlegendentry{A}
\addplot table [row sep=\\,y index=0] {
data\\
8\\
69\\
90\\
70\\
153\\
};
\addplot table [row sep=\\,y index=0] {
data\\
75\\
41\\
30\\
88\\
29\\
};
\addplot table [row sep=\\,y index=0] {
data\\
56\\
80\\
356\\
20\\
36\\
};
\addplot table [row sep=\\,y index=0] {
data\\
68\\
34\\
70\\
32\\
37\\
};
% block 2
\addplot table [row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot table [row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot table [row sep=\\,y index=0] {
data\\
759\\
419\\
309\\
883\\
299\\
};
\addplot table [row sep=\\,y index=0] {
data\\
516\\
480\\
1356\\
200\\
736\\
};
\addplot table [row sep=\\,y index=0] {
data\\
684\\
340\\
700\\
325\\
377\\
};
% block 3
\addplot table [row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot table [row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot table [row sep=\\,y index=0] {
data\\
759\\
419\\
309\\
883\\
299\\
};
\addplot table [row sep=\\,y index=0] {
data\\
516\\
480\\
1356\\
200\\
736\\
};
\addplot table [row sep=\\,y index=0] {
data\\
684\\
340\\
700\\
325\\
377\\
};
% block 4
\addplot table [row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot table [row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot table [row sep=\\,y index=0] {
data\\
759\\
419\\
309\\
883\\
299\\
};
\addplot table [row sep=\\,y index=0] {
data\\
516\\
480\\
1356\\
200\\
736\\
};
\addplot table [row sep=\\,y index=0] {
data\\
684\\
340\\
700\\
325\\
377\\
};
\end{axis}
\end{tikzpicture}
\end{document}