Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Create a Simple Line Chart | Basics: Line Charts
Visualization in Python with matplotlib

book
Create a Simple Line Chart

Tehtävä

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:

  1. Import the matplotlib.pyplot under the plt alias.
  2. Save the United States data in the usa variable.
  3. Create Figure and Axes objects using the .subplots() method of plt and assign them to variables fig, and ax, respectively.
  4. 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).
  5. Display the plot.

Once you've completed this task, click the button below the code to check your solution.

Ratkaisu

# 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.

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 3
# 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.___()
toggle bottom row
some-alt