【问题标题】:How to plot multiple Y axis using Altair on Streamlit?如何在 Streamlit 上使用 Altair 绘制多个 Y 轴?
【发布时间】:2020-10-26 01:01:43
【问题描述】:

我想知道如何在 Streamlit 上使用 Altair 绘制多个 Y 轴。

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)

上面的例子返回如下错误:

SchemaValidationError: Invalid specification altair.vegalite.v4.schema.core.FacetedEncoding->0, 验证 'type' [{'type': 'quantitative', 'field': 'age'}, {'type': 'quantitative', 'field': 'salary'}] 不是 'object' 类型

【问题讨论】:

    标签: python charts altair streamlit


    【解决方案1】:

    我自己发现了如何使用 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)
    

    【讨论】:

      猜你喜欢
      • 2020-04-26
      • 2021-09-02
      • 1970-01-01
      • 1970-01-01
      • 2021-10-06
      • 2012-07-23
      • 2013-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多