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
x_values = [0, 1, 2, 3, 4] y_values = [0, 1, 4, 9, 16] plt.plot(x_values, y_values) plt.show()
x_valuesis a variable holding a list of x-values for each point on our line graph
y_valuesis a variable holding a list of y-values for each point on our line graph
pltis 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.
We are going to make a simple graph representing someone’s spending on lunch over the past week.
First, define two lists,
money_spent, that contain the following integers:
Make sure to scroll to the bottom of the table to see all the rows of data!
days on the x-axis and
money_spent on the y-axis using
Show the plot using