为 pgfplot 添加标题

为 pgfplot 添加标题

我正在尝试为用 制作的图添加标题pgfplot。代码如下:

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

不幸的是,输出结果被分成了三页,而我并没有简单地在输出的一页上得到带标题的图表。我遗漏了什么?

答案1

如果你在其他地方使用此图,那么你应该在那里添加标题,而不是在 LaTeX 中添加。因此,你可能想要删除\begin{figure}\caption{..}\end{figure}这样你就剩下

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

[scale=10]顺便说一句,你一定要把它删除。

但是如果你坚持要有标题,tikz从类中删除选项,修复中的拼写错误varwidth,添加capt-of包,添加\setcounter{figure}{2}如果这应该是第三个图,如标题所示,删除figure环境并使用\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}

在此处输入图片描述

相关内容