Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Add Information to the Plot | Scatter Plots
Visualization in Python with matplotlib

book
Add Information to the Plot

Oppgave

Swipe to start coding

  1. Using the .set() function, perform the following:

    • set the x-axis label (xlabel) to 'GDP per capita';
    • set the y-axis label (ylabel) to 'Life expectancy';
    • set the plot title (title) to 'GDP per capita vs life expectancy'.
  2. Assign fig.colorbar() to the cbar variable.

  3. Set the colormap legend label to 'HDI' (by applying the .ax.set_xlabel() to the cbar).

Løsning

# Import the libraries
import pandas as pd
import matplotlib.pyplot as plt

# Load the data
data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/ed80401e-2684-4bc4-a077-99d13a386ac7/gapminder2017.csv', index_col = 0)

# Create Figure and Axes objects
fig, ax = plt.subplots()

# Initalizing the plot
cax = ax.scatter(data['gdp per capita'], data['life exp'],
c = data['hdi'], cmap = 'plasma')

# Add labels and title
ax.set(xlabel = 'GDP per capita', ylabel = 'Life expectancy',
title = 'GDP per capita and life expectancy')

# Display the legend for colormap
cbar = fig.colorbar(cax)
cbar.ax.set_xlabel('HDI')

# Display the plot
plt.show()

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 12
# Import the libraries
import pandas as pd
import matplotlib.pyplot as plt

# Load the data
data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/ed80401e-2684-4bc4-a077-99d13a386ac7/gapminder2017.csv', index_col = 0)

# Create Figure and Axes objects
fig, ax = plt.subplots()

# Initalizing the plot
cax = ax.scatter(data['gdp per capita'], data['life exp'],
c = data['hdi'], cmap = 'plasma')

# Add labels and title
ax.___(___ = 'GDP per capita', ___ = '___',
___ = 'GDP per capita and life expectancy')

# Display the legend for colormap
___ = fig.colorbar(cax)
cbar.___.___('___')

# Display the plot
plt.show()

Spør AI

expand
ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

some-alt