Tuesday, August 1, 2017

Donut chart with python matplotlib

There is no donut chart option in matplatlib. However, we will use pie and circle to create a donut chart. Let's look at the example below.
import matplotlib
import matplotlib.pyplot as plt
   
matplotlib.rcParams['text.color'] = 'k'
matplotlib.rcParams['font.size'] = 20

# The slices will be ordered and plotted counter-clockwise.
labels = ['Frogs', 'Hogs', 'Dogs', 'Logs']
sizes = [15, 30, 45, 10]
color = ['#F6B445', '#BDB39C', '#14E800', '#85B731']
explode = [0, 0, 0, 0]  # explode a slice if required

fig = plt.figure(figsize=(10,10), dpi=50)

plt.pie(sizes, explode=explode, labels=labels, colors=color,
        autopct='%1.0f%%', 
        startangle=45,
        pctdistance=0.8, 
        labeldistance=1.1, 
        shadow=False)
        
#draw a circle at the center of pie to make it look like a donut
centre_circle = plt.Circle((0,0), 0.65, color='black', fc='white', linewidth=0)
fig = plt.gcf()
fig.gca().add_artist(centre_circle)

# Set aspect ratio to be equal so that pie is drawn as a circle.
plt.axis('equal')
plt.show()

No comments:

Post a Comment