Course Content
Ultimate Visualization with Python
Ultimate Visualization with Python
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:
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())
We will use two line plots to compare average yearly temperatures of Seattle and Boston. Here is the first option:
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()
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:
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()
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.
Swipe to show code editor
- Use the correct function to create a line plot on the fifth line.
- Pass
data_linear
as an argument in the function on the sixth line, do not use any markers. - Use the correct function to create a line plot on the eighth line.
- Pass
data_squared
as an argument in the function on the fifth line, use'o'
markers with solid line.
Thanks for your feedback!
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:
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())
We will use two line plots to compare average yearly temperatures of Seattle and Boston. Here is the first option:
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()
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:
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()
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.
Swipe to show code editor
- Use the correct function to create a line plot on the fifth line.
- Pass
data_linear
as an argument in the function on the sixth line, do not use any markers. - Use the correct function to create a line plot on the eighth line.
- Pass
data_squared
as an argument in the function on the fifth line, use'o'
markers with solid line.
Thanks for your feedback!
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:
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())
We will use two line plots to compare average yearly temperatures of Seattle and Boston. Here is the first option:
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()
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:
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()
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.
Swipe to show code editor
- Use the correct function to create a line plot on the fifth line.
- Pass
data_linear
as an argument in the function on the sixth line, do not use any markers. - Use the correct function to create a line plot on the eighth line.
- Pass
data_squared
as an argument in the function on the fifth line, use'o'
markers with solid line.
Thanks for your feedback!
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:
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())
We will use two line plots to compare average yearly temperatures of Seattle and Boston. Here is the first option:
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()
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:
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()
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.
Swipe to show code editor
- Use the correct function to create a line plot on the fifth line.
- Pass
data_linear
as an argument in the function on the sixth line, do not use any markers. - Use the correct function to create a line plot on the eighth line.
- Pass
data_squared
as an argument in the function on the fifth line, use'o'
markers with solid line.