Gonzalo Gonzalo - 2 months ago 11
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
startangle
or
explode
arguments.

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.axis('equal')
plt.legend(loc='best', labels=country)
plt.tight_layout()


Error in the line of startangle=70:

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


Thanks!

Answer

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'>
Comments