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

Eil*_*aer 2 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)
Run Code Online (Sandbox Code Playgroud)

输出

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

chart_2 = (alt.Chart(df2).mark_bar().encode(x='metric', y='value')).configure_axisY(orient='right')
Run Code Online (Sandbox Code Playgroud)

但是当使用 func 呈现时会抛出异常hconcat

ValueError: Objects with "config" attribute cannot be used within HConcatChart. Consider defining the config attribute in the HConcatChart object instead.
Run Code Online (Sandbox Code Playgroud)

有办法做到这一点吗?

提前致谢

jak*_*vdp 5

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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述