问题
I'm working with windows using Spyder, I plot with matplotlib. My problem is that I want to do interactive plot (or sometimes plotting a lot of things) and I want spyder to wait that I close the figure to continue the code (same way as a traditional terminal would).
I tried plt.ion(), %mpl TkAgg before loading matplotlib, Ipython and python console... And I can't find any solution.
If you want an example, the goal is that the "hello" prints only when I close the figure, with Spyder on windows 10.
import matplotlib.pyplot as plt
plt.figure('Close me to get hello')
plt.plot(0,0,'*')
plt.show()
print("hello")
回答1:
You need to deactivate Spyder's Matplotlib support by going to
Tools > Preferences > IPython console > Graphics
and deselecting the option called
Activate support
Then you need to change your code like this
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
plt.figure('Close me to get hello')
plt.plot(0,0,'*')
plt.show()
print("hello")
to set your backend (TkAgg
in this case) by hand before creating your plot.
回答2:
When I run the code, the desired behaviour of the plot window blocking subsequent code from being executed is already there. So I guess there are some other settings involved. For that reason I also cannot test the following, but I would suppose that you need to turn the interactive mode off before calling show
.
import matplotlib.pyplot as plt
plt.ion() # turn interactive on, if it isn't already
plt.figure('Close me to get hello')
plt.plot(0,0,'*')
plt.draw()
# possibly do other stuff that would make the use of interactive mode useful
plt.ioff() # turn interactive off
plt.show()
print("hello")
来源:https://stackoverflow.com/questions/46529082/spyder-interactive-plot-wait-for-the-plot-to-be-closed-to-continue