ValueError:需要多于1个值才能解压缩如何修复它

时间:2015-01-14 14:21:43

标签: python numpy matplotlib plot zip

我正在尝试使用python(2.7)进行绘图,但是我得到了这个ValueError:需要多于1个值才能解压缩。

我的代码如下所示:

x, y = zip(*list_with_data) 
xlocs = np.arange(len(x))

fig = plt.figure()
ax = fig.gca()
ax.bar(xlocs + 0.6, y)
ax.set_xticks(xlocs + 1)
ax.set_xticklabels(x)
ax.set_xlim(0.0, xlocs.max() + 2)

plt.show()

我的

list_with_data = Counter(numbers(text))

我想要x标签,文本包含的数字:

  

... 1,2,3,4,5,6-

我希望我的y标签代表:它们包含在文本中的次数。

示例:

x: 1, y: 150
x: 2, y: 20

如何从我的数据集中绘制这个?

1 个答案:

答案 0 :(得分:2)

你只是压缩dict键的键,所以你只有一个值因此解包错误:

In [12]: list_with_data = Counter("1 2 3 2 3 4 5 6")

In [13]: zip(*list_with_data)
Out[13]: [(' ', '1', '3', '2', '5', '4', '6')] 

如果要解压缩两个值,则需要两个值:

In [14]: x, y = zip(*list_with_data.items())  
In [15]: x
Out[15]: (' ', '1', '3', '2', '5', '4', '6')

In [16]: y
Out[16]: (7, 1, 2, 2, 1, 1, 1