Adicionar legenda ao pgfplot

Adicionar legenda ao pgfplot

Estou tentando adicionar uma legenda a um enredo feito com pgfplot. O código é o seguinte:

\documentclass[border=20pt,tikz,varwith]{standalone}

\usepackage{pgfplots}
\pgfplotsset{
/pgfplots/xbar legend/.style={
    /pgfplots/legend image code/.code={%
        \draw[##1,/tikz/.cd,bar width=3pt,yshift=-0.2em,bar shift=0pt]
        plot coordinates {(0.8em,0cm)(0.6em,2*\pgfplotbarwidth)};}% <-changed
}
}


\usetikzlibrary{patterns}

\begin{document}
\begin{figure}
    \begin{tikzpicture}[scale=10]
    \begin{axis}[xbar,title style={xshift=-1.5cm},
    title=Figure 3..,
    enlargelimits=0.15,
    legend style={at={(1.3,.6)},
        anchor=north,reverse legend,legend cell align=left},
    yticklabels={Cat,Horse, Charly, Otto, Elisabeth, Fredy },
    xlabel={Happyness}]

    \addplot [draw=black,
    pattern=horizontal lines light blue,
    error bars/.cd,
    x dir=both,
    x explicit
    ] 
    coordinates {
        (10,5) -=(7,0) += (1,0) 
        (2,10) -=(2,0)  += (2,0) 
        (4,15)   -=(4,0) += (4,0)
        (5,20)   -=(5,0) += (5,0)
    };

    \addplot [draw=black,
    pattern=horizontal lines dark blue,
    error bars/.cd,
    x dir=both,
    x explicit
    ] 
    coordinates {
        (23,5)  -=(5,0) += (0.41-0.23,0) 
        (17,10)  -=(4,0) += (3,0) 
        (4,15)   -=(4,0) += (4,0) 
        (13,20)   -=(3,0) += (4,0) 
    }; 

    \legend{ Sweets, Playground}
    \draw[ultra thin] (axis cs:0,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:0,\pgfkeysvalueof{/pgfplots/ymax});
    \end{axis} 
    \end{tikzpicture}
    \caption{bla}
    \end{figure}
 \end{document}

Infelizmente, a saída resultante é dividida em três páginas e não estou simplesmente obtendo a figura com legenda em uma página da saída. o que estou perdendo?

Responder1

Se você estiver usando esta figura em outro lugar, adicione a legenda lá, e não no LaTeX. Portanto, você provavelmente deseja remover \begin{figure}, \caption{..}e \end{figure}, então fica com

\documentclass{standalone}
<preamble>
\begin{document}
\begin{tikzpicture}
<plot>
\end{tikzpicture}
\end{document}

[scale=10]A propósito, você definitivamente deveria remover .

Mas se você insistir em ter a legenda, retire a tikzopção da classe, corrija o erro de digitação varwidth, adicione o capt-ofpacote, adicione \setcounter{figure}{2}se esta for a terceira figura, como o título indica, remova o figureambiente e use \captionof{figure}{..}:

\documentclass[border=20pt,varwidth]{standalone}

\usepackage{pgfplots,capt-of}
\pgfplotsset{
/pgfplots/xbar legend/.style={
    /pgfplots/legend image code/.code={%
        \draw[##1,/tikz/.cd,bar width=3pt,yshift=-0.2em,bar shift=0pt]
        plot coordinates {(0.8em,0cm)(0.6em,2*\pgfplotbarwidth)};}% <-changed
}
}


\usetikzlibrary{patterns}

\begin{document}

    \begin{tikzpicture}%[scale=10]
    \begin{axis}[xbar,title style={xshift=-1.5cm},
    title=Figure 3..,
    enlargelimits=0.15,
    legend style={at={(1.3,.6)},
        anchor=north,reverse legend,legend cell align=left},
    yticklabels={Cat,Horse, Charly, Otto, Elisabeth, Fredy },
    xlabel={Happyness}]

    \addplot [draw=black,
    pattern=horizontal lines light blue,
    error bars/.cd,
    x dir=both,
    x explicit
    ] 
    coordinates {
        (10,5) -=(7,0) += (1,0) 
        (2,10) -=(2,0)  += (2,0) 
        (4,15)   -=(4,0) += (4,0)
        (5,20)   -=(5,0) += (5,0)
    };

    \addplot [draw=black,
    pattern=horizontal lines dark blue,
    error bars/.cd,
    x dir=both,
    x explicit
    ] 
    coordinates {
        (23,5)  -=(5,0) += (0.41-0.23,0) 
        (17,10)  -=(4,0) += (3,0) 
        (4,15)   -=(4,0) += (4,0) 
        (13,20)   -=(3,0) += (4,0) 
    }; 

    \legend{ Sweets, Playground}
    \draw[ultra thin] (axis cs:0,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:0,\pgfkeysvalueof{/pgfplots/ymax});
    \end{axis} 
    \end{tikzpicture}
    \captionof{figure}{bla}

 \end{document}

insira a descrição da imagem aqui

informação relacionada