Open oldoc63 opened 1 year ago
Line graphs are helpful for visualizing how a variable changes over time. Some possible data that would be displayed with a line graph:
Using matplotlib methods, the following code will create a simple line graph using .plot() and displaying it using .show():
x_values = [0, 1, 2, 3, 4]
y_values = [0, 9, 4, 1, 16]
plt.plot(x_values, y_values)
plt.show()
We can also have multiple line plots displayed on the same set of axes. This can be very useful if we want to compare two datasets with the same scale and axis categories. Matplotlib will automatically place the two lines on the same axes and give them different colors if you call plt.plot() twice.
Let's look at the graph we made in the last exercise to track lunch spending, where days is on the x-axis and spending (money_spent) is on the y-axis and add a friend's spending for comparison:
We then get two lines on the same plot. By default, the first line is always blue, and the second line is always orange.
Matplotlib is a Python library used to create charts and graphs. The concepts you will learn include: