Bokeh,Python:如何更新额外轴的范围

4

我想创建一个带有两个y轴的图表,当点击按钮时,它们的范围会更新。该脚本将在Bokeh服务器上运行。请注意,在下面的代码中,通过更改f.y_range.start/end来更新主要y轴。然而,这对于次要y轴是不可能的。我尝试使用其他两个命令,即

f.extra_y_ranges.update({"y2Range": Range1d(start=0, end=50)})

并且

f.extra_y_ranges.update = {"y2Range": Range1d(start=0, end=50)}

但它们都不起作用。
类似的问题在这里被提出:Bokeh: 如何更改额外轴的可见性
# Import libraries
from bokeh.io import curdoc
from bokeh.models import ColumnDataSource, Range1d, LinearAxis
from bokeh.models.widgets import Button
from bokeh.layouts import layout
from bokeh.plotting import figure


# Create figure
f=figure()

# Create ColumnDataSource
source = ColumnDataSource(dict(x=range(0,100),y=range(0,100)))

# Create Line
f.line(x='x',y='y',source=source)
f.extra_y_ranges = {"y2Range": Range1d(start=0, end=100)}
f.add_layout(LinearAxis(y_range_name='y2Range'), 'left')

# Update axis function
def update_axis():
    f.y_range.start = 0 
    f.y_range.end   = 50

# Create Button
button = Button(label='Set Axis')

# Update axis range on click
button.on_click(update_axis)

# Add elements to curdoc 
lay_out=layout([[f, button]])
curdoc().add_root(lay_out)
1个回答

10

我曾面临类似的问题。通过使用我创建的名称,我能够通过“extra_y_axis”字典访问其并更新次要轴的范围。对于你的情况,应该是这样的:


# Update primary axis function
def update_axis():
    f.y_range.start = 0 
    f.y_range.end   = 50   

# Update secondary axis function
def update_secondary_axis():
    f.extra_y_ranges['y2Range'].start = -20 #new secondary axis min
    f.extra_y_ranges['y2Range'].end = 80 #new secondary axis max

非常感谢!我现在遇到了一个新的问题,即如何将轴分配给线形图形。也许你有解决方法。请在这里找到描述。 - user7435037

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