
上QQ阅读APP看书,第一时间看更新
How to do it...
The following code block plots a pie chart of the genre of movies released in a year:
- 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%
- Show one slice slightly outside the circle:
explode = (0, 0, 0, 0, 0.1, 0) # only "explode" the 5th slice (i.e.'Action')
- Plot the pie chart:
plt.pie(sizes, labels=labels, explode=explode, autopct='%1.1f%%',
shadow=True, startangle=90)
- The equal aspect ratio ensures that pie is drawn as a circle. The default is an ellipse:
plt.axis('equal')
- Display the graph on the screen:
plt.show()