【问题标题】:Is there a way to add a new axis to a bokeh graph after it is already being displayed?有没有办法在散景图已经显示后向它添加新轴?
【发布时间】:2021-07-16 10:57:27
【问题描述】:

我正在尝试更新在散景服务器上运行的图形。我发现您可以使用figure.extra_y_ranges = {'name':Range1d(0,10)} 然后调用figure.add_layout(LinearAxis(y_range_name='name'),'right') 将一个范围为0-10 的新y 轴添加到我的图中。但是,这仅对我有效,如果我在启动该图后立即执行此操作。然而,我想要做的是在单击按钮时添加新的 y 轴。

这是我正在运行的代码的简化版本:

from bokeh.io import curdoc
from bokeh.plotting import figure
from bokeh.models import LinearAxis, Range1d
from bokeh.models.widgets import Button
from bokeh.layouts import column, row, grid

def update_fig(fig):
    fig.extra_y_ranges = {'a':Range1d(0,10)}
    fig.add_layout(LinearAxis(y_range_name='a', axis_label='a'), 'left')

def update_axis():
    update_fig(fig)

fig = figure()
fig.circle_dot([1,2,3,4],[1,2,3,4])
update_fig(fig) # This works

value_sel = Button(label='Add y-axis')
value_sel.on_click(update_axis) # This doesn't work

layout = row(
    column(value_sel),
    grid(fig)
    )

curdoc().add_root(layout)

然后我使用bokeh serve --show example.py 运行该函数

感谢您的帮助,谢谢!

【问题讨论】:

标签: python python-3.x visualization bokeh


【解决方案1】:

我现在正在使用一种解决方法,即从布局中删除图形,然后添加第二个 y 轴,然后将图形添加回布局以防万一有人遇到同样的问题。

向我建议的更好的版本是设置不同的 y 轴和起点,然后激活/停用它们的可见性

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-02
    • 2015-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多