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.
-
Initialize a scatter plot with the next parameters:
- Set points color (
c
) to the'hdi'
column values of thedata
; - Set colormap (
cmap
) to'plasma'
.
Save the call of the
.scatter()
function to thecax
variable. - Set points color (
-
Apply
.colorbar()
function toFigure
object assigningcax
as an argument.
Solution
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 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?
Thanks for your feedback!
Section 3. Chapter 10
single
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 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
Ask AI
Ask anything or try one of the suggested questions to begin our chat