Search code examples
bokeh

bokeh: grid alignment for two plots in one row


I have a bokeh plot with two subplots in a row representing different types of data view (lines and hbars). Both subplots has the same y-axis values, total height and it's expected that grid lines representing y-ticks also will be aligned in one line. However it's not the case (see at the picture): enter image description here

I suspect that there are two ways to solve the issue: 1) find some options which will align them automatically; 2) manually control paramer value responsible for offset from x-asis to first y-tick.

However I don't know bokeh enough to go for any of such solutions. Please advise if there are some parameter to fix it or probably totally different way around. Thanks!


Solution

  • I think your #2 solution will work fine. You can manually set the y_range to be what you want. Here's an example of the same plot data in plots with y_range defined or not:

    from bokeh.plotting import figure, show
    from bokeh.layouts import Row, Column
    
    x = [0,   1,  2,  3,  4,  5,  6]
    y = [43, 45, 32, 50, 28, 30, 31]
    z = [50, 52, 35, 28, 45, 47, 43]
    
    p1 = figure(width=400, height=300)
    p1.line(x=x, y=y, color='red')
    
    p2 = figure(width=400, height=300)
    p2.line(x=x, y=z, color='blue')
    
    p3 = figure(width=400, height=300, y_range=(0, 60))
    p3.line(x=x, y=y, color='red')
    
    p4 = figure(width=400, height=300, y_range=(0, 60))
    p4.line(x=x, y=z, color='blue')
    
    row_1 = Row(p1, p2)
    row_2 = Row(p3, p4)
    col = Column(row_1, row_2)
    
    show(col)
    

    Another way to achieve a similar result without being quite so specific is to have the y_range of one refer to the other. So you could let p3 figure out what it needs the range to be, and then define p4 as:

    p4 = figure(width=400, height=300, y_range=p3.y_range)
    

    As a third alternative, you can explicity set the start/end of ranges as you need to, after the plot is defined:

    p4.y_range.start = min(your_data_list)
    p4.y_range.end = max(your_data_list)