1

I'm trying to plot the following dataset, but, for some reason I can't get de ticks right. It seems they are always offplace.

y = 
[0.72727273,0.5,0.33333333,0.4375,0.4375,0.14285714,0.36363636,1.44444444,0.53333333,0.26666667,0.625,0.71428571,0.4,1.21428571,0.22222222,0.09090909,0.35714286,0.07142857,0.4,1.46666667,0,1.44444444,0.42857143,0.13333333,0.5,0.25,0,0.44444444,0.33333333,0.46153846,0,0.57142857,0.8,0.35714286,0.16666667,0.45454545,0,0.95,0,0,0,0.15384615,0.5,1,3.11111111,3,2.7,0.8,1.66666667,0.69230769,0,1.66666667,1,3,6.5,3.75,0,6,0,0]
month_and_year_name =
['Jun 2014', 'Jul 2014', 'Aug 2014', 'Sep 2014', 'Oct 2014', 'Nov 2014', 'Dec 2014', 'Jan 2015', 'Feb 2015', 'Mar 2015', 'Apr 2015', 'May 2015', 'Jun 2015', 'Jul 2015', 'Aug 2015', 'Sep 2015', 'Oct 2015', 'Nov 2015', 'Dec 2015', 'Jan 2016', 'Feb 2016', 'Mar 2016', 'Apr 2016', 'May 2016', 'Jun 2016', 'Jul 2016', 'Aug 2016', 'Sep 2016', 'Oct 2016', 'Nov 2016', 'Dec 2016', 'Jan 2017', 'Mar 2017', 'Apr 2017', 'May 2017', 'Jun 2017', 'Jul 2017', 'Aug 2017', 'Sep 2017', 'Aug 2018', 'Sep 2018', 'Oct 2018', 'Nov 2018', 'Dec 2018', 'Feb 2019', 'Mar 2019', 'Apr 2019', 'May 2019', 'Jun 2019', 'Jul 2019', 'Aug 2019', 'Sep 2019', 'Oct 2019', 'Dec 2019', 'Jan 2020', 'Feb 2020', 'Mar 2020', 'Sep 2020', 'Oct 2020', 'Nov 2020']

I'm using the following piece of code to do the plot.

    fig, ax = plt.subplots(figsize = (10,4))  

    ax.bar(month_and_year_name,y, width =1 , align = 'center', linewidth = 0.5, edgecolor = 'yellow' )
    ax.tick_params(axis='x', rotation=45)

    plt.tight_layout()

    plt.savefig("files/sparks_in_all_logs.png", dpi = 300)

this is what I've been getting

As you can see, not only the ticks are completely missplace they are also out of the png i've saved.

What I'm doing wrong here? what should I do to correct this plot? And, for the future I'll add more and more information in both parameters, y and month_year. Is there a way to properly set the figure size with that in mind? (each month will have a new pair of y and month_year)

enter image description here In previous months I managed to get the plot right, but the same code has not the effect I want.

hcp
  • 319
  • 2
  • 16

1 Answers1

1

First convert month_and_year_name into proper datetime objects and plot with those dates:

from datetime import datetime
x = [datetime.strptime(name, '%b %Y') for name in month_and_year_name]

import matplotlib.dates as mdates
fig, ax = plt.subplots(figsize=(15, 4))  
h = ax.bar(x, y, width=5, align='center', linewidth=0.5, edgecolor='yellow')

Increase the tick frequency to have 1 tick per bar:

start, end = ax.get_xlim()
xticks = np.linspace(start, end, len(x))
ax.set_xticks(xticks)
ax.set_xticklabels(xticks)

Fix the tick/bar alignment:

xticks_pos = [0.65*bar.get_width() + bar.get_xy()[0] for bar in h]
plt.xticks(xticks_pos, x,  ha='center', rotation=90)

And set the axis date format with set_major_formatter():

fmt = mdates.DateFormatter('%b %Y') # '%b %Y' is 'Mon YYYY'
ax.xaxis.set_major_formatter(fmt)

bar chart output

tdy
  • 36,675
  • 19
  • 86
  • 83
  • 1
    That looks great! but I need each bin to have its year and month... – hcp Mar 26 '21 at 05:39
  • 1
    Ok, you can [increase the tick frequency manually](https://stackoverflow.com/a/66810042/13138364). I'll update the code here as well. – tdy Mar 26 '21 at 05:42
  • 1
    Ok, see if the updated code is what you're looking for. There's 1 tick per x value, and the [xtick positioning is fixed](https://stackoverflow.com/questions/21477465/matplotlib-python-barplot-position-of-xtick-labels-have-irregular-spaces-betwee) – tdy Mar 26 '21 at 05:54
  • 1
    Thats perfect! I've made some adaptations, my problem is a little more difficult than that, but it just works fine. thank you! – hcp Mar 26 '21 at 05:56