gpt4 book ai didi

python - matplotlib 历史() : weights should have the same shape as x while shape is the same

转载 作者:行者123 更新时间:2023-11-28 16:20:05 34 4
gpt4 key购买 nike

我正在尝试绘制 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)

堆栈跟踪:

(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

最佳答案

您的数据集中有空值。

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

关于python - matplotlib 历史() : weights should have the same shape as x while shape is the same,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41011799/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com