で作成したプロットにキャプションを追加しようとしています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}
残念ながら、結果の出力は 3 ページに分割されており、出力の 1 ページにキャプション付きの図が単純に表示されません。何が足りないのでしょうか?
答え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}
タイトルが示すようにこれが 3 番目の図である場合は を追加し、環境を削除して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}