I am trying to customize the legend for this graph. First of all, I would like the legend width to be equal to the x-axis width. At the moment, I am setting the width of the legend via
/tikz/every even column/.append style={column sep=1.0cm}
and I could manually adjust this value so that length equals the x-axis width, but I guess that there is a more elegant way to set the width of the legend.
Second, I don't really like the little color rectangles in the legend. I only want one rectangle in front of every entry and not two. This rectangle does not need to have a border and should be vertically centred with the text. The alignment seems to be okay for the rectangle closer to the text, but not for one the far left.
Update: Thanks to Christian Feuersänger's comment, there is possibility to change the rectangles via
legend image code/.code={%
\draw[#1, draw=none] (0cm,-0.1cm) rectangle (0.6cm,0.1cm);
or what Gonzalo Medina proposed
legend image code/.code={%
\draw[#1, draw=none, /tikz/.cd, bar width=3pt, yshift=-0.2em, bar shift=0pt] %
plot coordinates {(2*\pgfplotbarwidth,0.6em)};
Third, I would like to the legend entries to start next to the text "Legend:".
I don't really know if with these requirements it still makes sense to use legend or if it would be better to add everything manually via after end axis/.code
and \ref{label}
.
Graph
MWE
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar,
height=6cm,
title={Some plot},
width=12cm,
axis on top,
y axis line style={opacity=0},
axis x line*=bottom,
tick align=inside,
tick style={draw=none},
xtick=data,
ymajorgrids,
major grid style={draw=white},
xtick={1,2,3,4},
legend style={
at={(0.0,-0.15)},
anchor=north west,
legend columns=-1,
/tikz/every even column/.append style={column sep=1.0cm}
},
]
\addlegendimage{empty legend}
\addlegendentry{\textbf{Legend:}}
\addplot coordinates {
(1, 0.2064)
(2, 0.3961)
(3, 0.5961)
(4, 0.7961)}; \addlegendentry[text width=45pt, text depth=]{legend a}
\addplot coordinates {
(1, 0.3064)
(2, 0.4961)
(3, 0.6961)
(4, 0.8961)}; \addlegendentry[text width=65pt, text depth=]{lengend b}
\end{axis}
\end{tikzpicture}
\end{document}
Best Answer
Regarding your first question:
To my knowledge, this is impossible by means of a PGF
\matrix
-- and these kind of legends are a PGF\matrix
. As considered in your question, the remaining option would be to\label
every plot and generate a legend manually using\ref
to each plot, for example usingRegarding your second question:
As pointed out in some comment, the small images shown in legends can be reconfigured using
legend image code
. A suitable substitute for the small bars might be to use some rectangles (as in your edit).Regarding the third question:
You can distribute the
column sep
such that it does not apply to your first artificial legend entry if you configure/tikz/column 2/.style
. I did that in the example above.You will note that I also added
draw=none
due to the lacking support for your first question.