Matplotlib/Pandas error using histogram Matplotlib/Pandas error using histogram python python

Matplotlib/Pandas error using histogram


This error occurs among other things when you have NaN values in the Series. Could that be the case?

These NaN's are not handled well by the hist function of matplotlib. For example:

s = pd.Series([1,2,3,2,2,3,5,2,3,2,np.nan])fig, ax = plt.subplots()ax.hist(s, alpha=0.9, color='blue')

produces the same error AttributeError: max must be larger than min in range parameter. One option is eg to remove the NaN's before plotting. This will work:

ax.hist(s.dropna(), alpha=0.9, color='blue')

Another option is to use pandas hist method on your series and providing the axes[0] to the ax keyword:

dfj2_MARKET1['VSPD1_perc'].hist(ax=axes[0], alpha=0.9, color='blue')


The error is rightly due to NaN values as explained above. Just use:

df = df['column_name'].apply(pd.to_numeric)

if the value is not numeric and then apply:

df = df['column_name'].replace(np.nan, your_value)