#161 Custom matplotlib donut plot

 

 

 

 

 

This page aims to describe how to improve the features of your basic donut plot. It shows how to custom color, labels, and wedges. These first line of code are common to every next examples

 

 

 

 

 


# library
import matplotlib.pyplot as plt

# create data
names='groupA', 'groupB', 'groupC', 'groupD',
size=[12,11,3,30]

# Create a circle for the center of the plot
my_circle=plt.Circle( (0,0), 0.7, color='white')
<pre>
Color

Here I propose 3 examples allowing to understand how to custom the color of your donut chart. On the first example (left), colors are provided one by one. Note that if you don’t give enough color, they will be recycled (middle). The last chart show how to import a palette using the Palettable utility. See the matplotlib color page for more info!


# Give color names
plt.pie(size, labels=names, colors=['red','green','blue','skyblue'])
p=plt.gcf()
p.gca().add_artist(my_circle)
plt.show()

# Custom colors --> colors will cycle
plt.pie(size, labels=names, colors=['red','green'])
p=plt.gcf()
p.gca().add_artist(my_circle)
plt.show()

from palettable.colorbrewer.qualitative import Pastel1_7
plt.pie(size, labels=names, colors=Pastel1_7.hex_colors)
p=plt.gcf()
p.gca().add_artist(my_circle)
plt.show()
Labels

# Label distance: gives the space between labels and the center of the pie
plt.pie(size, labels=names, labeldistance=0.45)
p=plt.gcf()
p.gca().add_artist(my_circle)
plt.show()

# Label color
plt.rcParams['text.color'] = 'red'
plt.pie(size, labels=names)
p=plt.gcf()
p.gca().add_artist(my_circle)
plt.show()
Wedges


# Custom wedges
plt.pie(size, labels=names, wedgeprops = { 'linewidth' : 7, 'edgecolor' : 'white' })
p=plt.gcf()
p.gca().add_artist(my_circle)
plt.show()

Leave a Reply

Your email address will not be published.