Search code examples
pythonmatplotlibcolorspie-chart

How do I change the color of percentage label to be white in pie chart without interfering the text label color?


How do I change the color of percentage label to be white in pie chart without interfering the text label color using matplotlib? When I edit textprops={'size': 'x-large','color':'w'}, the text color will turn white which it is not contrast to the background.

labels = df01['new_sentiment']
count = df01['count']

cmap = plt.get_cmap('Greys')
colors = list(cmap(np.linspace(0.45, 0.85, len(count))))

fig1, ax1 = plt.subplots()
ax1.pie(count, labels=labels, autopct='%1.1f%%', textprops={'size': 'x-large'}, colors=colors)

ax1.axis('equal')
plt.title('7D-prior-PM_sem_sen', pad=30)
plt.savefig('04_7D-prior-PM_sem_sen.tiff', dpi=300, format='tiff', bbox_inches='tight')

enter image description here


Solution

  • You can change the colors of the inner labels like this:

    _,_,m = ax1.pie(count, labels=labels, autopct='%1.1f%%', textprops={'size': 'x-large'}, colors=colors)
    [m[i].set_color('white') for i in range(len(m))]