在Bokeh中,我如何实现`set_xlim`或`set_ylim`?

47

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

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)

我该如何在bokeh中进行程序化缩放?

4个回答

64

一种方法是在创建图形时使用一个简单的元组来对事物进行编码:

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)

1
如何在 y_axis_type = 'log' 上实现此功能?它与底部和顶部范围不兼容。 - asofyan
6
我无法运行。我必须执行 fig.x_range = Range1d(x_min, x_max) - tommy.carstensen
3
“set”实际上只是一种实现细节,而且已经被删除了。我已经更新了答案。 - bigreddot

9

从 Bokeh 2.X 开始,似乎无法将 figure.{x,y}_range 替换为来自 DataRange1d 的新实例或反之亦然。

相反,需要通过设置 figure.x_range.startfigure.x_range.end 进行动态更新。

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


bingo - 谢谢 - 在1.4.0和Bokeh 2中表现得像冠军一样。 - Marc Compere

4
也许这是一个幼稚的解决方案,但为什么不将限制轴作为函数参数传递呢?
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

0

你也可以直接使用它

p = Histogram(wind , xlabel= '米/秒', ylabel = '密度',bins=12,x_range=Range1d(2, 16)) show(p)


1
对我不起作用。我必须执行 p.x_range = Range1d(x_min, x_max) - tommy.carstensen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接