Search code examples
pythonaltairvega-lite

shared interactive scales with 1D or tick plot


When a plot is either 1D or has tick marks, the domain is forced to include to origin. When I combine this plot with another 2D/non-tick plot, shared interactive scales don't work.

domain = alt.selection_interval(bind="scales", encodings=["x", "y"])

chart_1d =\
    ( alt.Chart(d1d)
    . mark_tick()
    . encode(x="values")
    . add_selection(domain)
    )

chart_2d =\
    ( alt.Chart(d2d)
    . mark_line()
    . encode(x="x", y="y")
    . add_selection(domain)
    )

chart = alt.vconcat(chart_1d, chart_2d)

I would like:

  • The domain of the 1D chart to use the domain of the 2D chart, whatever that may be.
  • Panning in the x-axis and zooming to be shared.
  • Panning in the y-axis to be disabled only for the 1D chart.

Solution

  • One easy way to make this happen is to use the same field name for both x axes:

    import altair as alt
    import pandas as pd
    
    d1d = pd.DataFrame({'x': [1, 2, 3, 4]})
    d2d = pd.DataFrame({'x': [1, 2, 3, 4], 'y': [2, 3, 2, 3]})
    
    domain = alt.selection_interval(bind="scales", encodings=["x", "y"])
    
    chart_1d =\
        ( alt.Chart(d1d)
        . mark_tick()
        . encode(alt.X("x", title='values'))
        . add_selection(domain)
        )
    
    chart_2d =\
        ( alt.Chart(d2d)
        . mark_line()
        . encode(x="x", y="y")
        . add_selection(domain)
        )
    
    chart = alt.vconcat(chart_1d, chart_2d)