user9639519
user9639519

Reputation:

How to set transparency and background colour for a pie chart matplotlib

I'm sure this is somewhere in SO but I can't see to find it anywhere. I'm trying to alter the colour transparency or alpha for a pie chart in matplotlib. I'm also hoping to set the background colour to grey.

import pandas as pd
import matplotlib.pyplot as plt

d = ({ 
    'C' : ['X','Y','Z','X','Y','Z','A','X','Y','Z'],
    })

df = pd.DataFrame(data=d)

fig, ax = plt.subplots(figsize = (20,12))
ax.grid(False)
plt.style.use('ggplot')

df['C'].value_counts().plot(kind = 'pie')

plt.show

I cant seem to change the pie chart colour transparency or use the background generally created by 'ggplot'?

Upvotes: 9

Views: 13231

Answers (1)

Sheldore
Sheldore

Reputation: 39072

There are two similar ways: the basic idea is to define the wedge properties using wedgeprops. On a side note, I would rather use lightgrey background as it looks much better than grey which is a bit dark.

First:

df['C'].value_counts().plot(kind = 'pie',wedgeprops={'alpha':0.5})
fig.set_facecolor('lightgrey')

enter image description here

Second: using plt.pie

plt.pie(df['C'].value_counts(), wedgeprops={'alpha':0.5})
fig.set_facecolor('lightgrey')

enter image description here

Upvotes: 11

Related Questions