8000 Qt embedding example: Separate drawing and data retrieval timers by doronbehar · Pull Request #28589 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Qt embedding example: Separate drawing and data retrieval timers #28589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions galleries/examples/user_interfaces/embedding_in_qt_sgskip.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,34 @@ def __init__(self):
self._static_ax.plot(t, np.tan(t), ".")

self._dynamic_ax = dynamic_canvas.figure.subplots()
t = np.linspace(0, 10, 101)
# Set up a Line2D.
self._line, = self._dynamic_ax.plot(t, np.sin(t + time.time()))
self._timer = dynamic_canvas.new_timer(50)
self._timer.add_callback(self._update_canvas)
self._timer.start()
self.xdata = np.linspace(0, 10, 101)
self._update_ydata()
self._line, = self._dynamic_ax.plot(self.xdata, self.ydata)
# The below two timers must be attributes of self, so that the garbage
# collector won't clean them after we finish with __init__...

def _update_canvas(self):
t = np.linspace(0, 10, 101)
# The data retrieval may be fast as possible (Using QRunnable could be
# even faster).
self.data_timer = dynamic_canvas.new_timer(1)
self.data_timer.add_callback(self._update_ydata)
self.data_timer.start()
# Drawing at 50Hz should be fast enough for the GUI to feel smooth, and
# not too fast for the GUI to be overloaded with events that need to be
# processed while the GUI element is changed.
self.drawing_timer = dynamic_canvas.new_timer(20)
self.drawing_timer.add_callback(self._update_canvas)
self.drawing_timer.start()

def _update_ydata(self):
# Shift the sinusoid as a function of time.
self._line.set_data(t, np.sin(t + time.time()))
self._line.figure.canvas.draw()
self.ydata = np.sin(self.xdata + time.time())

def _update_canvas(self):
self._line.set_data(self.xdata, self.ydata)
# It should be safe to use the synchronous draw() method for most drawing
# frequencies, but it is safer to use draw_idle().
self._line.figure.canvas.draw_idle()


if __name__ == "__main__":
Expand Down
0