On Linux, you can turn off display as follows:
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():
The main process ends, but the schedule remains.
Attempt No. 2. It also works on Linux; , you can try: but not on OS X.
import matplotlib.pyplot as plt import matplotlib.mlab as mlab import numpy as np import os import multiprocessing as mp def detach_display(): mu, sigma = 0, 0.5 x = np.linspace(-3, 3, 100) plt.plot(x, mlab.normpdf(x, mu, sigma)) plt.show() proc = mp.Process(target=detach_display) proc.start() os._exit(0)
Without os._exit(0) , the main blocks of the process. Pressing Ctrl-C kills the main process, but the plot remains.
With os._exit(0) main process ends, but the graph remains.
Sigh. Attempt number 3. If you put your matplotlib calls in another script, you can use the subprocess as follows:
show.py:
import matplotlib.pyplot as plt import numpy as np import sys filename = sys.argv[1] data = np.load(filename) plt.plot(data['x'], data['y']) plt.show()
test.py
import subprocess import numpy as np import matplotlib.mlab as mlab mu, sigma = 0, 0.5 x = np.linspace(-3, 3, 100000) y = mlab.normpdf(x, mu, sigma) filename = '/tmp/data.npz' np.savez(filename, x=x, y=y) proc = subprocess.Popen(['python', '/path/to/show.py', filename])
Running test.py should display the graph and control the return to the terminal, leaving the displayed graph.