Reputation: 50540
I am attempting to show a histogram. The array below is the count for each individual bin of the histogram.
binVals = [0,5531608,6475325,1311915,223000,609638,291151,449434,1398731,2516755,3035532,2976924,2695079,1822865,1347155,304911,3562,157,5,0,0,0,0,0,0,0,0,0]
How would I go about doing this? I attempted with this code
import matplotlib.pyplot as plt
binVals = [0,5531608,6475325,1311915,223000,609638,291151,449434,1398731,2516755,3035532,2976924,2695079,1822865,1347155,304911,3562,157,5,0,0,0,0,0,0,0,0,0]
plt.hist(binVals, bins=len(binVals), color='r', alpha=0.5, label='Values')
plt.title("Demo Histogram")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.legend()
plt.show()
This returns an image similar to this
This is off though, because bins 0 and 19 through 27 have a count of zero in binVals
. The zero values in the image above are not in the locations I'd expect.
I am expecting something similar to this
How can I modify my code to get this type of result?
As a bonus question, at the bottom of the expected graph there are nicely labeled Bins. Can this be done with matlibplot?
Upvotes: 4
Views: 2801
Reputation: 2724
I think you're plotting the wrong thing. Matplotlib will calculate the histogram itself. It now plots that you have 14 bins with a value between 0 and 250,000, 3 with a value between 250,000 and 500,000 etc. If you calculate the histogram yourself, use a bar() plot, or let Matplotlib calculate the histogram for you.
For the tick labels, use set_xticklabels(["Under 600", "600-700",...], rotation = "vertical")
(I made the comments an answer, so it can be marked as answered)
Upvotes: 2