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

book
Group by Continuous Variable

Task

Swipe to start coding

This time, we are goint to paint the points for the same scatter plot in respect to the 'hdi' column values.

  1. Initialize a scatter plot with the next parameters:

    • Set points color (c) to the 'hdi' column values of the data;
    • Set colormap (cmap) to 'plasma'.

    Save the call of the .scatter() function to the cax variable.

  2. Apply .colorbar() function to Figure object assigning cax as an argument.

Solution

# 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')

# Display the legend for colormap
fig.colorbar(cax)

# Display the plot
plt.show()

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 10
single

single

# 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
___ = ax.scatter(data['gdp per capita'], data['life exp'],
___ = data['___'], ___ = ___)

# Display the legend for colormap
___.___(___)

# Display the plot
plt.show()

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

some-alt