Python ValueError:权重的形状应与x相同

Python ValueError:权重的形状应与x相同,python,matplotlib,Python,Matplotlib,在我的代码中 from __future__ import division import sys import math import os import matplotlib.pyplot as plt def get_pt_t(event): pt_list=[] pdg_list=[] for particle in event: p = FourMomentum()

在我的代码中

    from __future__ import division
    import sys
    import math
    import os
    import matplotlib.pyplot as plt

    def get_pt_t(event):
        pt_list=[]
        pdg_list=[]
        for particle in event:
            p = FourMomentum()
            if particle.pid in [-11,11,13,-13] and particle.status==1 :
                p += particle
                pt_list.append(p.pt)
                break
        return pt_list
values_x= []

PT_t=[]
PT_t.append(pt_list)

现在,pt_list为上面的每个“粒子”保存一个类似于[x,y]的列表,现在,在尝试绘制时

nbins=300
# prepare the plots
gs1 = gridspec.GridSpec(2, 1, height_ratios=[5,1.2])
gs1.update(wspace=0, hspace=0) # set the spacing between axes.

# plot the Three main curve for the pt
ax = plt.subplot(gs1[0])
wgts_list = []
wgts_list.append( [1,2])
n3, nbins, _ = ax.hist(PT_t, bins=nbins, histtype='step', linewidth=0,weights=wgts_list)
但我明白了

n3, nbins, _ = ax.hist(PT_t, bins=nbins, histtype='step', linewidth=0,weights=wgts_list)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/axes.py", line 8290, in hist
    raise ValueError('weights should have the same shape as x')
ValueError: weights should have the same shape as x

我认为问题在于我想画一个列表(PT_t),但无法在weights参数中指定权重列表。

您的
PT_t
列表有多长(由
len(PT_t)返回的内容)
?您的
wgts\u列表
是两个元素的列表。您收到的错误告诉您它们应该是相同的长度。您的
PT\u t
列表有多长(由
len(PT\u t)返回的内容)
?您的
wgts\u列表
是两个元素的列表。您收到的错误告诉您它们应该是相同的长度。