Setting fixed x-axis using Scale does not work

Hey @quba9210,

For continuous scales there is a nice attribute which rounds the scale extent on “nice round” values, so the 168 in your scale becomes 180 I guess.

Anyway you can disable this by setting nice to False :

df = pd.DataFrame({
        'x': [20, 40],
        'y': [2, 2]
    })

chart = alt.Chart(df).mark_line().encode(
    alt.X('x:Q', scale=alt.Scale(domain=[0, 168], nice=False)),
    alt.Y('y:Q', scale=alt.Scale(domain=[0, 4])),
)
st.altair_chart(chart.properties(width=600, height=400)) 

Then if you need there’s some tinkering to do on the label/tick level of the axis to have the 168 appear and maybe a tick per 24 hour :wink:

df = pd.DataFrame({
        'x': [20, 40],
        'y': [2, 2]
    })

chart = alt.Chart(df).mark_line().encode(
    alt.X('x:Q', axis=alt.Axis(values=[i*24 for i in range(8)], title="Hour of week"), scale=alt.Scale(domain=[0, 168], nice=False)),
    alt.Y('y:Q', scale=alt.Scale(domain=[0, 4])),
)
st.altair_chart(chart.properties(width=600, height=400), use_container_width=True) 

2 Likes