
我正在嘗試為使用 製作的繪圖添加標題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}