【问题标题】:ValueError: 'label' must be of length 'x' using matplotlibValueError:“标签”的长度必须为“x”,使用 matplotlib
【发布时间】:2018-04-24 08:17:35
【问题描述】:

我正在尝试使用可变数据绘制饼图。

下面是我的代码。 Postive_percentagetopic 都是 list,其数据来自代码的其他部分并通过各种操作。我已经从输出中发布了 Postive_percentagetopic 的值。

我是 python 新手,这就是为什么我无法理解这个问题。 Postive_percentagetopiccolors 有 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


    【解决方案1】:

    分配给标签时删除str(..)。 如果您需要副本而不仅仅是参考,请将其替换为 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() 时,它将整个列表作为字符串,包括方括号。因此,在控制台打印时,你可能会认为这是一个列表,而它只是一个长字符串..

    【讨论】:

    • 非常感谢先生。你解决了我的问题。我确实将 list() 添加到主题和大小并得到了输出。再次非常感谢您。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-24
    • 2018-11-04
    • 2019-02-24
    • 2018-01-28
    • 1970-01-01
    • 2014-08-25
    相关资源
    最近更新 更多