How can I accomplish `set_xlim` or `set_ylim` in Bokeh?

41,790

Solution 1

One way is to can things with a simple tuple when creating a figure:

figure(..., x_range=(left, right), y_range=(bottom, top))

But you can also set the x_range and y_range properties of a created figure directly. (I had been looking for something like set_xlim or set_ylim from matplotlib.)

from bokeh.models import Range1d

fig = make_fig()
left, right, bottom, top = 3, 9, 4, 10
fig.x_range=Range1d(left, right)
fig.y_range=Range1d(bottom, top)
show(fig)

Solution 2

As of Bokeh 2.X, it seems it is not possible to replace figure.{x,y}_range with a new instance of Range1d from DataRange1d or vice versa.

Instead one has to set figure.x_range.start and figure.x_range.end for a dynamic update.

See https://github.com/bokeh/bokeh/issues/8421 for further details on this issue.

Solution 3

Maybe a naive solution, but why not passing the lim axis as argument of your function?

import numpy
from bokeh.plotting import figure, show, output_notebook
output_notebook()

def make_fig(rows=16, cols=16,x_range=[0, 16], y_range=[0, 16], plot_width=500, plot_height=500):
    img = numpy.ones((rows, cols), dtype=numpy.uint32)
    view = img.view(dtype=numpy.uint8).reshape((rows, cols, 4))
    view[:, :, 0] = numpy.arange(256)
    view[:, :, 1] = 265 - numpy.arange(256)
    fig = figure(x_range=x_range, y_range=y_range, plot_width=plot_width, plot_height=plot_height)
    fig.image_rgba(image=[img], x=[0], y=[0], dw=[cols], dh=[rows])
    return fig
Share:
41,790

Related videos on Youtube

Brian
Author by

Brian

I am a graduate of Miami University with B.S. in electrical engineering and B.A. in mathematics and of Colorado State University with M.S. in electrical engineering. I am currently pursuing my PhD in aerospace engineering at the University of Colorado, Boulder under the direction of Dr. Yu Morton in the area of global navigation satellite systems (GNSS). If you have thoughts, questions, ideas, etc revolving around GNSS and programming, let me know! I'm #SOreadytohelp and participate.

Updated on April 10, 2020

Comments

  • Brian
    Brian about 4 years

    I create a figure in a function, e.g.

    import numpy
    from bokeh.plotting import figure, show, output_notebook
    output_notebook()
    
    def make_fig():
        rows = cols = 16
        img = numpy.ones((rows, cols), dtype=numpy.uint32)
        view = img.view(dtype=numpy.uint8).reshape((rows, cols, 4))
        view[:, :, 0] = numpy.arange(256)
        view[:, :, 1] = 265 - numpy.arange(256)
        fig = figure(x_range=[0, c], y_range=[0, rows])
        fig.image_rgba(image=[img], x=[0], y=[0], dw=[cols], dh=[rows])
        return fig
    

    Later I want to zoom in on the figure:

    fig = make_fig()
    # <- zoom in on plot, like `set_xlim` from matplotlib
    show(fig)
    

    How can I do programmatic zoom in bokeh?

  • asofyan
    asofyan over 7 years
    How to implement this on y_axis_type = 'log'? It doesn't work with that bottom and top range.
  • tommy.carstensen
    tommy.carstensen about 7 years
    Doesn't work for me. I had to do p.x_range = Range1d(x_min, x_max)
  • tommy.carstensen
    tommy.carstensen about 7 years
    Doesn't work for me. I had to do fig.x_range = Range1d(x_min, x_max)
  • bigreddot
    bigreddot about 6 years
    set was really an implementation detail, and it has since been removed. I have updated the answer.
  • Marc Compere
    Marc Compere almost 4 years
    bingo - thank you - works like a champ in 1.4.0 and Bokeh 2