如何在 Bokeh 中完成 `set_xlim` 或 `set_ylim`?

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

我在函数中创建了一个图形,例如

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

稍后想放大图:

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

如何在散景中进行编程缩放?

一种方法是在创建图形时使用简单的元组:

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

但您也可以直接设置创建的图形的x_rangey_range属性。 (我一直在从 matplotlib 中寻找类似 set_xlimset_ylim 的东西。)

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)

也可以直接使用

p = Histogram(wind , xlabel= 'meters/sec', ylabel = 'Density',bins=12,x_range=Range1d(2, 16)) show(p)

也许是一个幼稚的解决方案,但为什么不将 lim 轴作为函数的参数传递呢?

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

从 Bokeh 2.X 开始,似乎无法用 DataRange1d 中的 Range1d 的新实例替换 figure.{x,y}_range,反之亦然。

相反,必须设置 figure.x_range.startfigure.x_range.end 才能进行动态更新。

有关此问题的更多详细信息,请参阅 https://github.com/bokeh/bokeh/issues/8421