...

/

Exercise: Trellis Chart, Column Chart, and Stacked Column Chart

Exercise: Trellis Chart, Column Chart, and Stacked Column Chart

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

We'll cover the following...

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

Python 3.10.4
import pandas as pd
df = pd.read_csv('data/average_internet_speed.csv',parse_dates=['quarter'])
print(df)

In the following terminal, represent it through a trellis chart and column chart:

Python 3.10.4
import pandas as pd
import altair as alt
import os
df = pd.read_csv('data/average_internet_speed.csv',parse_dates=['quarter'])
# drop countries for which there is only just one value
df = df[ ~ df['Country'].isin(['MDA', 'BIH','XKO', 'UA'])]
# select all countries
countries = df.Country.unique()
# split the chart in a 4 x 10 grid
start = 0
burst = 10
end = burst
charts = []
# for each row, draw a line chart
# store all the charts in a list
for i in range(4):
# select only the current countries
df_part = df[df['Country'].isin(countries[start:end])]
# TRELLIS CHART
# use mark_line() to draw the chart
# COLUMN CHART
# use mark_bark to draw the chart
#chart =
#charts.append(chart)
start = end
end = start + burst
# uncomment the following lines to show the chart
#charts1 = charts[0] & charts[1]
#charts2 = charts[2] & charts[3]
#(charts1 & charts2).save('chart.html')
#os.system('cat chart.html')

Also, represent the dataset through a stacked column chart:

Python 3.10.4
import pandas as pd
import altair as alt
import os
df = pd.read_csv('data/average_internet_speed.csv',parse_dates=['quarter'])
# drop countries for which there is only just one value
df = df[ ~ df['Country'].isin(['MDA', 'BIH','XKO', 'UA'])]
countries = df.Country.unique()
# draw a drop down menu to select a specific country
input_dropdown = alt.binding_select(options=countries, name='Country ')
selection = alt.selection_single(fields=['Country'], bind=input_dropdown, empty='none')
color = alt.condition(selection,
'Country',
alt.value('lightgray'))
# draw the chart using mark_bar()
# use add_selection(selection) to connect the selection to the chart
# chart = alt.Chart(df)
# uncomment the following lines to show the chart
#chart.save('chart.html')
#os.system('cat chart.html')

Solution: trellis chart

We’ll use the mark_line() function to ...