Python 使用直方图的Matplotlib/Pandas错误
我在制作熊猫系列对象的直方图时遇到问题,我不明白为什么它不起作用。该代码以前运行良好,但现在不行 下面是我的一段代码(具体地说,我正试图制作一个熊猫系列对象的直方图): 其中输出结果:Python 使用直方图的Matplotlib/Pandas错误,python,matplotlib,pandas,histogram,Python,Matplotlib,Pandas,Histogram,我在制作熊猫系列对象的直方图时遇到问题,我不明白为什么它不起作用。该代码以前运行良好,但现在不行 下面是我的一段代码(具体地说,我正试图制作一个熊猫系列对象的直方图): 其中输出结果: 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].g
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.
AttributeError回溯(最近一次调用)
在()
1图,轴=plt.子批次(1,7,figsize=(30,4))
2.
---->3轴[1]。历史(dfj2_MARKET1['VSPD2_perc'],alpha=0.9,color='blue')
4轴[1]。栅格(真)
5轴[1]。设置_xlabel('花费的时间[%]”)
hist中的C:\Python27\lib\site packages\matplotlib\axes.pyc(self、x、bin、range、normed、weights、累计、底部、histtype、align、orientation、rwidth、log、color、label、stacked、**kwargs)
8322#这将自动覆盖垃圾箱,
8323#因此每个直方图使用相同的箱子
->8324米,箱子=np。柱状图(x[i],箱子,重量=w[i],**hist_kwargs)
8325 m=m.astype(float)#如果是int,则稍后会导致问题
8326如果mlast为无:
直方图中的C:\Python27\lib\site packages\numpy\lib\function\u base.pyc(a、箱子、范围、标准化、权重、密度)
158如果(mn>mx):
159提高属性错误(
-->160“最大值必须大于范围参数中的最小值”。)
161
162如果无法安装(箱子):
AttributeError:范围参数中的最大值必须大于最小值。
当序列中有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在范围参数中必须大于min。
一个选项是例如在打印前删除NaN。这将有助于:
ax.hist(s.dropna(), alpha=0.9, color='blue')
另一个选项是对序列使用pandashist
方法,并为ax
关键字提供axs[0]
:
dfj2_MARKET1['VSPD1_perc'].hist(ax=axes[0], alpha=0.9, color='blue')
如上所述,
NaN
值正确地导致了该错误。只需使用:
df = df['column_name'].apply(pd.to_numeric)
如果该值不是数字,则应用:
df = df['column_name'].replace(np.nan, your_value)
嗯,这对我有用。你能显示你的数据帧吗?嗯,奇怪的是,当我这样做的时候,我实际上可以生成一个直方图:s=dfj2_MARKET1['VSPD1_perc']s.hist()是的,但是你使用的是pandas
hist
函数,而不是matplotlibs。这将按预期处理eg NAN。查看我的更新。
df = df['column_name'].replace(np.nan, your_value)