【问题标题】:Altair Hconcat - It it possible to configure different axes for charts in same HConCat?Altair Hconcat - 是否可以在同一个 HConCat 中为图表配置不同的轴?
【发布时间】:2020-07-03 00:22:54
【问题描述】:

我正在使用streamlit 构建仪表板,我想呈现两个图表并排显示,使用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 func 呈现时会引发异常:

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

有没有办法做到这一点?

提前致谢

【问题讨论】:

    标签: python altair streamlit


    【解决方案1】:

    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)
    

    【讨论】:

      猜你喜欢
      • 2020-06-05
      • 1970-01-01
      • 2021-10-06
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 2012-07-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多