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

Swipe to show menu

book
Multiple Line Plots

Often, it's necessary to create multiple line plots on a single Axes object to compare different trends or patterns. This can be done in two main ways. Here's the first approach.

Here is a sample of average yearly temperatures (in Β°\degreeF) of Seattle and Boston:

12345
import pandas as pd url = 'https://staging-content-media-cdn.codefinity.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

Two line plots will be used to compare data from Seattle and Boston.

First Option

The plot() function is used twice to create two separate line plots on the same Axes object. Remember, the indices of the pandas Series are used as the x-axis values β€” in this example, the years serve as the indices.

1234567
import matplotlib.pyplot as plt import pandas as pd weather_df = pd.read_csv('https://staging-content-media-cdn.codefinity.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

Second Option

In this example, the plot() function is called only once. Since markers are specified for both data series, matplotlib interprets them as two separate plots and uses the Series indices as the x-axis values.

If markers are not specified, the function creates only a single plot, using the first pandas Series for the x-axis and the second for the y-axis.

123456
import matplotlib.pyplot as plt import pandas as pd weather_df = pd.read_csv('https://staging-content-media-cdn.codefinity.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

Third Option

Another way to create multiple line plots in a single call is to pass the entire DataFrame directly to the plot() function.

In this case, matplotlib automatically treats each column in the DataFrame as a separate line plot. The index of the DataFrame is used for the x-axis, and the values of each column are plotted on the y-axis.

This approach is convenient when you want to quickly visualize multiple features across a common index (such as time or categories), without manually calling plot() for each one.

123456
import matplotlib.pyplot as plt import pandas as pd weather_df = pd.read_csv('https://staging-content-media-cdn.codefinity.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/weather_data.csv', index_col=0) # Calling the plot() function for whole DataFrame plt.plot(weather_df, '-o') plt.show()
copy
Note
Study More

Feel free to explore even more about line plots with plot() function documentation.

Task

Swipe to start coding

  1. Use the correct function to create a 2 line plots.
  2. Pass data_linear as an argument in the first plot function, do not use any markers.
  3. Pass data_squared as an argument in the second function, use 'o' markers with solid line.

Solution

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

Ask AI

expand
ChatGPT

Ask anything or try one of the suggested questions to begin our chat

book
Multiple Line Plots

Often, it's necessary to create multiple line plots on a single Axes object to compare different trends or patterns. This can be done in two main ways. Here's the first approach.

Here is a sample of average yearly temperatures (in Β°\degreeF) of Seattle and Boston:

12345
import pandas as pd url = 'https://staging-content-media-cdn.codefinity.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

Two line plots will be used to compare data from Seattle and Boston.

First Option

The plot() function is used twice to create two separate line plots on the same Axes object. Remember, the indices of the pandas Series are used as the x-axis values β€” in this example, the years serve as the indices.

1234567
import matplotlib.pyplot as plt import pandas as pd weather_df = pd.read_csv('https://staging-content-media-cdn.codefinity.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

Second Option

In this example, the plot() function is called only once. Since markers are specified for both data series, matplotlib interprets them as two separate plots and uses the Series indices as the x-axis values.

If markers are not specified, the function creates only a single plot, using the first pandas Series for the x-axis and the second for the y-axis.

123456
import matplotlib.pyplot as plt import pandas as pd weather_df = pd.read_csv('https://staging-content-media-cdn.codefinity.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

Third Option

Another way to create multiple line plots in a single call is to pass the entire DataFrame directly to the plot() function.

In this case, matplotlib automatically treats each column in the DataFrame as a separate line plot. The index of the DataFrame is used for the x-axis, and the values of each column are plotted on the y-axis.

This approach is convenient when you want to quickly visualize multiple features across a common index (such as time or categories), without manually calling plot() for each one.

123456
import matplotlib.pyplot as plt import pandas as pd weather_df = pd.read_csv('https://staging-content-media-cdn.codefinity.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/weather_data.csv', index_col=0) # Calling the plot() function for whole DataFrame plt.plot(weather_df, '-o') plt.show()
copy
Note
Study More

Feel free to explore even more about line plots with plot() function documentation.

Task

Swipe to start coding

  1. Use the correct function to create a 2 line plots.
  2. Pass data_linear as an argument in the first plot function, do not use any markers.
  3. Pass data_squared as an argument in the second function, use 'o' markers with solid line.

Solution

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