我在从 pandas 系列对象制作直方图时遇到问题,我不明白为什么它不起作用。该代码之前运行良好,但现在不行。
这是我的一些代码(特别是我正在尝试制作直方图的 Pandas 系列对象):
type(dfj2_MARKET1['VSPD2_perc'])
输出结果:
pandas.core.series.Series
这是我的绘图代码:
fig, axes = plt.subplots(1, 7, figsize=(30,4))
axes[0].hist(dfj2_MARKET1['VSPD1_perc'],alpha=0.9, color='blue')
axes[0].grid(True)
axes[0].set_title(MARKET1 + ' 5-40 km / h')
错误信息:
AttributeError Traceback (most recent call last)
<ipython-input-75-3810c361db30> in <module>()
1 fig, axes = plt.subplots(1, 7, figsize=(30,4))
2
----> 3 axes[1].hist(dfj2_MARKET1['VSPD2_perc'],alpha=0.9, color='blue')
4 axes[1].grid(True)
5 axes[1].set_xlabel('Time spent [%]')
C:\Python27\lib\site-packages\matplotlib\axes.pyc in hist(self, x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, **kwargs)
8322 # this will automatically overwrite bins,
8323 # so that each histogram uses the same bins
-> 8324 m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs)
8325 m = m.astype(float) # causes problems later if it's an int
8326 if mlast is None:
C:\Python27\lib\site-packages\numpy\lib\function_base.pyc in histogram(a, bins, range, normed, weights, density)
158 if (mn > mx):
159 raise AttributeError(
--> 160 'max must be larger than min in range parameter.')
161
162 if not iterable(bins):
AttributeError: max must be larger than min in range parameter.
最佳答案
当您在 Series 中有 NaN 值时,会发生此错误。会这样吗?
matplotlib 的 hist
函数不能很好地处理这些 NaN。例如:
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')
产生相同的错误 AttributeError: max must be greater than min in range parameter.
一种选择是例如在绘图前删除 NaN。这将起作用:
ax.hist(s.dropna(), alpha=0.9, color='blue')
另一种选择是在您的系列中使用 pandas hist
方法并将 axes[0]
提供给 ax
关键字:
dfj2_MARKET1['VSPD1_perc'].hist(ax=axes[0], alpha=0.9, color='blue')
关于python - 使用直方图的 Matplotlib/Pandas 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20656663/