Altair candlestick chart not plotting interactively

I am trying to plot an interactive financial chart, but it doesn’t turn dynamic when I use st.altair_chart
Here’s the code:

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)

Here is the output plot:
image

I understood the issue, you have to explicitly layer the charts (using alt.layer) and call on interactive() to actually make it interactive!
Basically:

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)
1 Like

Glad you got this to work, @Pk13055! Btw, that’s a very pretty chart! :slightly_smiling_face: