Gallery of Graphs
This lesson discusses the variety of commonly used graphs in Python.
We'll cover the following...
The plotting package matplotlib
allows you to make very fancy graphs. We will discuss some of them in this lesson.
Pie Charts #
A pie chart uses pie slices to show the percentage of each constituent in the whole unit. It is commonly used in scientific publications to represent data. Let’s look at its implementation using matplotlib
:
import numpy as npimport matplotlib.pyplot as pltgenre = ['Drama', 'Comedy', 'Thriller', 'Sci-Fi', 'History']amount = [100, 25, 30, 70, 75]pieColor = ['MediumBlue', 'SpringGreen', 'BlueViolet'];plt.axis('equal')plt.pie(amount, labels=genre, autopct='%1.1f%%', colors=pieColor)
The pie chart is created using the plt.pie()
command. There are two main arguments of plt.pie()
. First is the size of each section, which is stored in amount
. Second is their corresponding labels, which is stored in genre
. These are plotted sequentially in the counterclockwise direction.
The autopct = '%1.1f%%'
command shows the percentages to one decimal place. To format the percentage to the two decimal places, use autopct = '%1.2f%%'
, for three decimal places, use autopct = '%1.3f%%'
and so on.
You may want to use plt.axis('equal')
to make the scales along the horizontal and vertical axes equal so that the pie actually looks like a circle rather than an ellipse. We will use the colors
keyword in your pie chart to specify a sequence of colors. The sequence must be ...