Group by Region
Compito
Swipe to start coding
Let's build a new scatter plot with the same data, but this time painting the points with respect to the region (the 'continent'
column). Follow the next steps:
- Iterate over pair of
colors
andregions
, usingcolor
andregion
as dummy variables. - Within the loop, filter the observations to a particular continent (respective value of
region
variable). - Then, within the same loop, initialize a scatter plot of
'gdp per capita'
vs'life exp'
. Set color of points tocolor
and set thelabel
parameter toregion
. - Display the legend of the plot.
Soluzione
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
25
# 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()
# List of colors
colors = ["r", "g", "b", "c", "m"]
regions = data.continent.unique()
# Iterate over each pair of color/region
for color, region in zip(colors, regions):
# Filter data to a certain region
temp_data = data.loc[data.continent == region]
# Add filtered points on plot
ax.scatter(temp_data['gdp per capita'], temp_data['life exp'],
c = color, label = region)
# Display the plot
plt.legend()
plt.show()
Tutto è chiaro?
Grazie per i tuoi commenti!
Sezione 3. Capitolo 8
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
25
# 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()
# List of colors
colors = ["r", "g", "b", "c", "m"]
regions = data.continent.unique()
# Iterate over each pair of color/region
for color, region in zip(___, ___):
# Filter data to a certain region
temp_data = data.___[data.continent == ___]
# Add filtered points on plot
ax.scatter(temp_data['gdp per capita'], temp_data['life exp'],
c = ___, label = ___)
# Display the plot
___
plt.show()
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione