Интерактивный сюжет Боке: Как сложить / умножить два (или более) значения из ползунков. чтобы показать примеры, такие как ток и напряжение или звуковые волны - PullRequest
1 голос
/ 06 января 2020

Я написал (скопировал куски вместе) следующий код

mybinder

https://gist.github.com/TDUPB/be832c288ad9028148872676622e7a7a#file -interaktiveabb2-ipynb

и хочу использовать данные двух волн, чтобы создать третью волну. Получающаяся волна похожа на электрическую энергию или звук.

from ipywidgets import interact, interactive, fixed, interact_manual # An dieser Stelle und den folgenden "Import" -Aufrufen werden aus Programmbibliotheken fertiger Programmcode und dessen "Funktionen" eingebunden.
import numpy as np
from bokeh.io import push_notebook, show, output_notebook
from bokeh.plotting import figure
from bokeh.models import Span
from ipywidgets import interact, interactive, fixed, interact_manual
output_notebook()

x = np.linspace(-5, 5, 3000)
y0 = np.sin(x)

tools = 'pan', 'crosshair', 'wheel_zoom', 'box_zoom', 'reset', 'save'  #https://docs.bokeh.org/en/latest/docs/user_guide/tools.html#userguide-tools-pandrag

TOOLTIPS = [ ("(x,y)", "($x, $y)")] # Hover Tools ermöglicht das Ablesen von x,y- Werten der Datenpunkte


p1 = figure(title="2 Sensor-Signale: Harmonische Schwingung", plot_height=600, plot_width=900, y_range=(-10,10), 
           tooltips=TOOLTIPS, tools=tools, toolbar_location="below")#('pan', 'crosshair'))

p1.xaxis.axis_label = 'Zeit in sec.'
p1.yaxis.axis_label = 'Spannung in Volt'

r1 = p1.line(x, y0, color="#FF0000", line_width=2) 
r2 = p1.line(x, y0, color="#0000CD", line_width=2)

r3 = p1.line(x, r1.data_source.data['y']+ r2.data_source.data['y'])# This is not working

p1.ygrid.minor_grid_line_color = 'navy'
p1.ygrid.minor_grid_line_alpha = 0.1

p1.xgrid.minor_grid_line_color = 'navy'
p1.xgrid.minor_grid_line_alpha = 0.1

vline = Span(location=0, dimension='height', line_color='green', line_width=1)
hline = Span(location=0, dimension='width', line_color='green', line_width=1)
p1.renderers.extend([vline, hline])

def push1(Funkt_ROT="cos", Frequenz1=1, Amplitude1=1, Phasenverschiebung1=0, Offset1=0):
    if   Funkt_ROT == "sin": func1 = np.sin
    elif Funkt_ROT == "cos": func1 = np.cos
    r1.data_source.data['y'] = Amplitude1 * func1(2*np.pi*Frequenz1 * x + (Phasenverschiebung1/360)*2*np.pi) + Offset1

    push_notebook()

def push2(Funkt_BLAU="sin", Frequenz2=1, Amplitude2=1, Phasenverschiebung2=0, Offset2=0):

    if   Funkt_BLAU == "sin": func2 = np.sin
    elif Funkt_BLAU == "cos": func2 = np.cos
    r2.data_source.data['y'] = Amplitude2 * func2(2*np.pi*Frequenz2*x + (Phasenverschiebung2/360)*2*np.pi) + Offset2


    push_notebook()



show(p1, notebook_handle=True)



widget1 = interact(push1, Funkt_ROT=["sin", "cos"], Frequenz1=(1,50), Amplitude1=(0.5, 20, 0.1), Phasenverschiebung1=(0, 360, 0.01), Offset1=(0, 10, 0.05))

widget2 = interact(push2, Funkt_BLAU=["sin", "cos"], Frequenz2=(1,50), Amplitude2=(0.5, 20, 0.1), Phasenverschiebung2=(0, 360, 0.01), Offset2=(0, 10, 0.05))

Спасибо за чтение и / или обучение этому для меня. Я не могу опубликовать, потому что там меньше деталей ...

1 Ответ

0 голосов
/ 06 января 2020

Добавьте следующую строку в обе функции push1() и push2() непосредственно перед строкой push_notebook():

r3.data_source.data['y'] = r1.data_source.data['y']+ r2.data_source.data['y']
...