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...
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'?
1 Answer
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')
Second: using plt.pie
plt.pie(df['C'].value_counts(), wedgeprops={'alpha':0.5})
fig.set_facecolor('lightgrey')