Python 使用直方图的 Matplotlib/Pandas 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20656663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:58:01  来源:igfitidea点击:

Matplotlib/Pandas error using histogram

pythonmatplotlibpandashistogram

提问by jonas

I have a problem making histograms from pandas series objects and I can't understand why it does not work. The code has worked fine before but now it does not.

我在从 pandas 系列对象制作直方图时遇到问题,我不明白为什么它不起作用。该代码以前运行良好,但现在不行了。

Here is a bit of my code (specifically, a pandas series object I'm trying to make a histogram of):

这是我的一些代码(特别是我正在尝试制作直方图的熊猫系列对象):

type(dfj2_MARKET1['VSPD2_perc'])

which outputs the result: pandas.core.series.Series

输出结果: pandas.core.series.Series

Here's my plotting code:

这是我的绘图代码:

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

Error message:

错误信息:

    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.

采纳答案by joris

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

当系列中有 NaN 值时,会发生此错误。可能是这样吗?

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

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

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:

产生相同的错误AttributeError: max must be larger than min in range parameter.一种选择是例如在绘图之前删除 NaN。这将起作用:

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

Another option is to use pandas histmethod on your series and providing the axes[0]to the axkeyword:

另一种选择是使用大熊猫hist在你的一系列方法和提供axes[0]ax关键字:

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

回答by brainhack

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

错误是由NaN上述值引起的。只需使用:

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)