TransWikia.com

Histogram: increasing space between bars

TeX - LaTeX Asked on July 10, 2021

How can I maintain the same width of the bars, and at the same time increasing the space that separates them?

documentclass{article}
usepackage{pgfplots}
usepackage{tikz}

begin{document}

    begin{figure}[p]
    centering  
    begin{tikzpicture}[font=footnotesize]
        begin{axis} [ylabel={Results},
            width=textwidth,
            height=8cm,
            enlarge x limits=0.03,
            ybar,ymin=0,ymax=1750,xtick=data,
            ymajorgrids=true,xtick pos=left,
            ytick={0, 500, 1000, 1500},
            yticklabels={$0$,$500$,$1000$,$1500$},
            x tick label style=%
            {rotate=90,anchor=east},
            xticklabel interval boundaries,
            symbolic x coords={$1990$,$1991$,$1992$,$1993$,$1994$,$1995$,$1996$,$1997$,$1998$,$1999$,$2000$,$2001$,$2002$,$2003$,$2004$,$2005$,$2006$,$2007$,$2008$,$2009$,$2010$,$2011$,$2012$,$2013$,$2014$,$2015$,$2016$,$2017$,$2018$,$2019$,$2020$},
            ]
            addplot
            [ybar interval, fill=cyan,
            draw=black] coordinates
            {($1990$, 31) ($1991$, 23) ($1992$, 36) ($1993$, 34) ($1994$, 46) ($1995$, 72) ($1996$, 127) ($1997$, 105) ($1998$, 137) ($1999$, 147) ($2000$, 135) ($2001$, 162) ($2002$, 211) ($2003$, 209) ($2004$, 301) ($2005$, 349) ($2006$, 452) ($2007$, 474) ($2008$, 635) ($2009$, 943) ($2010$, 998) ($2011$, 1047) ($2012$, 1069) ($2013$, 1173) ($2014$, 1236) ($2015$, 1106) ($2016$, 1236) ($2017$, 1329) ($2018$, 1447) ($2019$, 1697) ($2020$, 1291)
            };          
        end{axis}
    end{tikzpicture}
end{figure}

end{document}

I tried to add the bar width=9pt, and to eliminate ybar interval, but the space between bars seems to be different for some pairs of bars.

One Answer

Like this?

enter image description here

documentclass[border=5pt]{standalone}
usepackage{pgfplots}
pgfplotsset{compat=1.17}

begin{document}
    begin{tikzpicture}
begin{axis}[x=4mm, height=8cm,
    ybar,
    bar width=3mm,
    enlarge x limits=0.02,          
    enlarge y limits={.1, upper},   
ymajorgrids=true,
    minor y tick num=4,   
    xtick=data,
    ytick={0, 500, 1000, 1500},
    yticklabels={0,500,1000,1500},
    x tick label style={rotate=90,anchor=east},
symbolic x coords={ 1990,1991,1992,1993,1994,
                    1995,1996,1997,1998,1999,
                    2000,2001,2002,2003,2004,
                    2005,2006,2007,2008,2009,
                    2010,2011,2012,2013,2014,
                    2015,2016,2017,2018,2019,
                    2020},
                    ]
addplot coordinates   {(1990, 31)   (1991, 23)   (1992, 36)   (1993, 34)   (1994, 46)
                        (1995, 72)   (1996, 127)  (1997, 105)  (1998, 137)  (1999, 147)
                        (2000, 135)  (2001, 162)  (2002, 211)  (2003, 209)  (2004, 301)
                        (2005, 349)  (2006, 452)  (2007, 474)  (2008, 635)  (2009, 943)
                        (2010, 998)  (2011, 1047) (2012, 1069) (2013, 1173) (2014, 1236)
                        (2015, 1106) (2016, 1236) (2017, 1329) (2018, 1447) (2019, 1697)
                        (2020, 1291)
                       };
    end{axis}
end{tikzpicture}
end{document}

Miain difference between proposed solution and your MWE:

  • defined distance between bars (x=5mm)
  • defined bar's width (bar width=3mm)
  • removed ybar interval, from addplot

Correct answer by Zarko on July 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP