动态改变散景图的形状
Dynamically change the shape of bokeh Figure
我正在构建一个 Web 应用程序,它将显示图像作为数据分析管道的一部分。为此,我需要在散景中动态更改 Figure
对象的宽度和高度。
使用下面的代码,改变了Figure
的形状,但是这个改变只有在我调整了我的浏览器window之后才生效,即使浏览器window调整了大小永远那么小。
import bokeh.plotting
import bokeh.models
import bokeh.layouts
# set up the interface
fig1 = bokeh.plotting.figure()
button = bokeh.models.Button(label='scramble')
# define a callback and connect it
def callback():
fig1.width = int(fig1.width * .8)
button.on_click(callback)
# add everything to the document
bokeh.plotting.curdoc().add_root(bokeh.layouts.column(button, fig1))
是否有一些我需要的更新方法运行?我读过 "next tick callbacks" 但我不明白这是否相关。
我的 gnome 系统上的 firefox 和 chromium 都会出现上述行为。
有一种方法可以使用内置功能动态调整散景图的大小。例如,
fig = plotting.figure(width=1200, height=900, title="Dynamic plot".format(chartType), sizing_mode='scale_width')
关键选项是sizing_mode='scale_width'
width
和height
命令作为初始值。 sizing_mode
还有其他选项,所以我会研究一下。
发生这种情况的原因是布局未更新。尽管您的代码更改了图形的 属性 值,但您必须重新计算文档解算器中的所有值才能实际调整大小。
这是 BokehJS 中发生调整大小挂钩的行:
https://github.com/bokeh/bokeh/blob/master/bokehjs/src/coffee/document.coffee#L92
在文档级别调用调整大小后,重新渲染调整大小的对象:
https://github.com/bokeh/bokeh/blob/master/bokehjs/src/coffee/models/layouts/layout_dom.coffee#L61
问题是,据我所知,目前没有公开的方法来重新触发文档调整大小事件。
但是您可以在客户端进行。这是使用 CustomJS
:
的工作代码
test.py
from bokeh.io import show
from bokeh.layouts import column
from bokeh.models import Button, CustomJS
from bokeh.plotting import figure
fig = figure()
button = Button(label='scramble')
button.callback = CustomJS(args=dict(fig=fig), code="""
var old_width = fig.width;
var doc = fig.document;
fig.width = old_width * 0.8;
doc.resize();
""")
col = column(button, fig)
show(col)
这可以是 运行 和 python test.py
。
请注意,您也可以使用散景服务器将最后一行 show(col)
替换为 curdoc().add_root(col)
,但我这样做并不是为了强调这是一个客户端解决方案。
我正在构建一个 Web 应用程序,它将显示图像作为数据分析管道的一部分。为此,我需要在散景中动态更改 Figure
对象的宽度和高度。
使用下面的代码,改变了Figure
的形状,但是这个改变只有在我调整了我的浏览器window之后才生效,即使浏览器window调整了大小永远那么小。
import bokeh.plotting
import bokeh.models
import bokeh.layouts
# set up the interface
fig1 = bokeh.plotting.figure()
button = bokeh.models.Button(label='scramble')
# define a callback and connect it
def callback():
fig1.width = int(fig1.width * .8)
button.on_click(callback)
# add everything to the document
bokeh.plotting.curdoc().add_root(bokeh.layouts.column(button, fig1))
是否有一些我需要的更新方法运行?我读过 "next tick callbacks" 但我不明白这是否相关。
我的 gnome 系统上的 firefox 和 chromium 都会出现上述行为。
有一种方法可以使用内置功能动态调整散景图的大小。例如,
fig = plotting.figure(width=1200, height=900, title="Dynamic plot".format(chartType), sizing_mode='scale_width')
关键选项是sizing_mode='scale_width'
width
和height
命令作为初始值。 sizing_mode
还有其他选项,所以我会研究一下。
发生这种情况的原因是布局未更新。尽管您的代码更改了图形的 属性 值,但您必须重新计算文档解算器中的所有值才能实际调整大小。
这是 BokehJS 中发生调整大小挂钩的行:
https://github.com/bokeh/bokeh/blob/master/bokehjs/src/coffee/document.coffee#L92
在文档级别调用调整大小后,重新渲染调整大小的对象:
https://github.com/bokeh/bokeh/blob/master/bokehjs/src/coffee/models/layouts/layout_dom.coffee#L61
问题是,据我所知,目前没有公开的方法来重新触发文档调整大小事件。
但是您可以在客户端进行。这是使用 CustomJS
:
test.py
from bokeh.io import show
from bokeh.layouts import column
from bokeh.models import Button, CustomJS
from bokeh.plotting import figure
fig = figure()
button = Button(label='scramble')
button.callback = CustomJS(args=dict(fig=fig), code="""
var old_width = fig.width;
var doc = fig.document;
fig.width = old_width * 0.8;
doc.resize();
""")
col = column(button, fig)
show(col)
这可以是 运行 和 python test.py
。
请注意,您也可以使用散景服务器将最后一行 show(col)
替换为 curdoc().add_root(col)
,但我这样做并不是为了强调这是一个客户端解决方案。