【发布时间】:2018-04-24 08:17:35
【问题描述】:
我正在尝试使用可变数据绘制饼图。
下面是我的代码。 Postive_percentage 和 topic 都是 list,其数据来自代码的其他部分并通过各种操作。我已经从输出中发布了 Postive_percentage 和 topic 的值。
我是 python 新手,这就是为什么我无法理解这个问题。 Postive_percentage、topic 和 colors 有 7 个变量,但它仍然给我这个错误。
#topic and Postive_percentage copied from output and was not manually inserted by me.
topic = ['VirginAmerica', 'UnitedAirline', 'SouthWestAirline', 'USAirline', 'AmericanAirline', 'SpiritAirline', 'DeltaAirline']
Postive_percentage = [3.917525773195876, 10.0, 6.666666666666667, 10.0, 3.0, 5.0, 5.0]
sizes = Postive_percentage
print(sizes)
labels = str(topic)
# makeitastring = ''.join(map(str, labels))
print(labels)
colors = ['yellowgreen', 'lightgreen', 'darkgreen', 'gold', 'red', 'lightsalmon', 'darkred']
plt.pie(sizes, explode=None, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=90) #line 240
#plt.pie(sizes, labels, colors)
plt.axis('equal')
plt.legend()
plt.show()
这是正在生成的错误。
Traceback (most recent call last):
File "C:/Users/Aryana/Desktop/Sentimental-Analysis-on-Twitter-master/senti_twitter.py", line 248, in <module>
main()
File "C:/Users/Aryana/Desktop/Sentimental-Analysis-on-Twitter-master/senti_twitter.py", line 240, in main
plt.pie(sizes, explode=None, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=90)
File "C:\Users\Aryana\Desktop\Sentimental-Analysis-on-Twitter-master\venv\lib\site-packages\matplotlib\pyplot.py", line 3338, in pie
frame=frame, rotatelabels=rotatelabels, data=data)
File "C:\Users\Aryana\Desktop\Sentimental-Analysis-on-Twitter-master\venv\lib\site-packages\matplotlib\__init__.py", line 1855, in inner
return func(ax, *args, **kwargs)
File "C:\Users\Aryana\Desktop\Sentimental-Analysis-on-Twitter-master\venv\lib\site-packages\matplotlib\axes\_axes.py", line 2858, in pie
raise ValueError("'label' must be of length 'x'")
ValueError: 'label' must be of length 'x'
【问题讨论】:
标签: python matplotlib