Search code examples
pythonplotlinkerbokehpandas-bokeh

Is it possible to link axes from different plots in pandas-bokeh package?


In regular Bokeh there is a nice feature of linking axes from different subplots in a grid which e.g. is very useful for zooming on multiple graphs simultaneously. But this doesn't seem to work in the pandas-bokeh package - any good solutions?


Solution

  • Yes it is.

    It is not streight forward, but you can make use of js_link of the x_range and this is not too complicated. The same of course for the y_range if wanted.

    Minimal Example

    import pandas as pd
    import pandas_bokeh as pdb
    from bokeh.plotting import output_notebook
    output_notebook()
    
    # data
    data = list(range(5))
    df = pd.DataFrame({'x':data, 'y':data[::-1]})
    
    # plots
    p1 = pdb.plot(df['x'], show_figure=False)
    p2 = pdb.plot(df['y'], show_figure=False)
    
    # linking p1 ranges to p2
    p1.x_range.js_link('start', p2.x_range, 'start')
    p1.x_range.js_link('end', p2.x_range, 'end')
    p1.y_range.js_link('start', p2.y_range, 'start')
    p1.y_range.js_link('end', p2.y_range, 'end')
    
    # linking p2 ranges to p1 vice versa
    p2.x_range.js_link('start', p1.x_range, 'start')
    p2.x_range.js_link('end', p1.x_range, 'end')
    p2.y_range.js_link('start', p1.y_range, 'start')
    p2.y_range.js_link('end', p1.y_range, 'end')
    
    pdb.plot_grid([[p1],[p2]])
    

    Comment

    In an ideal world

    p1 = pdb.plot(df['x'], show_figure=False)
    p2 = pdb.plot(df['y'], x_arnge=p1.x_range, y_range=p1.y_range, show_figure=False)
    pdb.plot_grid([[p1],[p2]])
    

    should work. This is not, because in pandas-bokeh.plot() not all keyword arguments of the figure object are evaluated. In my opinion this is a misconfiguration and I opend a ticket on GitHub to address this issue.