Python – How to change autopct text to white and bold in a pie chart?
Question:
I want to change the autopct text to bold and white.
If I insert textprops={‘color’:’white’, ‘weight’:’bold’, ‘fontsize’:12.5} in ax1.pie(..) the labels disappear.
Can someone help me please?
sizes1 = [3, 19]
explode1 = (0, 0.05)
fig, (ax1, ax2) = plt.subplots(1,2, figsize=(10,10))
labels = ('CRD = 1', 'CRD = 0')
#fig1, ax1 = plt.subplots()
ax1.pie(sizes1,explode= explode1, labels=labels, autopct='%1.1f%%',
shadow=False,startangle=40, colors=('tab:red', 'tab:blue'))
ax1.set_title('Frauen', fontdict={'fontsize': 17}, y=0.8)
ax1.axis('equal')
sizes2 = [10, 24]
explode2 = (0, 0.05)
ax2.pie(sizes2, labels=labels, autopct='%1.1f%%',
shadow=False,explode = explode2, startangle=345, colors=('tab:red','tab:blue'), )
ax2.set_title('Männer', fontdict={'fontsize': 17}, y=0.8)
ax2.axis('equal')
Pie Plots
Answers:
Because the textprops
apply to both, the labels and the autopercentage texts, you need to format the autopercentage texts externally to the pie
function.
import matplotlib.pyplot as plt
sizes1 = [3, 19]
explode1 = (0, 0.05)
labels = ('CRD = 1', 'CRD = 0')
fig1, ax1 = plt.subplots()
_, _, autopcts = ax1.pie(sizes1,explode= explode1, labels=labels, autopct='%1.1f%%',
shadow=False,startangle=40, colors=('tab:red', 'tab:blue'))
plt.setp(autopcts, **{'color':'white', 'weight':'bold', 'fontsize':12.5})
ax1.set_title('Frauen', fontdict={'fontsize': 17})
plt.show()
You are plotting white on white:
In your case the labels disappear because you are setting the color to white in the textprops
argument. This is how you are doing it:
textprops={'color':'white', 'weight':'bold', 'fontsize':12.5}
and it plots white labels on white background, hence not seeng it. You can either:
1. change the plot background color to a different color
fig.set_facecolor('lightgrey')
or
2. change the textprops
color to something different:
textprops={'color':"blue", 'size': 10, 'weight':'bold'}
You can easily manage the font size, style and color using the textprops
argument in the ax.pie()
function.
I want to change the autopct text to bold and white.
If I insert textprops={‘color’:’white’, ‘weight’:’bold’, ‘fontsize’:12.5} in ax1.pie(..) the labels disappear.
Can someone help me please?
sizes1 = [3, 19]
explode1 = (0, 0.05)
fig, (ax1, ax2) = plt.subplots(1,2, figsize=(10,10))
labels = ('CRD = 1', 'CRD = 0')
#fig1, ax1 = plt.subplots()
ax1.pie(sizes1,explode= explode1, labels=labels, autopct='%1.1f%%',
shadow=False,startangle=40, colors=('tab:red', 'tab:blue'))
ax1.set_title('Frauen', fontdict={'fontsize': 17}, y=0.8)
ax1.axis('equal')
sizes2 = [10, 24]
explode2 = (0, 0.05)
ax2.pie(sizes2, labels=labels, autopct='%1.1f%%',
shadow=False,explode = explode2, startangle=345, colors=('tab:red','tab:blue'), )
ax2.set_title('Männer', fontdict={'fontsize': 17}, y=0.8)
ax2.axis('equal')
Pie Plots
Because the textprops
apply to both, the labels and the autopercentage texts, you need to format the autopercentage texts externally to the pie
function.
import matplotlib.pyplot as plt
sizes1 = [3, 19]
explode1 = (0, 0.05)
labels = ('CRD = 1', 'CRD = 0')
fig1, ax1 = plt.subplots()
_, _, autopcts = ax1.pie(sizes1,explode= explode1, labels=labels, autopct='%1.1f%%',
shadow=False,startangle=40, colors=('tab:red', 'tab:blue'))
plt.setp(autopcts, **{'color':'white', 'weight':'bold', 'fontsize':12.5})
ax1.set_title('Frauen', fontdict={'fontsize': 17})
plt.show()
You are plotting white on white:
In your case the labels disappear because you are setting the color to white in the textprops
argument. This is how you are doing it:
textprops={'color':'white', 'weight':'bold', 'fontsize':12.5}
and it plots white labels on white background, hence not seeng it. You can either:
1. change the plot background color to a different color
fig.set_facecolor('lightgrey')
or
2. change the textprops
color to something different:
textprops={'color':"blue", 'size': 10, 'weight':'bold'}
You can easily manage the font size, style and color using the textprops
argument in the ax.pie()
function.