Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib Histogram Alignment

def vs_time(data): #MUBASHWER, histogram
    clf()
    hist(data.keys(), bins = 12, weights = data.values())
    xticks(arange(0,24,2))
    xlabel('Time')
    ylabel('Number of fatalities')
    title('Number of Fatalities vs Time')
    webshow('vs_time.png')

histogram:enter image description here

I want the hours in x-axis to be on the left edge of each bar. But they are neither at the centre, nor at any edge. Please help. The problem could be solved if the histogram could be shirted without altering the axis.

like image 712
user3623449 Avatar asked Mar 10 '26 23:03

user3623449


1 Answers

This is clearly because the bin edges of your resulting histogram are not just even numbers 0,2,4....22.

To get the desired result, simply make use of the bins= argument and, i.e., :

plt.hist(YOUR_DATA,bins=arange(0,24,2)) #for bin edge 0,2,4...,22, as in your figure.
like image 198
CT Zhu Avatar answered Mar 12 '26 12:03

CT Zhu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!