[Tex/LaTex] Customizing legend in pgfplots

pgfplotstikz-pgf

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

enter image description here

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:

How can I configure the legend such that it occupies a prescribed amount of space like the width of the figure?

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 using

enter image description here

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}
    \def\INNERSEP{2pt}
    \def\WIDTH{12cm}
    \begin{axis}[
                ybar,
                height=6cm,
                title={Some plot},
                width=\WIDTH,
                scale only axis, % <------------
                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 image code/.code={%
                    \draw[#1, draw=none] (0cm,-0.1cm) rectangle (0.6cm,0.1cm);
                },  
                extra description/.code={
                    \node[
                        draw,
                        text depth=0pt,
                        at={(0.0,-0.15)},
                        anchor=north west,
                        text width=\WIDTH-2*\INNERSEP,
                        inner sep=\INNERSEP,
                        outer sep=0pt,
                    ] {
                        % Some TeX/LaTeX construct which constructs
                        % the legend:
                        \hbox to \linewidth{%
                            \textbf{Legend:} 
                            \ref{A} Legend a \hfill \ref{B} Legend b \hfill
                        }%
                    };
                },
            ]       
            \addplot coordinates {
                (1, 0.2064)
                (2, 0.3961)
                (3, 0.5961)
                (4, 0.7961)};
                \label{A}
            \addplot coordinates {
                (1, 0.3064)
                (2, 0.4961)
                (3, 0.6961)
                (4, 0.8961)};
                \label{B}
    \end{axis} 
\end{tikzpicture}
\end{document}

Regarding your second question:

How can I recondigure the appearance of the small images for each plot?

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).

enter image description here

\documentclass{standalone}

\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 image code/.code={%
                    \draw[#1, draw=none] (0cm,-0.1cm) rectangle (0.6cm,0.1cm);
                },  
                legend style={
                    draw=none, % ?
                    text depth=0pt,
                    at={(0.0,-0.15)},
                    anchor=north west,
                    legend columns=-1,
                    % default spacing:
                    column sep=1cm,
                    % The text "Legend:"
                    /tikz/column 2/.style={column sep=0pt,font=\bfseries},
                    %
                    % the space between legend image and text:
                    /tikz/every odd column/.append style={column sep=0cm},
                },
            ]       
            \addlegendimage{empty legend}
            \addlegendentry{\textbf{Legend:}}                   
            \addplot coordinates {
                (1, 0.2064)
                (2, 0.3961)
                (3, 0.5961)
                (4, 0.7961)}; \addlegendentry{legend a}
            \addplot coordinates {
                (1, 0.3064)
                (2, 0.4961)
                (3, 0.6961)
                (4, 0.8961)}; \addlegendentry{lengend b}
    \end{axis} 
\end{tikzpicture}
\end{document}

Regarding the third question:

How can I reconfigure the legend such that it starts right next to the text 'Legend:'?

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.