...
/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:
Press + to interact
import pandas as pddf = 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:
Press + to interact
import pandas as pdimport altair as altimport osdf = pd.read_csv('data/average_internet_speed.csv',parse_dates=['quarter'])# drop countries for which there is only just one valuedf = df[ ~ df['Country'].isin(['MDA', 'BIH','XKO', 'UA'])]# select all countriescountries = df.Country.unique()# split the chart in a 4 x 10 gridstart = 0burst = 10end = burstcharts = []# for each row, draw a line chart# store all the charts in a listfor i in range(4):# select only the current countriesdf_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 = endend = 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:
Press + to interact
import pandas as pdimport altair as altimport osdf = pd.read_csv('data/average_internet_speed.csv',parse_dates=['quarter'])# drop countries for which there is only just one valuedf = df[ ~ df['Country'].isin(['MDA', 'BIH','XKO', 'UA'])]countries = df.Country.unique()# draw a drop down menu to select a specific countryinput_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()
...