Matplotlib/Pandas error using histogram

落爺英雄遲暮 提交于 2019-11-28 17:22:12

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')
brainhack

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!