From 1c202a433d92ee0856c21ce9890f4d336a53255f Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 17 Nov 2022 09:48:29 -0500 Subject: [PATCH] Backport PR #24481: Fix floating-point drift in oscilloscope example --- examples/animation/strip_chart.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/examples/animation/strip_chart.py b/examples/animation/strip_chart.py index 5e3b8fea0d04..f06413c28661 100644 --- a/examples/animation/strip_chart.py +++ b/examples/animation/strip_chart.py @@ -26,13 +26,16 @@ def __init__(self, ax, maxt=2, dt=0.02): def update(self, y): lastt = self.tdata[-1] - if lastt > self.tdata[0] + self.maxt: # reset the arrays + if lastt >= self.tdata[0] + self.maxt: # reset the arrays self.tdata = [self.tdata[-1]] self.ydata = [self.ydata[-1]] self.ax.set_xlim(self.tdata[0], self.tdata[0] + self.maxt) self.ax.figure.canvas.draw() - t = self.tdata[-1] + self.dt + # This slightly more complex calculation avoids floating-point issues + # from just repeatedly adding `self.dt` to the previous value. + t = self.tdata[0] + len(self.tdata) * self.dt + self.tdata.append(t) self.ydata.append(y) self.line.set_data(self.tdata, self.ydata)