PGFPlots histogram not showing all series

1,041

I want to write a proper answer to this, for future reference.

It turns out that, actually, I wasn't unable to find the right thing in the ginormous PGFPlot manual :) so I've fixed it (thanks Stefan) using ybar and bar width = .8cm.

Solved! Correct histogram

Final code:

\begin{tikzpicture}
  \begin{axis}[
      x tick label style={
        /pgf/number format/1000 sep=},
      ylabel={\small{Efficienza (\milli\gram\per\gram)}},
      width = 15.4cm, height = 10.7cm,
      legend style={
        font=\scriptsize,
        cells={anchor=west}
      },
      legend pos=north west,
      symbolic x coords={Spugna 1,Spugna 2,Spugna 3,Spugna 4,Spugna 5,Spugna 6},
      xtick=data,
      tick label style={font=\scriptsize},
      ybar,
      bar width = .8cm
    ]
    \addplot coordinates {(Spugna 1,0.56) (Spugna 2,2.84) (Spugna 3,3.57) (Spugna 4,5.45) (Spugna 5,1.12) (Spugna 6,4.29)};
    \addplot coordinates {(Spugna 1,0.66) (Spugna 2,3.10) (Spugna 3,3.77) (Spugna 4,5.61) (Spugna 5,1.27) (Spugna 6,4.27)};
    \legend{Reali,Fitting}
  \end{axis}
 \end{tikzpicture}
Share:
1,041

Related videos on Youtube

fratili
Author by

fratili

BY DAY: degree chemist in Italy at Università di Firenze. BY NIGHT: multihobbist and minecrafter. FOR FUN: Minecraft, webmaster, PHP, Wordpress, HTML "When you change the way you see the world, you'll change the world you see" -Satya Nadella

Updated on August 01, 2022

Comments

  • fratili
    fratili over 1 year

    I've to do a (simple) histogram using PGFPlots, but I don't know why it doesn't shows all the series on the X-Axis.

    Minimal working example:

    \begin{tikzpicture}
      \begin{axis}[
          x tick label style={
            /pgf/number format/1000 sep=},
          ylabel={\small{Efficienza (\milli\gram\per\gram)}},
          width = 15.4cm, height = 10.7cm,
          legend style={
            font=\scriptsize,
            cells={anchor=west}
          },
          legend pos=north west,
          symbolic x coords={Spugna 1,Spugna 2,Spugna 3,Spugna 4,Spugna 5,Spugna 6},
          xtick=data,
          tick label style={font=\scriptsize},
          ybar interval=0.7,
        ]
        \addplot coordinates {(Spugna 1,0.56) (Spugna 2,2.84) (Spugna 3,3.57) (Spugna 4,5.45) (Spugna 5,1.12) (Spugna 6,4.29)};
        \addplot coordinates {(Spugna 1,0.66) (Spugna 2,3.10) (Spugna 3,3.77) (Spugna 4,5.61) (Spugna 5,1.27) (Spugna 6,4.27)};
        \legend{Reali,Fitting}
      \end{axis}
     \end{tikzpicture}
    

    There are clearly 6 series, but PGFPlots only show 5 of them:

    Wrong plot with only 5 series

    However, if I put ybar instead of ybar interval=0.7, all the series are typesetted (but in an ugly plot):

    Correct plot but ugly bars

    How can I fix that?

    • Stefan Pinnow
      Stefan Pinnow about 6 years
      What do you think is ugly on the second image? I guess you mean the white space between the bars. To reduce that you can either change the bar width to a value that fits your needs or you can change the width of the axis environment.
    • fratili
      fratili about 6 years
      @stefan-pinnow I will try it, thank you!
    • Torbjørn T.
      Torbjørn T. about 6 years
      It doesn't show all the bars in the first one, because that is what ybar interval is designed to do, read the description of it in the pgfplots manual.