Bokeh interactive plot: How to sum/multiply two (or more) values from sliders. to show examples like current and voltage or soundwaves

huangapple go评论66阅读模式
英文:

Bokeh interactive plot: How to sum/multiply two (or more) values from sliders. to show examples like current and voltage or soundwaves

问题

我编写(拼合了一些片段)以下代码

mybinder

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

并希望使用这两个波的数据创建第三个波。
结果的波像电力或声音一样。

from ipywidgets import interact, interactive, fixed, interact_manual
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'

TOOLTIPS = [("x,y", "($x, $y)")] 

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")

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'])

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))

感谢您的阅读和教导。我无法发布,因为细节不足...

英文:

I wrote (copied pieces together) following code

mybinder

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

and want to use the data of the two waves to create a third wave.
The resulting wave like electrical power or sound.

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))

Thank you for reading and or teaching this to me.
I cannot post because there are less details...

答案1

得分: 0

push1()push2()函数的push_notebook()行之前,添加以下行:

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

Add the following line to both functions push1() and push2() just before the push_notebook() line:

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

huangapple
  • 本文由 发表于 2020年1月6日 18:18:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610286.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定