13

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?

fariadantes
  • 347
  • 3
  • 17
  • 2
    Hey nice I used this right away - wonder if you are considering rewriting this as question and answer separately. I think it would make it clearer for the reader (normally, the code in the question is the one that does NOT work) – famargar Jul 09 '18 at 14:41

2 Answers2

4

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.

fariadantes
  • 347
  • 3
  • 17
  • 3
    I think that you can directly pas the `alpha` argument to `ax.pie` by adding the argument `wedgeprops` which is passed to the `patches.Wedge`. E.g. adding `wedgeprops = {"alpha": 0.5}`. – Seth Oct 31 '21 at 11:15
0

alpha can be passed to plt.pie directly using the wedgeprop arg (credit @Seth):

import matplotlib.pyplot as plt

plt.pie(x, wedgeprops={"alpha": 0.5})
johnDanger
  • 1,990
  • 16
  • 22