3

I am trying to display a legend for a boxplot created in pgfplots.

However the size of the legend is almost as large as the plot and covers up all of my plot, how do I fix this?

enter image description here

\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{pgfplots.statistics}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}

\begin{axis}[
  legend entries = {A, B},
]

\addplot+[boxplot] table [row sep=\\, y index=0] {
  data\\ 9\\ 6\\ 7\\ 7\\ 3\\ 9\\ 10\\ 1\\ 8\\ 7\\ 9\\ 9\\ 8\\ 10\\ 5\\ 10\\     10\\ 9\\ 10\\ 8\\
};

\addplot+[boxplot] table [row sep=\\, y index=0] {
  data\\ 9\\ 6\\ 7\\ 7\\ 3\\ 9\\ 10\\ 1\\ 8\\ 7\\ 9\\ 9\\ 8\\ 10\\ 5\\    10\\ 10\\ 9\\ 10\\ 8\\
};

\end{axis}

\end{tikzpicture}
\end{document}

If one removes either the words legend entries = {A, B} or boxplot from the code, the graphs are displayed as expected, just without a legend respectively not as a boxplot.

I suspect this to be a bug in pgfplots, are there any workarounds?

1
  • 1
    For the record: I added an issue to the tracker. Apr 20, 2021 at 17:21

2 Answers 2

3

Actually, there is no room for a legend inside the plot at all.

boxplot with legend

\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{pgfplots.statistics}
\pgfplotsset{compat=1.12}
\usepackage{graphicx}
\begin{document}
\begin{tikzpicture}

\begin{axis}[
  legend entries = {A, B},
  legend to name={legend},
  name=border
]

\addplot+[boxplot] table [row sep=\\, y index=0] {
  data\\ 9\\ 6\\ 7\\ 7\\ 3\\ 9\\ 10\\ 1\\ 8\\ 7\\ 9\\ 9\\ 8\\ 10\\ 5\\ 10\\     10\\ 9\\ 10\\ 8\\
};

\addplot+[boxplot] table [row sep=\\, y index=0] {
  data\\ 9\\ 6\\ 7\\ 7\\ 3\\ 9\\ 10\\ 1\\ 8\\ 7\\ 9\\ 9\\ 8\\ 10\\ 5\\    10\\ 10\\ 9\\ 10\\ 8\\
};
\end{axis}
\node[below right] at (border.north east) {\ref{legend}};

\end{tikzpicture}
\end{document}
2
  • I don't think it matters whether there is room for the legend, as that was never a problem with other graph types (whether this is wise to do is something else). Your solution of saving the legend to a name and using a node to insert it works though. Jul 21, 2015 at 22:26
  • It looks like a bug. I tried legend pos=outer north east and got the legend with boxplots again. Jul 21, 2015 at 22:31
1

You can use area legend in the axis environment as a workaround:

\begin{axis}[area legend, ...

You must log in to answer this question.

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