Matplotlib 3.0 Cookbook
上QQ阅读APP看书,第一时间看更新

How to do it...

The following code block plots a pie chart of the genre of movies released in a year:

  1. Set up the data for the pie chart:
labels = ['SciFi', 'Drama', 'Thriller', 'Comedy', 'Action', 'Romance']
sizes = [5, 15, 10, 20, 40, 10] # Add upto 100%
  1. Show one slice slightly outside the circle:
explode = (0, 0, 0, 0, 0.1, 0) # only "explode" the 5th slice (i.e.'Action')
  1. Plot the pie chart:
plt.pie(sizes, labels=labels, explode=explode, autopct='%1.1f%%', 
shadow=True, startangle=90)
  1. The equal aspect ratio ensures that pie is drawn as a circle. The default is an ellipse:
plt.axis('equal') 
  1. Display the graph on the screen:
plt.show()