Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'am trying to create a colored histogram using barplots using the following code:

\begin{tikzpicture}
\begin{axis}[ybar,
ymin=0,
bar width=0.2cm,
axis x line*=left,
nodes near coords=\rotatebox{90}{\scriptsize\pgfmathprintnumber\pgfplotspointmeta},
enlarge x limits=false,
grid=major,
height=7cm,
title={All Results},
xlabel={bias from reference in \%},
ylabel={Number of Results},
symbolic x coords={$<-20$,$-20$,$-19$,$-18$,$-17$,$-16$,$-15$,$-14$,$-13$,$-12$,$-11$,$-10$,$-9$,$-8$,$-7$,$-6$,$-5$,$-4$,$-3$,$-2$,$-1$,$0$,$1$,$2$,$3$,$4$,$5$,$6$,$7$,$8$,$9$,$10$,$11$,$12$,$13$,$14$,$15$,$16$,$17$,$18$,$19$,$20$,$>20$},
xtick=data,
xticklabel style={inner sep=0pt, anchor=north east, rotate=45},
width=\textwidth]
\addplot[red,fill=red!40!white] coordinates {($<-20$,3) ($-20$,2) ($20$,0) ($>20$,0)};
\addplot[yellow,fill=yellow!40!white] coordinates {($-19$,1) ($-18$,2) ($-17$,0) ($-16$,1) ($-15$,0) ($15$,0) ($16$,0) ($17$,0) ($18$,0) ($19$,0)};
\addplot[blue,fill=blue!40!white] coordinates {($-14$,0) ($-13$,1) ($-12$,1) ($-11$,0) ($-10$,1) ($10$,0) ($11$,1) ($12$,0) ($13$,0) ($14$,1)};
\addplot[green,fill=green!40!white] coordinates {($-9$,1) ($-8$,5) ($-7$,14) ($-6$,46) ($-5$,105) ($-4$,185) ($-3$,126) ($-2$,297) ($-1$,276) ($0$,301) ($1$,250) ($2$,153) ($3$,67) ($4$,85) ($5$,37) ($6$,29) ($7$,9) ($8$,2) ($9$,10)};
\legend{Bad,Fair,Good,Excellent}
\end{axis}
\end{tikzpicture}

Resulting in something that almost looks great, but I'am missing some x tick and some bars are not on the right spot :(

Someone got an idea to fix this, or can suggest an alternative?

Grtz, Berry

share|improve this question

1 Answer 1

up vote 7 down vote accepted

There is one problem in your listing: if you say ybar as axis option, pgfplots prepares the overal axis for one or more bar plots - and groups these bars automatically.

This grouping causes your problem "are not on the right spot" (please refer to the reference manual to see the use case for this grouping).

Thus, the main solution is to avoid ybar as axis option. If you write /tikz/ybar, it will work. In this case, the axis is unaware of bar-specific stuff and you can proceed as usual. You may want to add ybar legend (to format the legend as for bar plots) and xtick align=outside explicitly.

The other question "where are my remaining tick labels!?" can be solved by stating which tick positions should be displayed - your choice of xtick=data displays the x coordinates of the first plot. In your case, that is $<-20$,$-20$,$20$,$>20$. A solution would be to provide a "more useful" selection.

Here is a suggestion which has a selection of ticks and two "extra xticks" for the boundaries:

enter image description here

\begin{tikzpicture}
\begin{axis}[/tikz/ybar, % -------- CF
ybar legend, %-------- CF
xtick align=outside,% -------- CF
ymin=0,
bar width=0.2cm,
axis x line*=left,
nodes near coords=\rotatebox{90}{\scriptsize\pgfmathprintnumber\pgfplotspointmeta},
enlarge x limits=false,
grid=major,
height=7cm,
title={All Results},
xlabel={bias from reference in \%},
ylabel={Number of Results},
symbolic x coords={$<-20$,$-20$,$-19$,$-18$,$-17$,$-16$,$-15$,$-14$,$-13$,$-12$,$-11$,$-10$,$-9$,$-8$,$-7$,$-6$,$-5$,$-4$,$-3$,$-2$,$-1$,$0$,$1$,$2$,$3$,$4$,$5$,$6$,$7$,$8$,$9$,$10$,$11$,$12$,$13$,$14$,$15$,$16$,$17$,$18$,$19$,$20$,$>20$},
xtick={$-20$,$-15$,$-10$,$-5$,$0$,$5$,$10$,$15$,$20$},
minor x tick num=4, % ----------- CF
extra x ticks={$<-20$,$>20$},% ----------- CF
extra x tick style={xticklabel style={yshift=-15pt}},% ----------- CF
%xticklabel style={inner sep=0pt, anchor=north east, rotate=45},% ----------- CF
width=\textwidth]
\addplot[red,fill=red!40!white] coordinates {($<-20$,3) ($-20$,2) ($20$,0) ($>20$,0)};
\addplot[yellow,fill=yellow!40!white] coordinates {($-19$,1) ($-18$,2) ($-17$,0) ($-16$,1) ($-15$,0) ($15$,0) ($16$,0) ($17$,0) ($18$,0) ($19$,0)};
\addplot[blue,fill=blue!40!white] coordinates {($-14$,0) ($-13$,1) ($-12$,1) ($-11$,0) ($-10$,1) ($10$,0) ($11$,1) ($12$,0) ($13$,0) ($14$,1)};
\addplot[green,fill=green!40!white] coordinates {($-9$,1) ($-8$,5) ($-7$,14) ($-6$,46) ($-5$,105) ($-4$,185) ($-3$,126) ($-2$,297) ($-1$,276) ($0$,301) ($1$,250) ($2$,153) ($3$,67) ($4$,85) ($5$,37) ($6$,29) ($7$,9) ($8$,2) ($9$,10)};
\legend{Bad,Fair,Good,Excellent}
\end{axis}

\end{tikzpicture}

You may want to give a little bit extra space using, say, enlarge x limits=0.01

share|improve this answer
    
Great! Thank you for the quick answer! –  Berry Aug 2 '12 at 7:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.