Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
holoviz
GitHub Repository: holoviz/panel
Path: blob/main/examples/apps/fastApi_multi_apps/sliders2/sinewave.py
2014 views
1
import numpy as np
2
import param
3
4
from bokeh.models import ColumnDataSource
5
from bokeh.plotting import figure
6
7
8
class SineWave(param.Parameterized):
9
offset = param.Number(default=0.0, bounds=(-5.0, 5.0))
10
amplitude = param.Number(default=1.0, bounds=(-5.0, 5.0))
11
phase = param.Number(default=0.0, bounds=(0.0, 2 * np.pi))
12
frequency = param.Number(default=1.0, bounds=(0.1, 5.1))
13
N = param.Integer(default=200, bounds=(0, None))
14
x_range = param.Range(default=(0, 4 * np.pi), bounds=(0, 4 * np.pi))
15
y_range = param.Range(default=(-2.5, 2.5), bounds=(-10, 10))
16
17
def __init__(self, **params):
18
super().__init__(**params)
19
x, y = self.sine()
20
self.cds = ColumnDataSource(data=dict(x=x, y=y))
21
self.plot = figure(height=400, width=400,
22
tools="crosshair, pan, reset, save, wheel_zoom",
23
x_range=self.x_range, y_range=self.y_range)
24
self.plot.line('x', 'y', source=self.cds, line_width=3, line_alpha=0.6)
25
26
@param.depends('N', 'frequency', 'amplitude', 'offset', 'phase', 'x_range', 'y_range', watch=True)
27
def update_plot(self):
28
x, y = self.sine()
29
self.cds.data = dict(x=x, y=y)
30
self.plot.x_range.start, self.plot.x_range.end = self.x_range
31
self.plot.y_range.start, self.plot.y_range.end = self.y_range
32
33
def sine(self):
34
x = np.linspace(0, 4 * np.pi, self.N)
35
y = self.amplitude * np.sin(self.frequency * x + self.phase) + self.offset
36
return x, y
37
38