Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing line properties in matplotlib pie chart

I'm trying to make the lines on my matplotlib pie chart much lighter. Because I have so many slices, the lines are way too thick, as shown here:

enter image description here

I read this example which suggests using rcparam like this:

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

but although I can change the text color, it doesn't change the width of the lines between the pie slices. I believe that's because the slices aren't governed by line objects but by wedge objects. So is there a way to set the wedge border style and color?

Thanks a lot, Alex

like image 833
Alex S Avatar asked Dec 12 '13 18:12

Alex S


People also ask

How do I change the font size in a pie chart in matplotlib?

Matplotlib pie chart legend font size To change the fontsize we use the fontsize argument with the legend() method.

What is Autopct in Python pie chart?

autopct enables you to display the percent value using Python string formatting. For example, if autopct='%. 2f' , then for each pie wedge, the format string is '%. 2f' and the numerical percent value for that wedge is pct , so the wedge label is set to the string '%.

What is explode in matplotlib?

To “explode” a pie chart means to make one of the wedges of the pie chart to stand out. To make this possible in matplotlib , we use the explode() parameter.


2 Answers

try this:

ax = plt.subplot(111) 
wedges, texts = ax.pie(np.abs(np.random.randn(5)))

for w in wedges:
    w.set_linewidth(2)
    w.set_edgecolor('cyan')

pie

Additionally, if you only have an axes object and don't have direct access to the pie's wedges you can retrieve the wedges from ax.patches:

wedges = [patch for patch in ax.patches if isinstance(patch, matplotlib.patches.Wedge)]
like image 187
behzad.nouri Avatar answered Oct 28 '22 06:10

behzad.nouri


Im coming late to this party, but just found that, for pie charts, instead of using:

plt.rcParams['lines.linewidth'] = 2

you can use:

plt.rcParams['patch.linewidth'] = 0  

Also, to change the pie chart line color use this:

plt.rcParams['patch.edgecolor'] = 'white' 

instead of:

plt.rcParams['line.color'] = 'white'
like image 26
Manuel G Avatar answered Oct 28 '22 07:10

Manuel G