matplotlib hist():权重应与x具有相同的形状,而形状相同

时间:2016-12-07 07:38:06

标签: python pandas matplotlib

我正在尝试在熊猫系列('df_plot')中绘制一列的直方图。由于我希望y轴是一个百分比(而不是计数),我使用权重选项来实现这一点。正如您在下面的堆栈跟踪中找到的那样,权重数组和数据系列具有相同的形状。为什么我仍然得到错误告诉我w和x的形状不一样?

代码:

w = 100*(np.zeros_like(df_plot[var]) + 1. / len(df_plot[var]))
print w.shape
print df_plot[var].shape
df_plot[var].hist(bins=100,  cumulative=True, weights=w)

Stacktrace:

(9066,)
(9066,)


Traceback (most recent call last):

  File "<ipython-input-59-5612307b159e>", line 4, in <module>
    df_plot[var].hist(bins=100,  cumulative=True, weights=w)

  File "C:\Anaconda\lib\site-packages\pandas\tools\plotting.py", line 2819, in hist_series
    ax.hist(values, bins=bins, **kwds)

  File "C:\Anaconda\lib\site-packages\matplotlib\axes\_axes.py", line 5649, in hist
    'weights should have the same shape as x')

ValueError: weights should have the same shape as x

1 个答案:

答案 0 :(得分:3)

您的数据集中有空值。

s = df_plot[var].dropna()
w = 100*(np.zeros_like(s) + 1. / len(s))
s.hist(bins=100,  cumulative=True, weights=w)