logo

... altair.


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.


Notes

Don't forget to first have your dataframe ready.

import pathlib 
import pandas as pd
import altair as alt

df = pd.read_csv("content/data/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)
            .drop(columns=['Unnamed: 0'])
            .groupby(['date', 'wday', 'yday'])
            .agg(births = ('births', 'sum'), month=('month', 'first'))
            .reset_index())

plot_df = df.pipe(clean_dataset)

The code for the plot is listed below;

(alt.Chart(plot_df)
  .mark_point(color='black')
  .encode(x='yday', y='births')
  .properties(width=600, height=300)
  .interactive())

Feedback? See an issue? Something unclear? Feel free to mention it here.

If you want to be kept up to date, consider getting the newsletter.