Add Information to the Plot
Oppgave
Swipe to start coding
-
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'
.
- set the x-axis label (
-
Assign
fig.colorbar()
to thecbar
variable. -
Set the colormap legend label to
'HDI'
(by applying the.ax.set_xlabel()
to thecbar
).
Løsning
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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?
Takk for tilbakemeldingene dine!
Seksjon 3. Kapittel 12
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår