Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Histogram shows unlimited bins despite bin specification in matplotlib

I have a error data and when I tried to make a histogram of the data the intervals or the bin sizes were showing large as shown in the below image

enter image description here

Below is the code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import matplotlib.pyplot as plt
    
plt.figure()
plt.hist(error)
plt.title('histogram of error')
plt.xlabel('error')
plt.show()

When I tried to explicitly mention the bins as we usually do, like in the below code I get the hist plot as shown below

plt.figure()
plt.hist(error, bins=[-4,-3,-2,-1, 0,1, 2,3, 4,])
#plt.hist(error, bins = 6)
plt.title('histogram of error')
plt.xlabel('error')
plt.show()

enter image description here

I wish to make the hist look nice, something like below (an example from google) with bins clearly defined.

enter image description here

i Tried with seaborn displot and it gave a nice plot as shown below.

enter image description here

import seaborn as sns

sns.displot(error, bins=[-4,-3,-2,-1, 0,1, 2,3, 4,])
plt.title('histogram of error')
plt.xlabel('error')
plt.show()

Why is that the matplotlib not able to make this plot? Did I miss anything or do I need to set something in order to make the usual histogram plot? Please highlight

>Solution :

The matplotlib documentation for plt.hist() explains that the first parameter can either by a 1D array or a sequence of 1D arrays. The latter case is used if you pass in a 2D array and will result in plotting a separate bar with cycling colors for each of the rows.

This is what we see in your example: The X-axis ticks still correspond to the bin-edges that were passed in – but for each bin there are many bars. So, I’m assuming you passed in a multidimensional array.

To fix this, simply flatten your data before passing it to matplotlib, e.g. plt.hist(np.ravel(error), bins=bins).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading