Interactive figures freeze up

Solved: The garbage collector removed the object I was tracking things with. I saved a reference to a global variable (a set), which is removed when the figure is closed. It works now.

I’m trying to create some interactive figures, but after a few seconds of interaction they ‘freeze up’, and allow no more interaction, sliders will not move etc. This is using the tk backend, but I’ve tried with the Qt and the same occurs.

Tested in matplotlib 3.6.3
I’ve created a minimal example below. If I move the slider around for a few seconds, it will stop moving.

import matplotlib.pyplot as plt
from matplotlib.widgets import Slider


class InteractivePlot:
    def __init__(self):
        self.figure = plt.figure()
        axslider = self.figure.add_axes([0.25, 0.1, 0.65, 0.03])
        self.slider = Slider(axslider, 'label', -1, 1)
        self.slider.on_changed(self.update)

    def update(self, value):
        return


if __name__ == "__main__":
    InteractivePlot()
    plt.show()

The slider stops getting _update() calls, but I don’t know enough to know where to look next.

You need to keep a reference to your InteractivePlot object. So the final lines should read:

if __name__ == "__main__":
    ip = InteractivePlot()
    plt.show()

and then it will no longer be garbage collected