Search code examples
pythonvisualizationvega-litealtair

Altair combining multiple data sets


I just recently found out about Vega/Vega-Lite and Altair and see it as a genuine contender for best python plotting tool.

The thing I am currently struggling with is to plot information from two data frames into the same chart where one or two axes are shared.

I tried things like :

plot1 = alt.Chart(df1).mark_point().encode(x = 'time:T', y = [...])[...]
plot2 = alt.Chart(df2).mark_point().encode(x = 'time:T', y = [...])[...]

and that works, but it is quite clunky and not great.

I came across the LayerChart object, but from the documentation it was not quite clear to me how to use it properly to plot multiple data sets.

  • Does someone have an example of such a chart?
  • What would I need to do to get a dual y-axis?

Solution

  • Make DRYer code by separating the chart logic in a function, then iterate.

    Given

    import pandas as pd
    import altair as alt
    
    
    df0 = pd.DataFrame(dict(times=[1, 2, 3], values=[2, 2, 7]))
    df1 = pd.DataFrame(dict(times=[2, 3, 5], values=[3, 9, 8]))
    df2 = pd.DataFrame(dict(times=[3, 6, 8], values=[2, 6, 7]))
    df3 = pd.DataFrame(dict(times=[6, 7, 9], values=[3, 2, 5]))
    

    Code

    def base_chart(df):
        """Return an Altair chart."""
        # Add lengthy chart arguments here
        base = alt.Chart(
            df,
            width=500,
            height=300,
        ).mark_line(
        ).encode(
            x="times", 
            y="values"
        )
        return base
    
    
    def layer_charts(dfs, chart_func):
        """Return a layered chart."""
    
        return alt.layer(*[chart_func(df) for df in dfs])
    

    Demo

    layer_charts([df0, df1, df2, df3], base_chart)
    

    enter image description here