4

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

Community
  • 1
  • 1
edesz
  • 11,756
  • 22
  • 75
  • 123

1 Answers1

2

A:

From the mpl pie documentation:

If autopct is not None, return the tuple (patches, texts, autotexts), where patches and texts are as above, and autotexts is a list of Text instances for the numeric labels.

You need to unpack one value more. So you need to change that line:

patches, texts = plt.pie(...)

for

patches, texts, autotexts = plt.pie(...)

B:

As the plt.pie error say does not have a path_effects argument. You can loop over patches and set the custom shadow:

for patch in patches:
    patch.set_path_effects([path_effects.SimpleLineShadow(),
        path_effects.Normal()])

C:

You can use plt.rc for that.

Lucas
  • 6,869
  • 5
  • 29
  • 44
  • Thanks. For C. I was thinking of using `pl.rcParams.update({'legend.font':labelfont})`. But, what should I use instead of `legend`? i.e. what are these percent labels called? – edesz Jan 15 '17 at 03:56
  • For A., I tried it. I used `for label in autotexts: label.set_horizontalalignment('center'); label.set_verticalalalignment('center')`. This allows me to horizontally and vertically move the wedge label. However, the topmost wedge label (10.0%) is still floating too far away from the wedge compared to the others. Is there any other way to reduce this separation? – edesz Jan 15 '17 at 04:07
  • 1
    For A: You can use `label.set_position((x, y))` , but it does not seem like a good solution. You can also increase the radius! – Lucas Jan 15 '17 at 05:11