Basics
Last updated
Was this helpful?
Last updated
Was this helpful?
The Python library Matplotlib contains the pyplot module, which provides users with an interface for graphing data. Pyplot contains over 100 functions, from acorr to yticks. You must import the module, and plt
is the standard variable name used.
Line graphs are helpful for visualizing how a variable changes over time.
Using Matplotlib methods, the following code will create a simple line graph using .plot()
and display it using .show()
:
x_values
is a variable holding a list of x-values for each point on our line graph
y_values
is a variable holding a list of y-values for each point on our line graph
plt
is the name we have given to the Matplotlib module we have imported at the top of the code
plt.plot(x_values, y_values)
will create the line graph
plt.show()
will actually display the graph
Our graph would look like this:
We can also have multiple line plots displayed on the same set of axes.
We then get two lines on the same plot:
By default, the first line is always blue, and the second line is always orange. In the next exercise, we’ll learn how to customize these lines ourselves.
We can specify a different color for a line by using the keyword color
with either an HTML color name or a HEX code:
We can also make a line dotted or dashed using the keyword linestyle
.
We can also add a marker using the keyword marker
:
Sometimes, it can be helpful to zoom in or out of the plot, especially if there is some detail we want to address. To zoom, we can use plt.axis()
. We use plt.axis()
by feeding it a list as input. This list should contain:
The minimum x-value displayed
The maximum x-value displayed
The minimum y-value displayed
The maximum y-value displayed
For example, if we want to display a plot from x=0
to x=3
and from y=2
to y=5
, we would call plt.axis([0, 3, 2, 5])
.
Eventually, we will want to show these plots to other people to convince them of important trends in our data. When we do that, we’ll want to make our plots look as professional as possible.
The first step towards a professional-looking plot is adding labels to the x-axis and y-axis, and giving the plot a title.
We can label the x- and y- axes by using plt.xlabel()
and plt.ylabel()
. The plot title can be set by using plt.title()
.
Sometimes, we want to display two lines side-by-side, rather than in the same set of x- and y-axes. When we have multiple axes in the same picture, we call each set of axes a subplot. The picture or object that contains all of the subplots is called a figure.
We can create subplots using .subplot()
.
The command plt.subplot()
needs three arguments to be passed into it:
The number of rows of subplots
The number of columns of subplots
The index of the subplot we want to create
For instance, the command plt.subplot(2, 3, 4)
would create “Subplot 4” from the figure above.
Any plt.plot()
that comes after plt.subplot()
will create a line plot in the specified subplot. For instance:
This would result in a figure with the two plots arranged like this:
We can have many different subplots in the same figure, and we can lay them out in many different ways. We can think of our layouts as having rows and columns of subplots. For instance, the following figure has six subplots split into 2 rows and 3 columns: