|
1 | 1 | # This could be comparable to |
2 | 2 | # https://matplotlib.org/stable/gallery/animation/strip_chart.html |
3 | 3 |
|
| 4 | +from random import random |
4 | 5 | import numpy as np |
5 | 6 | from wxmplot.interactive import plot, set_data_generator |
6 | 7 |
|
7 | | -np.random.seed(19680801 // 10) |
8 | | - |
9 | 8 | class Scope: |
10 | 9 | def __init__(self, nmax=50, dt=0.1): |
11 | 10 | self.dt = dt |
12 | 11 | self.nmax = nmax |
13 | | - self.t, self.y = [], [] |
14 | | - print(nmax, dt) |
| 12 | + self.tmax = dt*nmax |
| 13 | + self.t = [] |
| 14 | + self.y = [] |
15 | 15 |
|
16 | 16 | def update(self): |
17 | | - if len(self.y) > self.nmax: |
18 | | - self.t, self.y = [], [] |
19 | | - self.t.append(self.dt*len(self.y)) |
20 | | - p = np.random.rand() |
21 | | - self.y.append(np.random.rand() if p < 0.15 else 0) |
| 17 | + n = len(self.y) |
| 18 | + if n > self.nmax: |
| 19 | + self.t, self.y, n = [0], [0], 1 |
| 20 | + self.t.append(n*self.dt) |
| 21 | + self.y.append(random() if random() < 0.15 else 0) |
22 | 22 | return [(self.t, self.y)] |
23 | 23 |
|
24 | | -NMAX, DT = 200, 0.05 |
25 | | -scope = Scope(nmax=NMAX, dt=DT) |
| 24 | +scope = Scope(nmax=200, dt=0.05) |
| 25 | + |
| 26 | +plotter = plot([0], [0], xmax=scope.tmax, ymin=-0.05, ymax=1.05, drawstyle='steps-mid') |
26 | 27 |
|
27 | | -plot([0], [0], xmax=NMAX*DT) |
28 | | -set_data_generator(scope.update) |
| 28 | +set_data_generator(scope.update, win=plotter.window) |
0 commit comments