Python 如何在 Bokeh 中完成 `set_xlim` 或 `set_ylim`?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29294957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I accomplish `set_xlim` or `set_ylim` in Bokeh?
提问by Brian
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?
如何在散景中进行程序化缩放?
采纳答案by Brian
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.)
但是您也可以直接设置创建的图形的x_range
和y_range
属性。(我一直在寻找类似set_xlim
或set_ylim
来自 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)
回答by sushmit
you can also use it directly
你也可以直接使用
p = Histogram(wind , xlabel= 'meters/sec', ylabel = 'Density',bins=12,x_range=Range1d(2, 16))
show(p)
p = Histogram(wind , xlabel= 'meters/sec', ylabel = 'Density',bins=12,x_range=Range1d(2, 16))
show(p)
回答by SeF
Maybe a naive solution, but why not passing the lim axis as argument of your function?
也许是一个天真的解决方案,但为什么不将 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
回答by Will
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.
从 Bokeh 2.X 开始,似乎无法figure.{x,y}_range
用Range1d
from的新实例替换,DataRange1d
反之亦然。
Instead one has to set figure.x_range.start
and figure.x_range.end
for a dynamic update.
相反,必须设置figure.x_range.start
并figure.x_range.end
进行动态更新。
See https://github.com/bokeh/bokeh/issues/8421for further details on this issue.
有关此问题的更多详细信息,请参阅https://github.com/bokeh/bokeh/issues/8421。