Altair for timeseries

I stumbled apon a neat technique to visualise timseries better using Altair. Let's take this starting point as an example:

import numpy as np
import polars as pl
from datetime import date
import altair as alt


df = pl.DataFrame(
    pl.date_range(date(2020,1,1), date(2025, 1, 1), eager=True).alias("date")
)

pltr = df.with_columns(value=np.random.normal(0, 1, df.shape[0]).cumsum())

alt.Chart(pltr).mark_line().encode(x="date", y="value").interactive()

This is what the result will look like:

It's not bad, but try zooming in. When you do that you zoom both the x and y-axis. This is not what you want when you are dealing with timeseries. But, because altair has a nice API, you can also do this:

alt.Chart(pltr).mark_line().encode(x="date", y="value").interactive(bind_y=False)

The only difference here is that we're setting bind_y=False. This will make it so that when you zoom in, you only zoom on the x-axis. Try it out below:

So much nicer!