...

/

Exercise: Bubble Chart and Hex Bin Chart

Exercise: Bubble Chart and Hex Bin Chart

Use the concepts you’ve learned so far to solve this exercise.

Consider the following dataset describing the average download/upload speed in European countries from 2019 to 2022:

Press + to interact
import pandas as pd
df = pd.read_csv('data/average_internet_speed.csv',parse_dates=['quarter'])
print(df)

In the following terminal, represent the data through a bubble chart:

Press + to interact
import pandas as pd
import altair as alt
import os
df = pd.read_csv('data/average_internet_speed.csv',parse_dates=['quarter'])
# build the chart
# use mark_point() to draw the points
# encode the average upload and download speeds in the x and y channels
# encode the country using the color channel.
# highlight the color of a single country (e.g. IT), using alt.condition()
# chart = alt.Chart()df
# uncomment the following lines to show the chart
#chart.save('chart.html')
#os.system('cat chart.html')

Then, represent the dataset through a hex bin chart in the following code widget:

Press + to interact
import pandas as pd
import altair as alt
import os
df = pd.read_csv('data/average_internet_speed.csv',parse_dates=['quarter'])
# split upload and download speed in 5 bins
bins = [1, 51, 102, 152, 203,254]
labels = [50, 101, 152, 203,254]
df['binned upload speed'] = pd.cut(x = df['average upload speed'], bins = bins, labels = labels, include_lowest = True)
df['binned download speed'] = pd.cut(x = df['average download speed'], bins = bins, labels = labels, include_lowest = True)
# drop null values
df.dropna(inplace=True)
# the shape of a hexagon
hexagon = "M0,-2.3094010768L2,-1.1547005384 2,1.1547005384 0,2.3094010768 -2,1.1547005384 -2,-1.1547005384Z"
size = 25
# Count of distinct x features
xFeaturesCount = 4
# Count of distinct y features
yFeaturesCount = 5
# use mark_point(shape=hexagon) to draw hexagons
# encode binned download speed in the y channel
# calculate the position on the x axis through transform_calculate()
#chart = alt.Chart(df)
# uncomment the following lines to show the chart
#chart.save('chart.html')
#os.system('cat chart.html')
...