我有这个代码:
\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
symbolic x coords={Germany,Spain,UK},
xtick=data,
axis x line=bottom,
axis y line=left,
x tick label style={rotate=45,anchor=east},
ymin=0,
enlarge x limits={abs=0.6cm},
ymax=110,
ytick={0,10,...,100},
ybar=6pt,
bar width=14pt,
ylabel=\% blablabla
]
\addplot[fill=blue] coordinates {
(Germany,10)
};
\addplot[fill=red] coordinates {
(Spain,30)
};
\addplot[fill=yellow] coordinates {
(UK,40)
};
\end{axis}
\end{tikzpicture}
\end{document}
我想用不同的颜色为每个国家绘制一个图。但是,正如您所见,“西班牙”和“英国”没有打印出来,而且图太远了。我该如何解决这个问题?
答案1
xtick=data
仅使用第一个\addplot
命令来确定刻度位置。您可以使用以下方法获得所需结果xtick={Germany,Spain,UK}
\documentclass{standalone}
\usepackage{pgfplots,tikz}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
symbolic x coords={Germany,Spain,UK},
xtick={Germany,Spain,UK},
axis x line=bottom,
axis y line=left,
x tick label style={rotate=45},
ymin=0,
enlarge x limits={abs=0.5cm},
ymax=110,
ytick={0,10,...,100},
ybar=6pt,
bar width=14pt,
ylabel=\% blablabla
]
\addplot[bar shift=0pt,fill=blue] coordinates {
(Germany,10)
};
\addplot[bar shift=0pt,fill=red] coordinates {
(Spain,30)
};
\addplot[bar shift=0pt,fill=yellow] coordinates {
(UK,40)
};
\end{axis}
\end{tikzpicture}
\end{document}