Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Multiple Line Plots | Creating Commonly Used Plots
Ultimate Visualization with Python
course content

Course Content

Ultimate Visualization with Python

Ultimate Visualization with Python

1. Matplotlib Introduction
2. Creating Commonly Used Plots
3. Plots Customization
4. More Statistical Plots
5. Plotting with Seaborn

book
Multiple Line Plots

It is often the case when we need to create more than one line plot on one Axes object. For instance, we may want to compare two or more graphs which represent certain dynamics, trends, etc. There are two possible ways to accomplish this. Let's start with the first one.

Here is a sample of our data:

12345
import pandas as pd url = 'https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/weather_data.csv' # Loading the dataset with the average yearly temperatures in Boston and Seattle weather_df = pd.read_csv(url, index_col=0) print(weather_df.head())
copy

We will use two line plots to compare average yearly temperatures of Seattle and Boston. Here is the first option:

1234567
import matplotlib.pyplot as plt import pandas as pd weather_df = pd.read_csv('https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/weather_data.csv', index_col=0) # Calling the plot() function for each of the line plots plt.plot(weather_df['Boston'], '-o') plt.plot(weather_df['Seattle'], '-o') plt.show()
copy

First Option

Here we use the plot() function twice to plot two line separate line plots on one Axes object. Remember, indices of the pandas Series are used (in our example we have years as indices) for x-axis values.

The second option is the following:

123456
import matplotlib.pyplot as plt import pandas as pd weather_df = pd.read_csv('https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/weather_data.csv', index_col=0) # Calling the plot() function once for two line plots plt.plot(weather_df['Boston'], '-o', weather_df['Seattle'], '-o') plt.show()
copy

Second Option

Here we use the plot() function only once, however, since we are specifying the markers twice, matplotlib understands that  these are two separate plots (and their indices are used).

However, without specifying the markers this option creates only one plot (using the leftmost pandas Series for x-axis and the right one for y-axis).

As a side note, feel free to explore even more about line plots with their documentation.

Task
test

Swipe to show code editor

  1. Use the correct function to create a line plot on the fifth line.
  2. Pass data_linear as an argument in the function on the sixth line, do not use any markers.
  3. Use the correct function to create a line plot on the eighth line.
  4. Pass data_squared as an argument in the function on the fifth line, use 'o' markers with solid line.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 2
toggle bottom row

book
Multiple Line Plots

It is often the case when we need to create more than one line plot on one Axes object. For instance, we may want to compare two or more graphs which represent certain dynamics, trends, etc. There are two possible ways to accomplish this. Let's start with the first one.

Here is a sample of our data:

12345
import pandas as pd url = 'https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/weather_data.csv' # Loading the dataset with the average yearly temperatures in Boston and Seattle weather_df = pd.read_csv(url, index_col=0) print(weather_df.head())
copy

We will use two line plots to compare average yearly temperatures of Seattle and Boston. Here is the first option:

1234567
import matplotlib.pyplot as plt import pandas as pd weather_df = pd.read_csv('https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/weather_data.csv', index_col=0) # Calling the plot() function for each of the line plots plt.plot(weather_df['Boston'], '-o') plt.plot(weather_df['Seattle'], '-o') plt.show()
copy

First Option

Here we use the plot() function twice to plot two line separate line plots on one Axes object. Remember, indices of the pandas Series are used (in our example we have years as indices) for x-axis values.

The second option is the following:

123456
import matplotlib.pyplot as plt import pandas as pd weather_df = pd.read_csv('https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/weather_data.csv', index_col=0) # Calling the plot() function once for two line plots plt.plot(weather_df['Boston'], '-o', weather_df['Seattle'], '-o') plt.show()
copy

Second Option

Here we use the plot() function only once, however, since we are specifying the markers twice, matplotlib understands that  these are two separate plots (and their indices are used).

However, without specifying the markers this option creates only one plot (using the leftmost pandas Series for x-axis and the right one for y-axis).

As a side note, feel free to explore even more about line plots with their documentation.

Task
test

Swipe to show code editor

  1. Use the correct function to create a line plot on the fifth line.
  2. Pass data_linear as an argument in the function on the sixth line, do not use any markers.
  3. Use the correct function to create a line plot on the eighth line.
  4. Pass data_squared as an argument in the function on the fifth line, use 'o' markers with solid line.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 2
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt