Skip to content

Line Plots

Line Plots

Line plots in Matplotlib can be created using Matplotlib's pyplot library. To create a line plot with Python and Matplotlib, first import Matplotlib's pyplot library using the line:

import matplot.pyplot as plt

It is common practice to use the alias plt for the matplotlib.pyplot library. Defining matplotlib.pyplot as plt allows us to use the syntax plt.<function name> instead of matplotlib.pyplot.<function name> in the rest of our code. See the differences below:

Importing matplotlib.pyplot with no alias:

import matplotlib.pyplot

matplotlib.pyplot.plot(x, y)

Importing matplotlib.pyplot with the alias plt:

import matplotlib.pyplot as plt

plt.plot(x, y)

Using the alias plt simplifies the code and cuts down the number of keystrokes needed to create a plot. Any alias can be used, such as import matplotlib.pyplot as RainbowUnicorn, but the alias plt is the standard convention.

If building Matplotlib plots in a Jupyter notebook, include the line:

%matplotlib inline

Below the import line. The %matplotlib inline line will output matplotlib plots directly in Jupyter notebook output cells. To build the first line plot, first import Matplotlib. If using a jupyter notebook include the line %matplotlib inline.

import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

Once Matplotlib is imported, and plots are set to display in Jupyter notebook output cells, we need data to plot. We will create the data as a Python list. In Python, list is a built-in data structure which can store multiple values. Lists are defined in Python using square brackets [ ]. The elements of a list are separated using commas [item1, item2, item 3].

An example line of code to create a list of numbers in Python is below:

a = [1, 3, 4, 8]

The numbers 1, 3, 4, and 8 are stored in a list. Once the list is defined, we can plot the values using Matplotlib's plt.plot() function. To create a line plot, pass the list of numbers as an argument to the plt.plot() function. Arguments are values passed to functions as input. The command plt.show() is needed at the end to show the plot. Make sure to include the double parenthesis ().

In [1]:
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

a = [1, 3, 4, 8] plt.plot(a) plt.show()

A line plot of the numbers 1, 3, 4, 8 is produced. Note the points 1, 3, 4, 8 are the y-values on the plot. The x-axis starts at zero and ends at three. Matplotlib automatically set the x-axis values, because only one list was supplied to plt.plot(). Let's specify two lists, one for the x-values on the plot called x and one for the y-values on the plot called y. With these two lists created, call plt.plot(x,y) and plt.show() and see the changes on a new plot.
In [2]:
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

x = [0, 2, 4, 6] y = [1, 3, 4, 8]

plt.plot(x,y) plt.show()

A line plot with x-values 0, 2, 4, 6 is produced. By specifying two lists to plt.plot(), we control both the x and y points shown in the line plot. The plot works, but it is pretty basic. In the next section, you will learn how to customize Matplotlib plots. So far the two plots we made are pretty basic. Lucky for us, Matplotlib has a vast array of customization options. You can customize and modify almost every part of the Matplotlib plot. The next section addresses customizing Matplotlib plots.