I'm trying to draw the following plot:
\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar,
ymin=0, ymax=90,
width=5.5cm,
height=6cm,
symbolic x coords={A,B},
xtick=data,
bar width=15pt,
axis lines*=left,
ytick={10,30,...,90},
nodes near coords,
xticklabel style={text height=1.5ex},
ymajorgrids,
enlarge x limits=.3,
]
\addplot[fill=red] coordinates {
(A,16.6)
};
\addplot[fill=green] coordinates {
(B,23.9)
};
\end{axis}
\end{tikzpicture}
\end{document}
However it does not seem to work out: the second xtick coordinate (B) doesn't show up.
What I want is to have a simple plot with two or more bars each with a different color.
thanks for your help
Best Answer
When you use
xtick=data
, only the first\addplot
command is used to determine the tick positions. To get both tick labels, usextick={A,B}
.By default, PGFPlots assumes that each
\addplot
series will have values at the same coordinates, which is why the bars for each series are offset to prevent overlap. In your special case of all\addplot
commands using different coordinates, that's not desired. To switch off this shift, set setbar shift=0pt
.