4

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!

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much. That did it. I thought that I had tried that before, but apparently I had not. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.