I am trying to display a pyplot that updates every 3 seconds with data from another thread. I need to close the plot and reopen with the updated variables. I do not need an animation, I want to close the figure and open a new one. This does not work unless I manually close the Figure window.
import numpy as np
from matplotlib import pyplot as plt
import threading as th
from time import sleep
Xplot = []
YPlot = []
def RandomXYGen():
x = np.random.normal(5, .75)
y = np.random.normal(10, 1.5)
return x, y
def LoopData():
while True:
xdata, ydata = RandomXYGen()
Xplot.append(xdata)
YPlot.append(ydata)
print(xdata, ydata)
sleep(0.1)
def Plotter(xToPlot, yToPlot):
plt.close('all')
plt.plot(xToPlot, yToPlot)
plt.show()
t = th.Thread(target=LoopData, daemon=True)
t.start()
while True:
Plotter(Xplot, YPlot)
sleep(3)
Any ideas why it is not closing?
[python 3.10.8 on Windows 10]
After many attempts on figuring it out I was finally able to get help from the Python Discord server. This is what solves the above problem:
plt.show(block=False)
plt.pause(0.01)
instead of:
plt.show()
Leaving this here in case someone runs into the same issue as me.