![图例中的条形图模式](https://linux22.com/image/311039/%E5%9B%BE%E4%BE%8B%E4%B8%AD%E7%9A%84%E6%9D%A1%E5%BD%A2%E5%9B%BE%E6%A8%A1%E5%BC%8F.png)
我有一个包含三个条形图的组图,使用一个共同的图例。当我使用彩色时,这种方法效果很好,但现在我必须准备以黑白格式发布。我对 pgfplots 一点也不熟悉。
我将条形图从纯色填充改为图案,但我不知道如何制作有用的图例。任何有关如何将图案放入图例的帮助都将不胜感激。
\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{patterns}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{groupplot}[
legend columns=-1,
legend entries={{\color{red}{\tiny Random}},{\color{blue}{\tiny ++Cost}},{\color{black}{\tiny ++FTE}},{\color{green}{\tiny ++Resources}},{\color{orange}{\tiny Hold All}}},
legend to name=CombinedLegendBar,
footnotesize,
group style={
group size=3 by 1,
xlabels at=edge bottom,
ylabels at=edge left,
xticklabels at=edge bottom}]
\nextgroupplot[title={\scriptsize Empirical CDF}, xticklabels=\empty]
\addplot[ybar, pattern=horizontal lines] coordinates { (1, 6.886)};
\addplot[ybar, pattern=vertical lines] coordinates { (2, 8.501)};
\addplot[ybar, pattern=grid] coordinates { (3, 10.179)};
\addplot[ybar, pattern=dots] coordinates { (4, 11.14)};
\addplot[ybar, pattern=north east lines] coordinates { (5, 15.001)};
\nextgroupplot[title={\scriptsize Triangular CDF}, xticklabels=\empty]
\addplot[ybar, pattern=horizontal lines] coordinates {(1, 6.886)};
\addplot[ybar, pattern=vertical lines] coordinates {(2, 7.745)};
\addplot[ybar, pattern=grid] coordinates {(3, 8.606)};
\addplot[ybar, pattern=dots] coordinates {(4, 8.630)};
\addplot[ybar, pattern=north east lines] coordinates {(5, 15.001)};
\nextgroupplot[title={\scriptsize LN/Exponential CDF}, xticklabels=\empty]
\addplot[ybar, pattern=horizontal lines] coordinates { (1, 6.886)};
\addplot[ybar, pattern=vertical lines] coordinates { (2, 8.428)};
\addplot[ybar, pattern=grid] coordinates { (3, 9.964)};
\addplot[ybar, pattern=dots] coordinates { (4, 11.087)};
\addplot[ybar, pattern=north east lines] coordinates { (5, 15.001)};
\end{groupplot}
\end{tikzpicture}
\ref{CombinedLegendBar}
\caption{Triage++ Performance}
\label{PlusPlusCombinedBar}
\end{figure}
\end{document}
答案1
ybar legend
您可以将v1.12 手册第 211 页中说明的选项添加到轴选项中。您还可以使用area legend
轴选项中的选项,该选项在手册的同一页中说明。请注意,我必须加载库并在 MWE 中groupplots
设置兼容性。compat=1.12
\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.12}
\usetikzlibrary{patterns}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{groupplot}[
legend columns=-1,
legend entries={{\color{red}{\tiny Random}},{\color{blue}{\tiny ++Cost}},{\color{black}{\tiny ++FTE}},{\color{green}{\tiny ++Resources}},{\color{orange}{\tiny Hold All}}},
legend to name=CombinedLegendBar,
footnotesize,
ybar legend,
% area legend, % This is the alternate option
group style={
group size=3 by 1,
xlabels at=edge bottom,
ylabels at=edge left,
xticklabels at=edge bottom}]
\nextgroupplot[title={\scriptsize Empirical CDF}, xticklabels=\empty]
\addplot[ybar, pattern=horizontal lines] coordinates { (1, 6.886)};
\addplot[ybar, pattern=vertical lines] coordinates { (2, 8.501)};
\addplot[ybar, pattern=grid] coordinates { (3, 10.179)};
\addplot[ybar, pattern=dots] coordinates { (4, 11.14)};
\addplot[ybar, pattern=north east lines] coordinates { (5, 15.001)};
\nextgroupplot[title={\scriptsize Triangular CDF}, xticklabels=\empty]
\addplot[ybar, pattern=horizontal lines] coordinates {(1, 6.886)};
\addplot[ybar, pattern=vertical lines] coordinates {(2, 7.745)};
\addplot[ybar, pattern=grid] coordinates {(3, 8.606)};
\addplot[ybar, pattern=dots] coordinates {(4, 8.630)};
\addplot[ybar, pattern=north east lines] coordinates {(5, 15.001)};
\nextgroupplot[title={\scriptsize LN/Exponential CDF}, xticklabels=\empty]
\addplot[ybar, pattern=horizontal lines] coordinates { (1, 6.886)};
\addplot[ybar, pattern=vertical lines] coordinates { (2, 8.428)};
\addplot[ybar, pattern=grid] coordinates { (3, 9.964)};
\addplot[ybar, pattern=dots] coordinates { (4, 11.087)};
\addplot[ybar, pattern=north east lines] coordinates { (5, 15.001)};
\end{groupplot}
\end{tikzpicture}
\ref{CombinedLegendBar}
\caption{Triage++ Performance}
\label{PlusPlusCombinedBar}
\end{figure}
\end{document}
ybar legend
:
area legend
: