Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Group by Region | Scatter Plots
Visualization in Python with matplotlib

book
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:

  1. Iterate over pair of colors and regions, using color and region as dummy variables.
  2. Within the loop, filter the observations to a particular continent (respective value of region variable).
  3. Then, within the same loop, initialize a scatter plot of 'gdp per capita' vs 'life exp'. Set color of points to color and set the label parameter to region.
  4. Display the legend of the plot.

Soluzione

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

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 8
# 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

expand
ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

some-alt