I am trying to make a very simple chart where I limit the x-axis to the domain (0,168), representing the hours in a week. However, even if I use alt.Scale(domain=[0, 168])
the resulting figure only cuts of the x-axis at x = 180. Below is my example, as well as a figure of the output
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])),
alt.Y('y:Q', scale=alt.Scale(domain=[0, 4])),
)
st.altair_chart(chart.properties(width=600, height=400))
And the output is
As you can see, I am not able to limit the x-range as I would like. Can you please help?
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
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