Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python pyplot histogram: Adjusting bin width, Not number of bins

I have been able to make myself a pretty little histogram that looks like this:

One day it will be pretty

I was able to produce the image with the following code:

    import numpy as np
    import matplotlib.pyplot as plt

    plt.figure()  
    plt.axis([0, 6000, 0, 45000])  

    data['column'][data.value == 0].hist(bins=200, label='A') 
    data['column2'][data.value == 1].hist(bins=200, label='B')

    plt.title('A Histogram')  
    plt.xlabel('x-axis')  
    plt.ylabel('y-axis')  
    plt.legend()  

    return plt

Which is all fine and good, but the bins are not equal lengths. The only way I have been able to get the bins at equal lengths is to do something like this:

 bins=[0,100,200,300,400,.......)

Which is not pretty at all.

I have googled a bit and looked around here. The most popular answer to a similar question is this guy which suggests a seemingly excellent answer that I can not get to work for the life of me.

Thanks for your help!

like image 558
Anna Reed Avatar asked Mar 18 '23 07:03

Anna Reed


1 Answers

I am a bit confused with your data structure and how you are calling the function hist. However, I suppose you are using matplotib, so you need to define the same binning range for the hist function. It works better if you pass an array with the bin boundaries, instead of the number of bins you want.

import numpy as np
import matplotlib.pyplot as plt

plt.figure()  
plt.axis([0, 6000, 0, 45000])  

# From your example I am assuming that the maximum value is 6000
binBoundaries = np.linspace(0,6000,201)

data['column'][data.value == 0].hist(bins=binBoundaries, label='A') 
data['column2'][data.value == 1].hist(bins=binBoundaries, label='B')

plt.title('A Histogram')  
plt.xlabel('x-axis')  
plt.ylabel('y-axis')  
plt.legend()

This should work for you.

Let me know if it helps.

like image 73
mrcl Avatar answered Mar 20 '23 04:03

mrcl