Search code examples
pythonbokeh

How I put same x-axis twice in Bokeh


I want a very large plot on which will be very convinient to have the same axis top and bottom. Is there any simple way of duplicating the axis? Or I need to define a secondary axis but with the same range (I don´t like this solution because Bokeh computes the range of the axis on its own and I don´t want to re compute them)

Thanks!


Solution

  • It is easy to add extra axis to a plot in bokeh. You need to import your type of axis, here LinearAxis is used.

    If you use p.x_range and p.y_range in the definition of your new axes, then they have the same range as the original ones.

    from bokeh.plotting import figure, show, output_notebook
    from bokeh.models import LinearAxis
    output_notebook()
    
    # some data
    p = figure(width=300, height=300)
    p.line(x=[1,2,3], y=[2,3,4])
    
    # exta x axis
    p.extra_x_ranges.update({'x_above':  p.x_range})
    p.add_layout(LinearAxis(x_range_name='x_above'), 'above')
    # exta y axis
    p.extra_y_ranges.update({'y_right':  p.y_range})
    p.add_layout(LinearAxis(y_range_name='y_right'), 'right')
    
    show(p)
    

    output