问题描述
我正在尝试使用可变数据绘制饼图.
I am trying to plot pie chart using varible data.
下面是我的代码. Postive_percentage 和 topic 都是列表,其数据来自代码的其他部分并通过各种操作获得.我从输出中发布了 Postive_percentage 和 topic 的值.
Below is my code. both Postive_percentage and topic is the list whose data comes from other part of code and through various operations. I have posted the value of Postive_percentage and topic from the output.
我是python的新手,所以我无法理解该问题. Postive_percentage , topic 和颜色有7个变量,但仍然给我这个错误.
I am new to python thats why i am not able to understand the issue. Postive_percentage, topic and colors have 7 variables and still it is giving me this error.
#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'
推荐答案
在分配给标签时移除 str(..)
.如果要复制而不是仅参考,请用 list(..)
.
Remove the str(..)
when assigning to labels.If you want a copy and not just the reference, replace it with list(..)
.
import matplotlib.pyplot as plt
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 = list(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()
说明:
在列表上使用 str()
时,它将整个列表作为字符串,包括方括号.因此,在控制台打印时,您可能会认为这是一个列表,而它只是一个长字符串..
Explanation:
When using str()
on a list, it makes the whole list as string, including the square-brackets. Therefore, when printing on console, you might have thought that this is a list, while it is just a long string..
这篇关于ValueError:使用matplotlib,“标签"的长度必须为"x"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!