无法使用 Bokeh 服务器以交互方式更新渲染器(即圆形、三角形等)大小

Unable to interactively update renderer (i.e. circle, triangle, etc.) size using Bokeh server

我可以在 Bokeh 服务器中更新渲染器的颜色和线宽,但是如果我尝试更新 "size" 什么也没有发生。您可以在颜色框中输入颜色(黑色、绿色、蓝色等),然后渲染器会更改颜色。我一直在调整 sliders.py 示例来测试它。代码如下。如果在 update_size 方法中将 glyph.size 更改为 glyph.line_width,则绘图会更新线宽,但使用 "size" 时,什么也不会发生。我正在使用 Python 2.7.12、Ubuntu 16.04、散景服务器 0.12.5 和 Tornado 4.4.2。感谢您的帮助。

import numpy as np
from bokeh.io import curdoc
from bokeh.layouts import row, widgetbox
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import Slider, TextInput
from bokeh.plotting import figure

# Set up data
N = 200
x = np.linspace(0, 4*np.pi, N)
y = np.sin(x)
source = ColumnDataSource(data=dict(x=x, y=y))

# Set up widgets
text = TextInput(title="title", value='my sine wave')
text2 = TextInput(title="size", value='6')
text3 = TextInput(title="color", value='red')
offset = Slider(title="offset", value=0.0, start=-5.0, end=5.0, step=0.1)
amplitude = Slider(title="amplitude", value=1.0, start=-5.0, end=5.0)
phase = Slider(title="phase", value=0.0, start=0.0, end=2*np.pi)
freq = Slider(title="frequency", value=1.0, start=0.1, end=5.1)

# Set up plot
plot = figure(plot_height=400, plot_width=400, title="my sine wave",
              tools="crosshair,pan,reset,save,wheel_zoom",
              x_range=[0, 4*np.pi], y_range=[-2.5, 2.5])

r = plot.circle('x', 'y', source=source, size=int(text2.value), line_alpha=0.6, color = text3.value, legend = 'test')
glyph = r.glyph

# Set up callbacks
def update_title(attrname, old, new):
    plot.title.text = text.value

text.on_change('value', update_title)

def update_size(attrname, old, new):
    glyph.size = int(text2.value)    

text2.on_change('value', update_size)

def update_color(attrname, old, new):
    glyph.fill_color = text3.value


text3.on_change('value', update_color)

def update_data(attrname, old, new):

    # Get the current slider values
    a = amplitude.value
    b = offset.value
    w = phase.value
    k = freq.value

    # Generate the new curve
    x = np.linspace(0, 4*np.pi, N)
    y = a*np.sin(k*x + w) + b

    source.data = dict(x=x, y=y)

for w in [offset, amplitude, phase, freq]:
    w.on_change('value', update_data)


# Set up layouts and add to document
inputs = widgetbox(text, text2, text3, offset, amplitude, phase, freq)
plot.legend.location = "top_left"
plot.legend.click_policy="hide"
curdoc().add_root(row(inputs, plot, width=800))
curdoc().title = "Sliders"

如果更改大小,不会立即发生任何变化。但是,如果您稍后进行另一项更改(例如擦除其中一个滑块),那么大小会更新,因此这绝对是某种错误。我建议提交 GitHub 问题:

http://github.com/bokeh/bokeh/issues

同时,将大小存储在 CDS 中解决了这个问题:

# Set up data
N = 200
x = np.linspace(0, 4*np.pi, N)
y = np.sin(x)
s = np.ones_like(y) * 6
source = ColumnDataSource(data=dict(x=x, y=y, size=s))

然后在字形调用中使用大小列:

r = plot.circle('x', 'y', size='size', source=source, 
                line_alpha=0.6, color = text3.value, legend = 'test')

最后更改回调以改为更新源:

def update_size(attrname, old, new):
    source.data['size'] = np.ones_like(y) * int(text2.value)