How do i .add_selection(interval) and bind the interval to a alt.X(domain=interval) on the same graph in Altair?

Viewed 257

I would like to use the

interval = alt.selection_interval(encodings=['x'])

on only one chart to zoom/resize into the interval I select (from mousedown to mouseup), and only to update view once I finish my brush (mouseup). However, the Altair chart updates as I am selecting the interval, and it jumps everywhere. This is an example of what I am wanting to do:

alt.Chart(weather).mark_rule(size=2).encode(
    x=alt.X('date:T', scale=alt.Scale(domain=interval.ref())),
    y='temp_min:Q',
    y2='temp_max:Q',
    color='weather:N'
).add_selection(interval)

This would enable me to select a portion of the chart, and when selection is complete, to zoom/resize into that selection using the selection interval. Thanks all!

1 Answers

If you want an interval selection to affect the axis of the chart on which the interval is drawn, you can do this by setting bind='scales':

interval = alt.selection_interval(bind='scales', encodings=['x'])

alt.Chart(weather).mark_rule(size=2).encode(
    x='date:T',
    y='temp_min:Q',
    y2='temp_max:Q',
    color='weather:N'
).add_selection(interval)

This will continuously update the attached scale as you interact with the interval.

From your question, though, it sounds like this is not the behavior you want, and you're looking for something like a box zoom. This is not supported in Altair or Vega-Lite; you can find the relevant feature request here: https://github.com/vega/vega-lite/issues/4742

Related