Calmcode - altair: introduction

Introduction and Installation of Altair in Python.

1 2 3 4 5 6 7 8 9

There are many visualisation libraries in python out there and altair offers some original features. It offers a wide variety of charts, a grammar-like api, loads of interactivity features and the option of exporting directly to the browser.

Install altair & friends

For this video you'll need to install the following dependencies;

python -m pip install jupyterlab pandas altair

You'll also need the dataset, it can be downloaded directly or fetched via;

wget https://calmcode.io/datasets/birthdays.csv

Clean Data

The python code that cleans the dataset is shown belo.

import pathlib
import pandas as pd
import altair as alt

df = pd.read_csv("birthdays.csv")

def clean_dataset(dataf):
    return (dataf
            .assign(date = lambda d: pd.to_datetime(d['date']))
            .assign(yday = lambda d: d['date'].dt.dayofyear)
            .groupby(['date', 'wday', 'yday'])
            .agg(births = ('births', 'sum'), month=('month', 'first'))
            .reset_index()
            .head(1000))

plot_df = df.pipe(clean_dataset)