pythonchartsaltairstreamlit

How to plot multiple Y axis using Altair on Streamlit?


I am wondering how I can plot multiple Y axis using Altair on Streamlit.

import pandas as pd
import numpy as np
import altair as alt
import streamlit as st

df = pd.DataFrame({
    'name': ['brian', 'dominik', 'patricia'],
    'age': [20, 30, 40],
    'salary': [1000, 2000, 3000]
})

c = alt.Chart(df).mark_area().encode(
    x='name', y=['age', 'salary'])
st.altair_chart(c, use_container_width=True)

The above example returns the following error:

SchemaValidationError: Invalid specification altair.vegalite.v4.schema.core.FacetedEncoding->0, validating 'type' [{'type': 'quantitative', 'field': 'age'}, {'type': 'quantitative', 'field': 'salary'}] is not of type 'object'


Solution

  • I found how to do it myself using layer:

    df = pd.DataFrame({
        'name': ['brian', 'dominik', 'patricia'],
        'age': [20, 30, 40],
        'salary': [100, 200, 300]
    })
    
    a = alt.Chart(df).mark_area(opacity=1).encode(
        x='name', y='age')
    
    b = alt.Chart(df).mark_area(opacity=0.6).encode(
        x='name', y='salary')
    
    c = alt.layer(a, b)
    
    st.altair_chart(c, use_container_width=True)