Beschriftung zu pgfplot hinzufügen

Beschriftung zu pgfplot hinzufügen

Ich versuche, einem mit erstellten Plot eine Überschrift hinzuzufügen pgfplot. Der Code lautet wie folgt:

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

Leider ist die resultierende Ausgabe auf drei Seiten aufgeteilt und ich erhalte in der Ausgabe nicht einfach die Abbildung mit Beschriftung auf einer Seite. Was übersehe ich?

Antwort1

Wenn Sie diese Abbildung woanders verwenden, sollten Sie die Beschriftung dort hinzufügen und nicht in LaTeX. Daher möchten Sie wahrscheinlich , und entfernen \begin{figure}, sodass Sie übrig bleiben mit\caption{..}\end{figure}

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

Solltest du [scale=10]übrigens unbedingt entfernen.

Wenn Sie jedoch auf der Überschrift bestehen, entfernen Sie die tikzOption aus der Klasse, beheben Sie den Tippfehler in varwidth, fügen Sie das capt-ofPaket hinzu und fügen Sie hinzu, \setcounter{figure}{2}ob dies die dritte Zahl sein soll, wie der Titel angibt, entfernen Sie die figureUmgebung und verwenden Sie \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}

Bildbeschreibung hier eingeben

verwandte Informationen