Pie charts

Let's take a look at the fractions of gases that exist in our atmosphere. The following content shows the code and output for Nitrogen, which is 78 percent of our atmosphere, Oxygen, which is 21 percent, and Argon, which is 1 percent. Now, naturally, Nitrogen, Oxygen, and Argon are not exactly numerical values. Hence, we cannot really plot these things against each other in the standard way. This is where we can use a pie chart as a best practice to display these differences:

  1. First, we must set the aspect ratio to equal here. This gives a standard pie chart, as follows:
# Basic Pie Chartfracs = (78, 21, 1)labels = ('Nitrogen', 'Oxygen', 'Argon')plt.pie(fracs, labels=labels);plt.gca().set_aspect('equal')

We will get the following ...

Get Mastering Matplotlib 2.x now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.