How do i set the domain of an axis to a value that isn't a multiple of five in Altair?

Viewed 243

I'm trying to set the x-axis domain to between 0-36, as some data I'm processing was collected in 6-week increments. Following the documentation i used the scale=alt.Scale(domain=[0,36]). However, this continues to show the chart up to 40.

df = pd.DataFrame({'x':[0,6,12,18,24,30,36],'y':[0,3,1,4,2,5,3]})
alt.Chart(df).mark_line(point=True).encode(
    x=alt.X('x:Q',
            axis=alt.Axis(values=[0,6,12,18,24,30,36]),
            scale=alt.Scale(domain=[0,36])),
    y=alt.Y('y:Q'),
)

Output of code above

Changing the above code to cut off between 30 and 35 i.e., scale=alt.Scale(domain=[0,31]) generates this behavior, where the chart axis gets truncated at 30 (but shows the data after 30, appropriately since the data hasn't been clipped).

But why can't I cut off the graph at values that aren't multiples of 5?

I'm using Altair v4.0.1

1 Answers

The Vega-Lite renderer defaults to choosing "nice" values for the scale. If you want to disable this behavior, you can pass nice=False:

import pandas as pd
import altair as alt

df = pd.DataFrame({'x':[0,6,12,18,24,30,36],'y':[0,3,1,4,2,5,3]})
alt.Chart(df).mark_line(point=True).encode(
    x=alt.X('x:Q',
            axis=alt.Axis(values=[0,6,12,18,24,30,36]),
            scale=alt.Scale(domain=[0,36], nice=False)),
    y=alt.Y('y:Q'),
)

enter image description here

Related