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
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:
We could add a friend’s lunch spending for comparison like this:
# Days of the week: days = [0, 1, 2, 3, 4, 5, 6] # Your Money: money_spent = [10, 12, 12, 10, 14, 22, 24] # Your Friend's Money: money_spent_2 = [11, 14, 15, 15, 22, 21, 12] # Plot your money: plt.plot(days, money_spent) # Plot your friend's money: plt.plot(days, money_spent_2) # Display the result: plt.show()
We then get two lines on the same plot:
By default, the first line is always blue, and the second line is always orange. In the next exercise, we’ll learn how to customize these lines ourselves.
We have defined lists called
time on the same plot as the last line.
Show the plot using