Search code examples
altair

Python Altair Charts - Highlight points on a time series chart


In Altair, is there a way to draw attention of the viewer to a particular point on a time-series chart?

For example, by drawing a circle around the data point:

example

and then allowing a custom text to be displayed in a tooltip?

Thank you!


Solution

  • You can do this with a layer chart; for example:

    import altair as alt
    import pandas as pd
    import numpy as np
    
    rng = np.random.default_rng(0)
    data = pd.DataFrame({
      'x': pd.date_range('2021-01-01', freq='W', periods=52),
      'y': rng.normal(size=52).cumsum()
    })
    
    line = alt.Chart(data).mark_line().encode(
        x='x',
        y='y',
    )
    
    callout = alt.Chart(data.iloc[7:8]).mark_point(
        color='red', size=300, tooltip="Tooltip text here"
    ).encode(
        x='x',
        y='y'
    )
    
    line + callout
    

    enter image description here