jon*_*nas 68 python matplotlib histogram pandas
我有问题从熊猫系列对象制作直方图,我不明白为什么它不起作用.代码之前运行良好,但现在却没有.
这是我的一些代码(具体来说,我正在尝试制作直方图的pandas系列对象):
type(dfj2_MARKET1['VSPD2_perc'])
Run Code Online (Sandbox Code Playgroud)
输出结果:
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')
Run Code Online (Sandbox Code Playgroud)
错误信息:
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.
Run Code Online (Sandbox Code Playgroud)
jor*_*ris 118
当您在系列中具有NaN值时,会发生此错误.可能是这样吗?
histmatplotlib 的功能不能很好地处理这些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')
Run Code Online (Sandbox Code Playgroud)
产生相同的错误AttributeError: max must be larger than min in range parameter.一个选项是例如在绘图之前移除NaN.这将有效:
ax.hist(s.dropna(), alpha=0.9, color='blue')
Run Code Online (Sandbox Code Playgroud)
另一种选择是使用大熊猫hist在你的系列,并提供方法axes[0]的ax关键字:
dfj2_MARKET1['VSPD1_perc'].hist(ax=axes[0], alpha=0.9, color='blue')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
65406 次 |
| 最近记录: |