Tables

To display the numerical values 78, 21, and 1, we will use the table method that Matplotlib provides and give the keyword argument cell data or cell text.

By inserting colLabels and rowLabels, we get a nice labeled table:

# Add a table
fracs = (78, 21, 1)
labels = ('Nitrogen', 'Oxygen', 'Argon')
plt.pie(fracs, labels=labels)
plt.table(cellText=[fracs], colLabels=labels, rowLabels=["fraction"])
plt.gca().set_aspect('equal')

This is a nice way to supplement data. If we have a plot where we have a couple of numbers to show alongside it, the plot table is the way to do this. You may recall that pandas provides a way to automatically do this.

We can also change the location of this table. We will change the location of the table so that it's to the left, as shown in the following code:

# Change location w/ loc
fracs = (78, 21, 1)
labels = ('Nitrogen', 'Oxygen', 'Argon')
plt.pie(fracs, labels=labels)
plt.table(cellText=[fracs], rowLabels=['Fraction'], colLabels=labels, loc='left')
plt.gca().set_aspect('equal')

We will get the following output:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset