Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5a31a8e

Browse files
tacaswellmeeseeksmachine
authored andcommitted
Backport PR #24481: Fix floating-point drift in oscilloscope example
1 parent 2bfbe24 commit 5a31a8e

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

examples/animation/strip_chart.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@ def __init__(self, ax, maxt=2, dt=0.02):
2626

2727
def update(self, y):
2828
lastt = self.tdata[-1]
29-
if lastt > self.tdata[0] + self.maxt: # reset the arrays
29+
if lastt >= self.tdata[0] + self.maxt: # reset the arrays
3030
self.tdata = [self.tdata[-1]]
3131
self.ydata = [self.ydata[-1]]
3232
self.ax.set_xlim(self.tdata[0], self.tdata[0] + self.maxt)
3333
self.ax.figure.canvas.draw()
3434

35-
t = self.tdata[-1] + self.dt
35+
# This slightly more complex calculation avoids floating-point issues
36+
# from just repeatedly adding `self.dt` to the previous value.
37+
t = self.tdata[0] + len(self.tdata) * self.dt
38+
3639
self.tdata.append(t)
3740
self.ydata.append(y)
3841
self.line.set_data(self.tdata, self.ydata)

0 commit comments

Comments
 (0)