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

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).

Leave a Reply