I am making a program with different modules, components to perform file/folder management, data acquisition, model computation, and online acquisition feedback with OpenCV. The program uses a very simple command-line interface, which core is a menu where the user can input a number from 1 to X to start the task described.
[IN] Available options:
| 1 - Do something
| 2 - Run acquisition
| 3 - Compute model
| 4 - Start something else
| 5 - Open cv2 windows
| 6 - Quit
The compute model function called when the user input 3
ends with a matplotlib
plot to let the user assess the correctness of the model.
My problem is that the plot shows up only at the program end, i.e. after the user inputs 6
to quit. The plot will not show up before, even if I add a plt.show()
or f.show()
with or without block=False
.
Is there a way to get matplotlib
to show the figure as soon as possible, or even to wait until matplotlib
shows the figure without having to execute this plot/model computation in a separate thread/process?
Reproducible example:
from matplotlib import pyplot as plt
def input_menu(options, retries=3):
print ('Available options:')
for key, value in options.items():
print (f" {key} - {value}")
selection = int(input('[IN] What do you want to do ? '))
return selection
while True:
selection = input_menu(options={1: 'Plot',
2: 'Quit'})
if selection == 1:
f, ax = plt.subplots(1, 1, figsize=(10, 10))
ax.plot([1, 2, 3])
elif selection == 2:
break
else:
break
Press 1; the menu will prompt again and the plot should appear. The plot will not appear until 2 is pressed and the program is completed. I am using spyder as an IDE. In spyder, the backend PyQT5 is selected.
As you suggested, I tried also with another backend, i.e. I used python test.py
in the terminal. This time the plot does show up as expected after 1 is pressed. No need to wait for the program to complete.