3

The code below generates random data, and displays it in realtime with Matplotlib. The sliders allow the user to change the y-axis range. All of this works.

Problem: when resizing the window size or moving the window, there is a performance problem: the plot is lagging during 5 seconds (and sometimes it comes back, and then lags again), sometimes even closer to 10 seconds (in an average i5 computer).

How to troubleshoot this in matplotlib.animation?

enter image description here

import numpy as np, threading, time
import matplotlib.animation as animation, matplotlib.pyplot as plt, matplotlib.widgets as widgets
class Test():
    def generate(self):  # generate random data
        while True:
            self.realtime_y = {i: np.random.rand(100) for i in range(4)}
            self.realtime_m = {i: np.random.rand(100) for i in range(4)}
            time.sleep(0.030)
    def start_gen(self):
        threading.Thread(target=self.generate).start()
    def realtime_visualization(self):
        fig = plt.figure("Test", figsize=(10, 6))
        ax1, ax2, ax3, ax4 = fig.subplots(4)
        ax1.set_ylim(Y_AXIS_RANGE)        
        ax2.set_ylim(Y_AXIS_RANGE)
        sliders = [widgets.Slider(ax3, f"Ymin", Y_AXIS_RANGE[0], Y_AXIS_RANGE[1], valinit=Y_AXIS_RANGE[0]),
                    widgets.Slider(ax4, f"Ymax", Y_AXIS_RANGE[0], Y_AXIS_RANGE[1], valinit=Y_AXIS_RANGE[1])]        
        sliders[0].on_changed(lambda v: [ax1.set_ylim(bottom=v), ax2.set_ylim(bottom=v)])
        sliders[1].on_changed(lambda v: [ax1.set_ylim(top=v), ax2.set_ylim(top=v)])
        l1 = {}
        l2 = {}
        for i, c in enumerate(["k", "r", "g", "b"]):
            l1[i], *_ = ax1.plot(self.realtime_y[i], color=c)
            l2[i], *_ = ax2.plot(self.realtime_m[i], color=c)        
        def func(n):
            for i in range(4):
                l1[i].set_ydata(self.realtime_y[i])
                l2[i].set_ydata(self.realtime_m[i])
        self.realtime_animation = animation.FuncAnimation(fig, func, frames=None, interval=30, blit=False, cache_frame_data=False)
        plt.show()
Y_AXIS_RANGE = [-3, 3]
t = Test()            
t.start_gen()
t.realtime_visualization()

Obviously with interval=500 the problem is less visible, but the animation has less fps, which is not always possible (e.g. if we have fast-moving data).

3
  • 2
    did you test it with different GUI backends - TkAgg, QtAgg, etc.? Maybe it will work better with other GUI. Commented May 22 at 15:04
  • You're right @furas, wxAgg solves it. Are there other recommended backends? Commented May 22 at 15:25
  • @furas Do you think there is way to keep it working correctly, even with TkAgg? (We only use tk for now, and I'd like to keep number of requirements as low as possible) Commented May 22 at 15:30

2 Answers 2

3

As advised by @furas, this solves the problem indeed:

import matplotlib
matplotlib.use('wxAgg')

instead of TkAgg. Note that this requires pip install wxPython.

PS: It also works with QtAgg. So the problem comes from TkAgg.

Sign up to request clarification or add additional context in comments.

Comments

0

It runs smooth for me in qtagg, TkAgg as well as in WebAgg. So the problem does not come from TkAgg necessarily. Try and install a clean environment and if possible stick to the versions as follows.

import matplotlib
# matplotlib.use("qtAgg")
# matplotlib.use("WebAgg")
matplotlib.use("TkAgg")
backend = matplotlib.get_backend()

[...]

    def realtime_visualization(self, backend):
        fig = plt.figure(f"Test '{backend}'", figsize=(10, 6))

[...]

t.realtime_visualization(backend)

Versions via conda list matplotlib and conda list tk:

# Name                    Version                   Build  Channel
matplotlib                3.10.0          py312h2e8e312_0    conda-forge
matplotlib-base           3.10.0          py312h90004f6_0    conda-forge
tk                        8.6.13               h5226925_1    conda-forge

TkAgg version

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.