Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Add Data to the Plot | Basics: Line Charts
Visualization in Python with matplotlib

book
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:

  1. Create Figure and Axes objects assigned to variables fig, ax respectively.
  2. Save within the can variable data for Canada.
  3. Use the same approach as for the usa data for building a new line for the can 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?

Come possiamo migliorarlo?

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()
toggle bottom row
We use cookies to make your experience better!
some-alt