【问题标题】:Bokeh plot first y-axis loses autoscale with second y-asix散景图第一个 y 轴失去了第二个 y 轴的自动缩放
【发布时间】:2019-03-29 23:43:14
【问题描述】:

一个简单的散景图,第一个 y 轴最初是自动调整的。添加第二个 y 轴后,第一个 y 轴范围受到影响。

我无法为任一 y 轴使用固定的 y 轴范围,因为事先不知道限制。我使用 AjaxDataSource 将数据更新到绘图。

下面的程序演示了这个问题。更改 y3 中的值将更改第一个 y 轴范围。

from numpy import sin
from bokeh.plotting import output_file, figure, show
from bokeh.models import LinearAxis, Range1d, DataRange1d

x = [p/100 for p in range(0, 320)]
y = sin(x).tolist()

output_file("twin_axis.html")

p = figure()
p.line(x, y, color="red")

x1 = [0, 1.0, 2.2, 3.2]
y3 = [60, 70, 70, 70]  # Changing these values affects first y-axis scale
p.extra_y_ranges = {"Yield": Range1d(start=0, end=50)}  # tried DataRange1d(), no help
p.circle(x=x1, y=y3, color="blue", y_range_name="Yield")
p.add_layout(LinearAxis(y_range_name="Yield", axis_label="Yield(%)"), 'right')

show(p)

我正在使用散景 v1.0.4。

【问题讨论】:

    标签: bokeh


    【解决方案1】:

    y_range 的默认值为"auto",因此如果您不指定任何范围,它将随您的额外范围进行缩放。解决方案是为您的绘图明确指定y_range,如下所示(适用于 Bokeh v1.0.4)

    from numpy import sin
    from bokeh.plotting import output_file, figure, show
    from bokeh.models import LinearAxis, Range1d, DataRange1d
    
    x = [p / 100 for p in range(0, 320)]
    y = sin(x).tolist()
    
    output_file("twin_axis.html")
    
    p = figure(y_range = Range1d(start = 0, end = 1))
    p.line(x, y, color = "red")
    
    x1 = [0, 1.0, 2.2, 3.2]
    y1 = [60, 70, 70, 70]  # Changing these values affects first y-axis scale
    p.extra_y_ranges = {"Yield": Range1d(start = 0, end = 50)}  # tried DataRange1d(), no help
    p.circle(x = x1, y = y1, color = "blue", y_range_name = "Yield")
    p.add_layout(LinearAxis(y_range_name = "Yield", axis_label = "Yield(%)"), 'right')
    
    show(p)
    

    结果:

    【讨论】: