1

I would like to plot some data in a bar chat using ybar. The data corresponds to years on the x-axis. Now, for some years I have three data values to plot and for others only one value. The problem is when I use ybar, the space between the sets of data is not uniform. How can I make the spacing uniform such that the space between the data sets is uniform? Here's my MWE:

\documentclass{standalone}

\usepackage{pgfplots}

\begin{document}
        \begin{tikzpicture}
        \begin{axis}[
        %small,
        ybar,%=8pt, % configures ‘bar shift’
%       ybar interval=0.05,
        enlargelimits=0.25,
%       enlarge x limits={abs=1cm}, % The distance between the center of the first bar and the left edge
        ylabel={PMU Count},
        symbolic x coords={2009, 2013, 2014, 2015},
        xtick=\empty,
        %tick label style={font=\footnotesize},
        legend style={at={(0.5,-0.15)},
            anchor=north,legend columns=-1},
        nodes near coords,
        every node near coord/.append style={font=\tiny},
        nodes near coords align={vertical},
        ymode=log,
        point meta=rawy,
        extra x ticks={2009, 2013, 2014, 2015}, 
        extra x tick labels={\kern-12mm 2009,\kern-4mm 2013,\kern-12mm 2014,\kern8mm 2015},
        every x tick/.style={white},
%       x=2cm,% Distance between the centers of the bars
        ]
        \addplot coordinates {(2009, 200) (2014, 1700)};
        \addplot[fill=green]  coordinates {(2013, 60)};
        \addplot[fill=gray!50]  coordinates {(2015, 30)};
        \addplot[fill=red!80]  coordinates {(2015, 2400)};
    \legend{N,I,B,C}

        \end{axis}
        \end{tikzpicture}

\end{document}
2
  • 2
    The coordinates on x-axis {2009,2013,2014,2015} are not uniformly distributed, so why should they be?
    – Enlico
    Jan 14, 2016 at 15:11
  • That's exactly the problem. so when I do the plotting, the distance between 2013 and 2014 is not equal to the distance between 2014 and 2015. the plot does not look elegant then
    – user95439
    Jan 18, 2016 at 10:03

1 Answer 1

0

This is not yet an answer. I simply didn't understand the question very well.

What's wrong with the figure produced by the following code?

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}
\begin{axis}[grid=major,xtick={2008,2009,...,2016},
    x tick label style={rotate=90,
        /pgf/number format/1000 sep=},
    ylabel=Population,
    enlargelimits=0.15,
    legend style={at={(0.5,1.15)},
        anchor=north,legend columns=-1},
    ybar,
    bar width=3pt,
]
\addplot
    coordinates {(2009,200) (2010,-100) (2011,-100) (2012,-100) (2013,-100) (2014,1700) (2015,-100)};
\addplot
    coordinates {(2009,-100) (2010,-100) (2011,-100) (2012,-100) (2013,60) (2014,-100) (2015,-100)};
\addplot
    coordinates {(2009,-100) (2010,-100) (2011,-100) (2012,-100) (2013,-100) (2014,-100) (2015,30)};
\addplot
    coordinates {(2009,-100) (2010,-100) (2011,-100) (2012,-100) (2013,-100) (2014,-100) (2015,2400)};

\legend{First,Second,Third,Fourth}
\end{axis}
\end{tikzpicture}


\end{document}

I used your data as well as other data with constant negative value in order to make you see that the bars are correctly centered with respect to sticks. The resulting figure is the following:

enter image description here

4
  • thanks Enrico for your efforts. However, my problem is if for example we have zero values for year 2014 instead of negative values, the distance between the remaining bar for year 2014 and bars for year 2015 will appear to be different from that between the bars for year 2014 and those of 2013. So, although the bar chart is correctly plotted, it does not look elegant or nice if you like. I hope this clarifies the problem.
    – user95439
    Jan 18, 2016 at 14:13
  • Say we eliminate the negative values I've added before; then, if the blue bar (1700) is correctly displaced with respect to the black bar (2400), it is not with respect to the brown bar (30).
    – Enlico
    Jan 18, 2016 at 14:34
  • To say in other words, all the bars with a common x coordinate are considered as a whole, and as whole they're centered. This is not ugly when data are not so sparse (many zeros).
    – Enlico
    Jan 18, 2016 at 14:36
  • Take a look at the spacing between bars in the figure in the answer to this question
    – Enlico
    Jan 18, 2016 at 14:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .