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

mar*_*ram 4 python matplotlib pandas

我正在尝试绘制熊猫系列('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)
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪:

(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
Run Code Online (Sandbox Code Playgroud)

piR*_*red 5

你的数据集中有空值。

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