Add Data to the Plot
Let's improve our plot by adding data for one more country.
Compito
Swipe to start coding
Display the lines for the United States and Canada emission levels on a single chart. Follow the next steps:
- Create
Figure
andAxes
objects assigned to variablesfig
,ax
respectively. - Save within the
can
variable data forCanada
. - Use the same approach as for the
usa
data for building a new line for thecan
data.
Soluzione
# Import the libraries
import matplotlib.pyplot as plt
import pandas as pd
# Load the data
data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/ed80401e-2684-4bc4-a077-99d13a386ac7/co2.csv', index_col = 0)
# Create Figure and Axes objects
fig, ax = plt.subplots()
# Save data for the US and Canada
usa = data.loc['United States']
can = data.loc['Canada']
# Initialize the plot
ax.plot(usa.index.astype(int), usa.values)
ax.plot(can.index.astype(int), can.values)
# Display the plot
plt.show()
Tutto è chiaro?
Grazie per i tuoi commenti!
Sezione 1. Capitolo 5
# Import the libraries
import matplotlib.pyplot as plt
import pandas as pd
# Load the data
data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/ed80401e-2684-4bc4-a077-99d13a386ac7/co2.csv', index_col = 0)
# Create Figure and Axes objects
fig, ax = ___()
# Save data for the US and Canada
usa = data.loc['United States']
can = data.loc['___']
# Initialize the plot
ax.plot(usa.index.astype(int), usa.values)
ax.plot(___, ___)
# Display the plot
plt.show()