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.

Thanks to Caramdir and Alan Munn I am now able to create basic bar charts that use text as x axis labels. Functionally they are perfect, but some of them look kind of ugly. See for yourself:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
             width=15cm,
             height=8cm,
             symbolic x coords={elso es masodrendu varosok,
                                falu es varos kozti atmenet,
                                funkcionalis ertelemben nem varos},
             x tick label style={font=\small},
             xtick=data,
             ymin=0,
             ymax=65,
             ylabel=\%,
             ylabel style={rotate=-90},
             ybar,
             bar width=45pt,
             ]
\addplot coordinates {
                      (elso es masodrendu varosok,36)
                      (falu es varos kozti atmenet,4)
                      (funkcionalis ertelemben nem varos,60)};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

% I have removed all non-English characters from the sample code
% but the image below was generated with the original strings.

Now, the chart has everything I need, but it would look much prettier if I could break the text labels into at least two lines and move the two side bars closer to the center bar. Unfortunately, {falu es varos\\kozti atmenet} is not usable as a valid x coordinate.

And I have also been unable to find a way to explicitly set spacing between the bars when using non-numeric coordinates. I tried to set the bar width to a bigger value thus forcing them closer to each other, but it does not work. The bars get fatter but they keep their positions.

Any help would be much appreciated.

share|improve this question

1 Answer 1

up vote 14 down vote accepted

You can add the key text width=<dimension>,align=center to the x tick label style option to make the tick labels wrap and center.

Decreasing the width of the plot then moves the columns closer together. By introducing enlarge x limits=<value>, you can move the outer columns closer to the center.

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
             width=8cm,
             height=8cm,
             symbolic x coords={elso es masodrendu varosok,
                                falu es varos kozti atmenet,
                                funkcionalis ertelemben nem varos},
             x tick label style={font=\small,text width=1.7cm,align=center},
             xtick=data,
             ymin=0,
             ymax=65,
             ylabel=\%,
             ylabel style={rotate=-90},
             ybar,
             enlarge x limits=.3,
             bar width=45pt,
             ]
\addplot coordinates {
                      (elso es masodrendu varosok,36)
                      (falu es varos kozti atmenet,4)
                      (funkcionalis ertelemben nem varos,60)};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

alt text

share|improve this answer
    
It looks excellent. Thank you. It's simple and elegant. Perfect. –  Heisenb0rg Jan 11 '11 at 6:38

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.