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

Anna Reed picture Anna Reed · Jan 23, 2015 · Viewed 19.9k times · Source

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!

Answer

mrcl picture mrcl · Jan 23, 2015

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.