Configure x-axis limits after chart creation

Viewed 41

I know I can create a chart with custom x-axis limits by, for example:

altair.Chart(source).encode(
    x=altair.X("whatever", scale=altair.Scale(domain=(left_limit, right_limit))
)

But given a chart that's been created, with all the bells and whistles on the x-axis and whatnot (my actual usage is more complicated than the simple example above), how do I readjust just the x-axis limits of the chart, without having to specify all of the bells and whistles of my x-axis again???

1 Answers

You can access and override the attributes of the chart object after creation like this:

import altair as alt
from vega_datasets import data

source = data.cars.url

chart = alt.Chart(source).mark_circle().encode(
    x=alt.X('Horsepower:Q'), #scale=alt.Scale(domain=[0, 250])),
    y='Miles_per_Gallon:Q',
)

chart

enter image description here

chart.encoding.x.scale = alt.Scale(domain=[40, 300])
chart

enter image description here

Related