There isn't duplicated answer because I was always blocked by solution below !!!!!!!
I want to plot a graph and still run following code without closing the graph automatically like Matlab.
I try plt.show(block=False) , it failed and appear in a small moment then close itself.
Code:
import numpy as np
import matplotlib.pyplot as plt
if __name__ == '__main__':
plt.figure(figsize=(10, 10))
plt.plot(range(5), lw=2, label='Real')
plt.title('Prediction')
plt.legend(loc="best")
plt.show(block=False)
print("---Plot graph finish---")
I try plt.draw() or interactive mode , it failed, too. Check the code below.
Code:
import numpy as np
import matplotlib.pyplot as plt
if __name__ == '__main__':
plt.figure(figsize=(10, 10))
plt.plot(range(5), lw=2, label='Real')
plt.title('Prediction')
plt.legend(loc="best")
plt.draw()
plt.show()
print("---Plot graph finish---")
Above will block until I close it.
Code:
import numpy as np
import matplotlib.pyplot as plt
if __name__ == '__main__':
plt.ion()
plt.figure(figsize=(10, 10))
plt.plot(range(5), lw=2, label='Real')
plt.title('Prediction')
plt.legend(loc="best")
plt.draw()
plt.show()
print("---Plot graph finish---")
Above will appear nothing, or it appear and disappear very fast.
My version is below:
user@ya:~/$ sudo pip freeze | grep matplotlib
matplotlib==2.2.3
user@ya:~/$ sudo pip -V
pip 18.1 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)
Environments:
I only execute script in Ubuntu ex: user@ya: python xxx.py
Distributor ID: Ubuntu
Description: Ubuntu 16.04.5 LTS
Release: 16.04
Codename: xenial
Can anyone help me? I just want to do like Matlab which won't close the plotted graph even if the script finishes.