TransWikia.com

Use year as label for monthly data histogram

TeX - LaTeX Asked by darkspirit510 on August 5, 2021

I have data in the format year,month and amount. I want to illustrate this data using a histogram. Since the data covers multiple years, printing the months just stacks everything. I try to use the years as labels and a year dividing grid instead.

Current solution:

begin{tikzpicture}
begin{axis}[
    ybar stacked,
    width=1.1textwidth,
    height=axisdefaultheight,
    bar width=7pt,
    grid=both,
    enlarge x limits=false,
    enlarge y limits=upper,
    xtick=data,     
    xticklabels={2013,2014,2015,2016,2017},
    ymin=0
]
    addplot[ybar,fill=gray] coordinates {
        (1,7)(2,5)(3,5)(4,7)(5,2)(6,4)(7,12)(8,0)(9,2)(10,6)(11,0)(12,0)(13,0)(14,2)(15,5)(16,0)(17,2)(18,6)(19,1)(20,0)(21,8)(22,10)(23,2)(24,21)(25,9)(26,4)(27,6)(28,32)(29,10)(30,10)(31,24)(32,44)(33,51)(34,43)(35,29)(36,39)(37,27)(38,12)(39,0)(40,9)(41,21)(42,7)(43,32)(44,1)(45,14)(46,22)(47,1)(48,23)(49,3)(50,13)(51,15)(52,23)(53,5)(54,16)(55,9)(56,28)(57,2)(58,26)(59,10)(60,0)
    };
end{axis}
end{tikzpicture}

Which produces:

messed up grid labels and grid

I tried some hacky solution using lots of empty commas and hand-drawn lines to somehow get what I want.

target

But as you can see, centering labels is hard (and will probably fail for other ranges). So do you have an idea how to:

  • create a vertical grid by years (each 12th entry)
  • use centered label for the year
  • bonus: remove the other (small) grid dashes for the x-axis on the top and bottom (optional!)

One Answer

One option might be to add a tick every 6, 18, etc. month, with

xtick={6,18,...,60},

and then calculate a new value to use for the labels with

xticklabel={pgfmathparse{(tick-6)/12+2013}pgfmathprintnumber{pgfmathresult}},

Note xticklabel instead of xticklabels. This lets you define a pattern for the ticklabels, based on the data value for the ticks, which is given by tick. Subtracting six and dividing by twelve gives you years from start, add 2013 to get the correct year.

In addition, add

xticklabel style={/pgf/number format/1000 sep=},

to remove the comma used to separate the thousands by default.

Two other small changes:

  • bar width=1 makes each bar one axis unit wide (i.e. one month). This requires you to enable compat=1.7 or a higher version number. I added 1.18 in the example below, because that is the version I have installed, if you have some older version of the package you have to modify that.

  • enlarge x limits={abs=0.5} adds 0.5 at each end of the axis, so that the bars at the end are inside the axis.

output of ode

documentclass[border=5mm]{standalone}
usepackage{pgfplots}
pgfplotsset{compat=1.18}
begin{document}
    
begin{tikzpicture}
begin{axis}[
    ybar stacked,
    width=1.1textwidth,
    height=axisdefaultheight,
    bar width=1, % with compat=1.7 or newer, a number without a unit is interpreted as axis units
    grid=both,
    enlarge y limits=upper,
    xtick={6,18,...,60},
    xticklabel={pgfmathparse{(tick-6)/12+2013}pgfmathprintnumber{pgfmathresult}},
    xticklabel style={/pgf/number format/1000 sep=},
    ymin=0,
    enlarge x limits={abs=0.5}
]
    addplot[ybar,fill=gray] coordinates {
        (1,7)(2,5)(3,5)(4,7)(5,2)(6,4)(7,12)(8,0)(9,2)(10,6)(11,0)(12,0)(13,0)(14,2)(15,5)(16,0)(17,2)(18,6)(19,1)(20,0)(21,8)(22,10)(23,2)(24,21)(25,9)(26,4)(27,6)(28,32)(29,10)(30,10)(31,24)(32,44)(33,51)(34,43)(35,29)(36,39)(37,27)(38,12)(39,0)(40,9)(41,21)(42,7)(43,32)(44,1)(45,14)(46,22)(47,1)(48,23)(49,3)(50,13)(51,15)(52,23)(53,5)(54,16)(55,9)(56,28)(57,2)(58,26)(59,10)(60,0)
    };
end{axis}
end{tikzpicture}
    
end{document}

Answered by Torbjørn T. on August 5, 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