ValueError: 'label' must be of length 'x' using matplotlib

后端 未结 1 1746
既然无缘
既然无缘 2021-01-27 12:05

I am trying to plot pie chart using varible data.

Below is my code. both Postive_percentage and topic is the list whos

相关标签:
1条回答
  • 2021-01-27 12:35

    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()
    

    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..

    0 讨论(0)
提交回复
热议问题