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'?

1

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')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest