I want to keep all my pyplot windows alive until the code itself is terminated, but without blocking. Right now, I either have to use
plt.show()
and wait until I close it before moving on.
Or use
plt.draw()
plt.pause(0.05)
where the pause is needed, as without it, for some strange reason the window is never drawn. In that case the window freezes after the pause time (checked by setting it to much longer)
What I want is matlab-like behaviour where the code will continue executing the script until the end of the script (where it will wait for user to hit enter). After hitting enter, everything should close, as with plt.close('all'), but I want the window to stay active, that is - resize, save if requested and have data cursor - just like a matlab plot would without having to close all the windows.
A complete example to illustrate my problem:
from matplotlib import pyplot as plt
from matplotlib import image as mpimg
img = mpimg.imread("/path/to/image.jpg")
plt.imshow(img)
plt.draw()
plt.pause(05.05) #window works like it should, but execution of rest of the code is frozen
input("hit enter") #the window is dead at this point
plt.close('all')
I've seen suggestion to do this (source):
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import numpy as np
import os
def detach_display():
mu, sigma = 0, 0.5
x = np.linspace(-3, 3, 100)
plt.plot(x, mlab.normpdf(x, mu, sigma))
plt.show()
if os.fork():
# Parent
pass
else:
# Child
detach_display()
It is mentioned to be linux specific, but since I'm running linux machine is not a problem, however I don't understand what exactly happens here and don't know what will happen when I use it multiple times across the code execution.
Waiting with plots until the end is not really an option, as the time between the first and last gets available is rather long and I prefer to see early on that the data selected is giving bad results, rather than running all processing to find that out.