Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?
like image 652
AJK Avatar asked Aug 03 '26 07:08

AJK


2 Answers

Charts with different datasets can be layered together with any of the mechanisms described in the Altair documentation.

For example:

import pandas as pd
import altair as alt

df1 = pd.DataFrame({
    'times': [1, 2, 3],
    'values': [1, 5, 4],
})

df2 = pd.DataFrame({
    'times': [2, 3, 4],
    'values': [4, 2, 3],
})

chart1 = alt.Chart(df1).mark_line().encode(x='times', y='values')
chart2 = alt.Chart(df2).mark_line().encode(x='times', y='values')

chart1 + chart2

enter image description here

like image 149
jakevdp Avatar answered Aug 07 '26 01:08

jakevdp


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

like image 27
pylang Avatar answered Aug 07 '26 02:08

pylang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!