6

I have a groupplot with three bar charts using a common legend. This worked great when I was using color, but now I have to prep for publication in black and white. I'm not well versed at all in pgfplots.

I changed the bars from solid color fills to patterns, but I don't know how to produce a useful legend now. Any help on how I could get the patterns into the legend is most appreciated.

    \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}

enter image description here

1 Answer 1

6

You can add the option ybar legend explained in page 211 of the v1.12 manual to the axis options. You can also use the area legend option in the axis options, explained on the same page of the manual. Note that I had to load the groupplots library and set the compatibility compat=1.12 in your MWE.

\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: enter image description here

area legend: enter image description here

4
  • Thank you very much and my apologies for the missing elements of the MWE...novice mistakes.
    – jerH
    May 9, 2015 at 3:14
  • No problem! Thank you for including an MWE at all - it made answering this question the work of moments and convinced me to answer; without the MWE you would not have an answer yet :-)
    – darthbith
    May 9, 2015 at 3:17
  • The area legend looks even better....can't believe I missed this in the manual...I swear I did look first :)
    – jerH
    May 9, 2015 at 3:19
  • I agree, and I'm not sure the purpose of the ybar legend but it is (by the name) what's intended for ybar plots... Its a bit confusing, I think I first found the answer somewhere on here a while ago, which is how I knew what you were looking for :-)
    – darthbith
    May 9, 2015 at 3:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .