python - 如何在 Streamlit 上使用 Altair 绘制多个 Y 轴?

标签 python charts altair streamlit

我想知道如何在 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:无效规范 altair.vegalite.v4.schema.core.FacetedEncoding->0,正在验证“type”[{'type': 'quantitative', 'field': 'age'}, {'type': 'quantitative', 'field': 'salary'}] 不是 'object' 类型

最佳答案

我找到了如何使用 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)

关于python - 如何在 Streamlit 上使用 Altair 绘制多个 Y 轴?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62746999/

相关文章:

python - QCombobox finData 方法始终返回 -1 与 numpy 数组

excel - VBA 图表集源数据

javascript - SVG 的 NVD3 样式

python - 如何创建饼图?

python - 如何将单个文本标签添加到 Altair 散点图中的最大点?

python - 如何使用排序的数字索引和 pandas 中的重复项迭代数据框的唯一行的列值?

python - TensorFlow 2.0 C++ - 加载预训练模型

python - 使用热着色和文本值过滤 Altair 热图

python - OpenAI 嵌入 API 错误 : "AttributeError: module ' openai' has no attribute 'Embedding' "

python - 如何使用 nbconvert 将交互式 Jupyter 笔记本转换为 html