python - Altair Hconcat - 是否可以在同一 HConCat 中为图表配置不同的轴?

标签 python altair streamlit

我正在使用 streamlit 构建一个仪表板,我想依次展示 2 个图表,使用 altair 效果很好,hconcat 函数允许我做到这一点。

import altair as alt

df1 = pd.DataFrame({'metric':list('ab'),
             'value':[8,10]})

df2 = pd.DataFrame({'metric':list('xyz'),
             'value':[5,9,7]})

chart_1 = (alt.Chart(df1).mark_bar().encode(x='metric', y='value'))
chart_2 = (alt.Chart(df2).mark_bar().encode(x='metric', y='value'))

(chart_1 | chart_2)

Output

我希望一个图表的 Y 轴位于左侧,而另一个图表的 Y 轴位于右侧,但尚未找到解决方案。配置可以在图表级别进行:

chart_2 = (alt.Chart(df2).mark_bar().encode(x='metric', y='value')).configure_axisY(orient='right')

但是使用 hconcat 函数呈现时会抛出异常:

ValueError: Objects with "config" attribute cannot be used within HConcatChart. Consider defining the config attribute in the HConcatChart object instead.

有办法做到这一点吗?

提前致谢

最佳答案

config 属性只能在图表的顶层定义,因为它本质上充当适用于最终图表所有组件的主题。

如果你想为每个子图设置不同的轴属性,全局配置不是执行此操作的地方;您可以在每个子图的轴属性中执行此操作。例如:

chart_1 = alt.Chart(df1).mark_bar().encode(
    x='metric',
    y=alt.Y('value', axis=alt.Axis(orient='left'))
)
chart_2 = alt.Chart(df2).mark_bar().encode(
    x='metric',
    y=alt.Y('value', axis=alt.Axis(orient='right'))
)

(chart_1 | chart_2)

enter image description here

关于python - Altair Hconcat - 是否可以在同一 HConCat 中为图表配置不同的轴?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60798307/

相关文章:

python - 运行时之前的 SimpleParse 非确定性语法

altair - 如何显式设置 mark_rect 的 y 范围(热图)

python-3.x - 使用回归线在 Altair 中可视化多列的快速方法

python - Streamlit 在使用 Docker 执行时向我显示 "Welcome to Streamlit"消息

python - 刽子手 : File I/O strings and lists python

python - 是否可以在 Django 中制作移动应用程序?

spacy - Streamlit云操作系统错误: [E053] Could not read config file

python - 使用 google colab 创建 Streamlit Web 应用程序进行基本图像分类 Web 应用程序时出错

python - 通过 '%' 实现 python 字符串操作的多线程安全

python - 如何强制 Altair 在特定轴上对热图(矩形)进行排序?