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