Agregar título a pgfplot

Agregar título a pgfplot

Estoy intentando agregar un título a una trama hecha con pgfplot. El código es el siguiente:

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

Desafortunadamente, el resultado resultante se divide en tres páginas y no obtengo simplemente la figura con el título en una página del resultado. ¿Qué me estoy perdiendo?

Respuesta1

Si estás usando esta figura en otro lugar, entonces debes agregar el título allí, y no en LaTeX. Por lo tanto, probablemente quieras eliminar y \begin{figure}, por lo que te quedará\caption{..}\end{figure}

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

[scale=10]Por cierto, definitivamente deberías eliminarlo .

Pero si insistes en tener el título, elimina la tikzopción de la clase, corrige el error tipográfico varwidth, agrega el capt-ofpaquete, agrega \setcounter{figure}{2}si esta debería ser la tercera figura, como indica el título, elimina el figureentorno y usa \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}

ingrese la descripción de la imagen aquí

información relacionada