matplotlib: Controlling pie chart font color, line width
Asked Answered
B

4

16

I'm using some simple matplotlib functions to draw a pie chart:

f = figure(...)
pie(fracs, explode=explode, ...)

However, I couldn't find out how to set a default font color, line color, font size – or pass them to pie(). How is it done?

Buddie answered 16/12, 2009 at 16:31 Comment(0)
P
13

Global default colors, line widths, sizes etc, can be adjusted with the rcParams dictionary:

import matplotlib
matplotlib.rcParams['text.color'] = 'r'
matplotlib.rcParams['lines.linewidth'] = 2

A complete list of params can be found here.

You could also adjust the line width after you draw your pie chart:

from matplotlib import pyplot as plt
fig = plt.figure(figsize=(8,8))
pieWedgesCollection = plt.pie([10,20,50,20],labels=("one","two","three","four"),colors=("b","g","r","y"))[0] #returns a list of matplotlib.patches.Wedge objects
pieWedgesCollection[0].set_lw(4) #adjust the line width of the first one.

Unfortunately, I can not figure out a way to adjust the font color or size of the pie chart labels from the pie method or the Wedge object. Looking in the source of axes.py (lines 4606 on matplotlib 99.1) they are created using the Axes.text method. This method can take a color and size argument but this is not currently used. Without editing the source, your only option may be to do it globally as described above.

Plunge answered 16/12, 2009 at 18:50 Comment(2)
Thank you, that's most helpful. One addition: To set the line widht of the actual wedge, you have to use (for the first wedge): pieWedgesCollection[0][0].set_linewidth(0)Buddie
So, how do you make font color of 'two' green, 'one' blude, ...? Do you know how to do that?Wirewove
T
15

Showing up a bit late for the party but I encountered this problem and didn't want to alter my rcParams.

You can resize the text for labels or auto-percents by keeping the text returned from creating your pie chart and modifying them appropriately using matplotlib.font_manager.

You can read more about using the matplotlib.font_manager here: http://matplotlib.sourceforge.net/api/font_manager_api.html

Built in font sizes are listed in the api; "size: Either an relative value of ‘xx-small’, ‘x-small’, ‘small’, ‘medium’, ‘large’, ‘x-large’, ‘xx-large’ or an absolute font size, e.g. 12"

from matplotlib import pyplot as plt
from matplotlib import font_manager as fm

fig = plt.figure(1, figsize=(6,6))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
plt.title('Raining Hogs and Dogs')

labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
fracs = [15,30,45, 10]

patches, texts, autotexts = ax.pie(fracs, labels=labels, autopct='%1.1f%%')

proptease = fm.FontProperties()
proptease.set_size('xx-small')
plt.setp(autotexts, fontproperties=proptease)
plt.setp(texts, fontproperties=proptease)

plt.show()

alt text

Thule answered 3/11, 2010 at 21:2 Comment(2)
That works great, thanks. Is there a similar way to change the line weight between slices?Amygdaline
How to change the font color of each label according to the pie segment colorWirewove
P
13

Global default colors, line widths, sizes etc, can be adjusted with the rcParams dictionary:

import matplotlib
matplotlib.rcParams['text.color'] = 'r'
matplotlib.rcParams['lines.linewidth'] = 2

A complete list of params can be found here.

You could also adjust the line width after you draw your pie chart:

from matplotlib import pyplot as plt
fig = plt.figure(figsize=(8,8))
pieWedgesCollection = plt.pie([10,20,50,20],labels=("one","two","three","four"),colors=("b","g","r","y"))[0] #returns a list of matplotlib.patches.Wedge objects
pieWedgesCollection[0].set_lw(4) #adjust the line width of the first one.

Unfortunately, I can not figure out a way to adjust the font color or size of the pie chart labels from the pie method or the Wedge object. Looking in the source of axes.py (lines 4606 on matplotlib 99.1) they are created using the Axes.text method. This method can take a color and size argument but this is not currently used. Without editing the source, your only option may be to do it globally as described above.

Plunge answered 16/12, 2009 at 18:50 Comment(2)
Thank you, that's most helpful. One addition: To set the line widht of the actual wedge, you have to use (for the first wedge): pieWedgesCollection[0][0].set_linewidth(0)Buddie
So, how do you make font color of 'two' green, 'one' blude, ...? Do you know how to do that?Wirewove
S
3
matplotlib.rcParams['font.size'] = 24

does change the pie chart labels font size

Schluter answered 10/6, 2010 at 11:47 Comment(1)
put it above your plt.pie statementPhrasing
S
0

To specify the line width and color in a pie chart, add this in pie():

wedgeprops={'linewidth': 2.0, 'edgecolor': 'white'}:

`plt.pie([10,20,50,20],labels=("one","two","three","four"),colors=("b","g","r","y"), wedgeprops={'linewidth': 2.0, 'edgecolor': 'white'})`
Studding answered 1/3, 2023 at 5:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.