问题
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?
回答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)
回答2:
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
回答3:
you can also use it directly
p = Histogram(wind , xlabel= 'meters/sec', ylabel = 'Density',bins=12,x_range=Range1d(2, 16))
show(p)
来源:https://stackoverflow.com/questions/29294957/how-can-i-accomplish-set-xlim-or-set-ylim-in-bokeh