Learn
Line graphs are helpful for visualizing how a variable changes over time.
Some possible data that would be displayed with a line graph:
- average prices of gasoline over the past decade
- weight of an individual over the past couple of months
- average temperature along a line of longitude over different latitudes
Using Matplotlib methods, the following code will create a simple line graph using .plot()
and display it using .show()
:
x_values = [0, 1, 2, 3, 4] y_values = [0, 1, 4, 9, 16] plt.plot(x_values, y_values) plt.show()
x_values
is a variable holding a list of x-values for each point on our line graphy_values
is a variable holding a list of y-values for each point on our line graphplt
is the name we have given to the Matplotlib module we have imported at the top of the codeplt.plot(x_values, y_values)
will create the line graphplt.show()
will actually display the graph
Our graph would look like this:

Let’s get some practice with plotting lines.
Instructions
1.
We are going to make a simple graph representing someone’s spending on lunch over the past week.
First, define two lists, days
and money_spent
, that contain the following integers:
Days | Money Spent |
---|---|
0 | 10 |
1 | 12 |
2 | 12 |
3 | 10 |
4 | 14 |
5 | 22 |
6 | 24 |
Make sure to scroll to the bottom of the table to see all the rows of data!
2.
Plot days
on the x-axis and money_spent
on the y-axis using plt.plot()
.
3.
Show the plot using plt.show()
.
Sign up to start coding
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.