python bokeh以交互方式绘制最小和最大之间的n条曲线

问题描述 投票:0回答:1

我试图生成两个参数函数的图,其中一个用作x_axis,另一个用于绘制n条曲线,在最小值和最大值之间改变参数。

以下代码有效:

import numpy as np
import bokeh
from bokeh.plotting import figure
from bokeh.io import push_notebook, show, output_notebook
output_notebook()
x = np.linspace(0,10,100)
f = figure()

fmin=1
fmax=3
nfreq=4

freq=np.linspace(fmin,fmax,nfreq)

for i in freq:
    y = np.sin(i*x)
    f.line(x,y)
show(f)  

现在我想有3个滑块以交互方式改变fmin,fmax和nfreq。我无法弄清楚如何做到这一点......

谢谢你的帮助

python-3.x bokeh
1个回答
0
投票

此示例适用于Bokeh v1.0.4。运行方式:bokeh serve --show app.py app.py的内容:

import numpy as np
from bokeh.models import Slider, Row, Column
from bokeh.plotting import figure, show, curdoc
from bokeh.models.sources import ColumnDataSource

plot = figure()
layout = Column(plot)
sources, lines = {}, {}

def get_x(n): return [np.linspace(0, 10, 100) for i in range(n)]
def get_y(n): return [np.sin(i * np.linspace(0, 10, 100)) for i in n]

def update(attr, old, new): 
    update_sources(layout.children[-3].value, layout.children[-2].value, layout.children[-1].value)

def update_sources(fmin, fmax, nfreq):
    freq = np.linspace(fmin, fmax, nfreq)
    for f, x, y in zip(freq, get_x(len(freq)), get_y(freq)):
        data = {'x': x, 'y': y}
        if f not in sources:
            sources[f] = ColumnDataSource(data)
            line = plot.line('x', 'y', source = sources[f])
            lines[f] = line
        else:
            sources[f].data = data
    for line in lines:
        lines[line].visible = (False if line not in freq else True)

for txt, max in zip(['fmin', 'fmax', 'nfreq'], [3, 4, 5]):
    slider = Slider(start = 1, end = max, value = 1, title = txt)
    slider.on_change('value', update)
    layout.children.append(slider)

update_sources(layout.children[-3].value, layout.children[-2].value, layout.children[-1].value)
[plot.line('x', 'y', source = sources[idx]) for idx in sources]
curdoc().add_root(layout)
© www.soinside.com 2019 - 2024. All rights reserved.