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 Β°F) of Seattle and Boston:
12345import pandas as pd url = 'https://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())
Two line plots will be used to compare data from Seattle and Boston.
First Option
Call plot() twice to draw two separate line plots on the same Axes.
The Series indices (years) automatically become the x-axis values for both lines.
1234567import matplotlib.pyplot as plt import pandas as pd weather_df = pd.read_csv('https://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()
Second Option
Here plot() is called once. Because both series have markers, matplotlib treats them as two separate plots, again using their indices for the x-axis.
If no markers are given, plot() draws only one line, using the first Series as x and the second as y.
123456import matplotlib.pyplot as plt import pandas as pd weather_df = pd.read_csv('https://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()
Third Option
You can also pass the entire DataFrame to plot().
Each column becomes a separate line, and the DataFrameβs index is used for the x-axis.
This is a quick way to visualize multiple time series or features without calling plot() repeatedly.
123456import matplotlib.pyplot as plt import pandas as pd weather_df = pd.read_csv('https://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()
Feel free to explore even more about line plots with plot() function documentation.
Swipe to start coding
- Use the correct function to create a 2 line plots.
- Pass
data_linearas an argument in the first plot function, do not use any markers. - Pass
data_squaredas an argument in the second function, use'o'markers with solid line.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 3.85
Multiple Line Plots
Swipe to show menu
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 Β°F) of Seattle and Boston:
12345import pandas as pd url = 'https://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())
Two line plots will be used to compare data from Seattle and Boston.
First Option
Call plot() twice to draw two separate line plots on the same Axes.
The Series indices (years) automatically become the x-axis values for both lines.
1234567import matplotlib.pyplot as plt import pandas as pd weather_df = pd.read_csv('https://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()
Second Option
Here plot() is called once. Because both series have markers, matplotlib treats them as two separate plots, again using their indices for the x-axis.
If no markers are given, plot() draws only one line, using the first Series as x and the second as y.
123456import matplotlib.pyplot as plt import pandas as pd weather_df = pd.read_csv('https://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()
Third Option
You can also pass the entire DataFrame to plot().
Each column becomes a separate line, and the DataFrameβs index is used for the x-axis.
This is a quick way to visualize multiple time series or features without calling plot() repeatedly.
123456import matplotlib.pyplot as plt import pandas as pd weather_df = pd.read_csv('https://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()
Feel free to explore even more about line plots with plot() function documentation.
Swipe to start coding
- Use the correct function to create a 2 line plots.
- Pass
data_linearas an argument in the first plot function, do not use any markers. - Pass
data_squaredas an argument in the second function, use'o'markers with solid line.
Solution
Thanks for your feedback!
single