Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib pie chart wedge transparency?

I saw that matplotlib's pyplot.scatter() has an 'alpha' parameter that can be used to set the transparency of points. The pyplot.pie() doesn't have a similar parameter however. How can I set the transparency of certain wedges?

like image 938
fariadantes Avatar asked May 25 '18 20:05

fariadantes


People also ask

How do you avoid overlapping of labels and Autopct in a matplotlib pie chart?

To avoid overlapping of labels and autopct in a matplotlib pie chart, we can follow label as a legend, using legend() method.

What is Autopct in pie chart?

labels is a list of sequence of strings which sets the label of each wedge. color attribute is used to provide color to the wedges. autopct is a string used to label the wedge with their numerical value. shadow is used to create shadow of wedge.

What is explode in matplotlib?

explode : array-like, optional, default: None. If not None, is a len(x) array which specifies the fraction of the radius with which to offset each wedge. labels : list, optional, default: None. A sequence of strings providing the labels for each wedge.

How do you change the background color of a pie chart in Python?

In order to change the background color, you should create a figure with figure() function and set the color with patch. set_facecolor() before creating your pie plot.


1 Answers

I found the answer while writing up this question and figured I'd post the solution for anyone who wants to know.

To set a wedge to be transparent:

import matplotlib.pyplot as plt

x  = [1,2,3,0.4,5]
alpha = 0.5
which_wedge = 4
n = plt.pie(x)
n[0][which_wedge].set_alpha(alpha)

If you want to only display a single wedge, use a loop:

for i in range(len(n[0])):
    n[0][i].set_alpha(0.0)
n[0][which_wedge].set_alpha(1.0)

Hope this helps someone! It can probably be used for pyplot.bar() too to hide certain bars.

like image 127
fariadantes Avatar answered Oct 27 '22 06:10

fariadantes