0

我正在尝试绘制交互式财务图表,但是当我使用它时它不会变成动态st.altair_chart的(图表的默认设置altair)。这是代码:

base = alt.Chart(df).encode(
    alt.X('Date:T', axis=alt.Axis(labelAngle=-45)),
    color=alt.condition("datum.Open <= datum.Close",
                        alt.value("#06982d"), alt.value("#ae1325"))
)

rule = base.mark_rule().encode(alt.Y('Low:Q', title='Price',
                                        scale=alt.Scale(zero=False)), alt.Y2('High:Q'))
bar = base.mark_bar().encode(alt.Y('Open:Q'), alt.Y2('Close:Q'))
st.altair_chart(rule + bar, use_container_width=True)

此代码导致绘图如下:

蜡烛图

(供参考,原始问题)

4

1 回答 1

2

基本上,您必须明确分层标记并调用.interactive()以使轴交互:

base = alt.Chart(df).encode(
    alt.X('Date:T', axis=alt.Axis(labelAngle=-45)),
    color=alt.condition("datum.Open <= datum.Close",
                        alt.value("#06982d"), alt.value("#ae1325"))
)

chart = alt.layer(
    base.mark_rule().encode(alt.Y('Low:Q', title='Price',
                                    scale=alt.Scale(zero=False)), alt.Y2('High:Q')),
    base.mark_bar().encode(alt.Y('Open:Q'), alt.Y2('Close:Q')),
).interactive()
st.altair_chart(chart, use_container_width=True)
于 2020-07-04T17:33:50.950 回答