Spyder interactive plot : wait for the plot to be closed to continue

南笙酒味 提交于 2019-12-11 07:07:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!