1

If followed an example from sharelatex.com for a simple bar graph.

\begin{tikzpicture}
\begin{axis}[
    x tick label style={
        /pgf/number format/1000 sep=},
    ylabel=Year,
    enlargelimits=0.05,
    legend style={at={(0.5,-0.1)},
    anchor=north,legend columns=-1},
    ybar interval=0.7,
]
\addplot 
    coordinates {(2012,408184) (2011,408348)
         (2010,414870) (2009,412156) (2008,415 838)}; % 2008 coordinate given, but does not show
\addplot 
    coordinates {(2012,388950) (2011,393007) 
        (2010,398449) (2009,395972) (2008,398866)}; % same here
\legend{Men,Women}
\end{axis}
\end{tikzpicture}

This plots fairly nicely (as one can see on the website too). After copying and adjusting it to my needs, I realized that it swallows the last coordinate.

This seems curious to me. Is this intended? If not, how to fix this properly (i.e. not adding a dummy coordinate)?

5
  • Sorry, what do you mean with "it swallows the last coordinate." ?
    – user31034
    May 30, 2014 at 16:25
  • @ferahfeza Look at the picture on sharelatex.com/learn/Pgfplots_package#Bar_graphs the entries concerning 2008 are missing.
    – stefan
    May 30, 2014 at 16:26
  • sorry I didn't noticed it.
    – user31034
    May 30, 2014 at 17:02
  • If I change ybar interval=0.7 into ybar, I get all bars.
    – egreg
    May 30, 2014 at 17:42
  • @egreg for me too, but bars now overlap and the x-axis labels overlap too. That's definitively worse than the workaround
    – stefan
    May 30, 2014 at 18:58

1 Answer 1

2

It is explained in the manual:

There is one conceptional difference when working with intervals: an interval is defined by two coordinates. Since ybar has one value for each interval, the ith bar is defined by
1. the y value of the ith coordinates,
2. the x value of the ith coordinate as left interval boundary,
3. the x value of the (i + 1)th coordinate as right interval boundary.
Consequently, there is one coordinate too much: the last coordinate will only be used to determine the interval width; its y value doesn't influence the bar appearance.

If you do not like adding a dummy coordinate you have to remove the ybar interval option. Instead you can adjust some other options:

  • ybar=<distance between two bars at the same coordinate>
  • bar width=<width of a bar>, can be a dimension or a axis unit
  • increase enlarge x limits

\documentclass[margin=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    x tick label style={
        /pgf/number format/1000 sep=},
    ylabel=Year,
    enlarge y limits=0.05,
    enlarge x limits=0.15,% increase limits for x
    legend style={at={(0.5,-0.1)},
    anchor=north,legend columns=-1},
    ybar=3pt, % distance between two bars at the same coordinate
    bar width=0.3 % defines the width of a bar in axis units or as a dimension
]
\addplot
    coordinates {(2012,408184) (2011,408348)
         (2010,414870) (2009,412156) (2008,415 838)};
\addplot 
    coordinates {(2012,388950) (2011,393007) 
        (2010,398449) (2009,395972) (2008,398866)};
\legend{Men,Women}
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

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