Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Add Lables and Titles | Basics: Line Charts
Visualization in Python with matplotlib

book
Add Lables and Titles

Завдання

Swipe to start coding

  1. Add custom labels on the axes: 'Year' for the x-axis and 'CO2 emission per person (metric tonnes)' for the y-axis.
  2. Add title plot 'CO2 emissions (tonnes per person) in the USA and Canada'.
  3. Initialize legend and display the plot.

Рішення

# 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, label = 'United States')
ax.plot(can.index.astype(int), can.values, label = 'Canada')

# Set custom labels on axis
ax.set_xlabel('Year')
ax.set_ylabel('CO2 emission per person (metric tonnes)')

# Add plot title
plt.title('CO2 emissions (tonnes per person) in the USA and Canada')

# Display the plot
plt.legend()
plt.show()

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 9
# 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, label = 'United States')
ax.plot(can.index.astype(int), can.values, label = 'Canada')

# Set custom labels on axis
ax.___('___')
___.___('___')

# Add plot title
___('CO2 emissions (tonnes per person) in the USA and Canada')

# Display the plot
plt.___()
___
toggle bottom row
some-alt