Gonzalo Gonzalo - 1 year ago 95
Python Question

Issue with pie chart matplotlib - startangle / len()

I have a script which creates a pie chart based on CSV files. My problem started when I was reading a CSV that had only one row (e.g.

percent = [100]
). Is there any limitation of using a pie chart, where is will not show 100% for only one item? It seems that the error is related to either the

My code is:

percent = [100]

plt.pie(percent, # data
explode=(0), # offset parameters
#labels=country, # slice labels - removed to hid labels and added labels=country in legend()
colors=colors, # array of colours
autopct='%1.0f%%', # print the values inside the wedges - add % to the values
shadow=False, # enable shadow
startangle=70 # starting angle

plt.legend(loc='best', labels=country)

Error in the line of startangle=70:

if len(x) != len(explode):
TypeError: object of type 'float' has no len()


Answer Source

Change the explode parameter to a list:

percent = [100]
explode = [0]

plt.pie(percent, explode=explode, ...)

If you have more values, you can use a tuple, but with one value (int) is seen as an integer:

>>> type((0))
<type 'int'>
>>> type((0, 1))
<type 'tuple'>

>>> type([0])
<type 'list'>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download