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 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:

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:

DaysMoney Spent
010
112
212
310
414
522
624

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

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?