I am trying to produce a Matplotlib pie chart in Python 2.7 using the example shown here. Here is the code I am using:
import matplotlib.pyplot as plt
# The slices will be ordered and plotted counter-clockwise.
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral']
explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')
plt.pie(sizes, explode=explode, colors=colors,
autopct='%1.1f%%', shadow=True, startangle=90, radius=0.4,
center=(0.5,0.5),
frame=True, pctdistance=1.125)
# Set aspect ratio to be equal so that pie is drawn as a circle.
plt.axis('equal')
plt.show()
The output of this is shown below
There is some plot formatting related to labels and drop shadow that I am trying to adjust and would like to get some help with these:
A. The label for the fraction 10% is much further away from the pink wedge than the label 45% from the blue wedge. There seems to be questions (1,2) about this but the latest Matploltlib has too many values to unpack. So, when I use the answer from there:
patches, texts = plt.pie(...)
it throws this error:
ValueError: too many values to unpack
Is there a way to ensure that all percentage labels are equidistant from their corresponding wedge?
B. The drop shadow is too thick (or strong). I attempted to customize this with the path_effects
argument from here using:
plt.pie(sizes, explode=explode, colors=colors,
autopct='%1.1f%%', shadow=True, startangle=90, radius=0.4,
center=(0.5,0.5),
frame=True, pctdistance=1.125,
path_effects=[path_effects.SimpleLineShadow(),
path_effects.Normal()])
However, I get an error:
TypeError: pie() got an unexpected keyword argument 'path_effects'
There seems to be a similar question here, but without an answer. Is there a way to manually reduce the thickness of the drop shadow?
C. I have specified a dictionary with the font I would like to use for the pie percentage labels:
labelfont = {'fontname':'Times New Roman', 'size':'14', 'color':'black', 'weight':'bold'}
For example, if I wanted to use this on the x-axis label of a scatter plot, I have been using this:
plt.xlabel('My_label',**labelfont)
I am trying to do something similar for the pie percentage labels. How can I specify that this font should be used for the percentages?