Create a Simple Line Chart
Compito
Swipe to start coding
Build a line chart representing the level of CO2 emissions (metric tonnes of CO2 per person) for the US. Follow the next steps:
- Import the
matplotlib.pyplot
under theplt
alias. - Save the
United States
data in theusa
variable. - Create
Figure
andAxes
objects using the.subplots()
method ofplt
and assign them to variablesfig
, andax
, respectively. - Initialize a line chart:
- display years on the x-axis (use the
.index
attribute), convert them to integers (int
); - display level of emissions on the y-axis (use the
.values
attribute).
- display years on the x-axis (use the
- Display the plot.
Once you've completed this task, click the button below the code to check your solution.
Soluzione
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 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)
# Filter the data
usa = data.loc["United States"]
# Create Figure and Axes objects
fig, ax = plt.subplots()
# Initialize the plot
ax.plot(usa.index.astype(int), usa.values)
# Display the plot
plt.show()
Disclaimer: FREE DATA FROM WORLD BANK VIA GAPMINDER.ORG, CC-BY LICENSE.
Tutto è chiaro?
Grazie per i tuoi commenti!
Sezione 1. Capitolo 3
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Import the libraries
import ___
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)
# Filter the data
usa = data.loc["___"]
# Create Figure and Axes objects
fig, ax = ___
# Initialize the plot
ax.___(usa.___.astype(___), usa.___)
# Display the plot
plt.___()
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione