Spyder交互式图:等待图关闭以继续

问题描述:

我正在使用Spyder处理Windows,并使用matplotlib进行绘图.我的问题是我想进行交互式绘图(或有时绘图很多东西),并且希望spyder等待关闭图形以继续代码(与传统终端相同).

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).

我尝试过 plt.ion(), %mpl TkAgg 在加载matplotlib,Ipython和python控制台之前...我找不到任何解决方案.

I tried plt.ion(), %mpl TkAgg before loading matplotlib, Ipython and python console... And I can't find any solution.

如果您想举个例子,我们的目标是仅当我关闭图形时才用Windows 10的Spyder打印"hello".

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")

您需要通过以下步骤停用Spyder的Matplotlib支持:

You need to deactivate Spyder's Matplotlib support by going to

Tools > Preferences > IPython console > Graphics

并取消选择名为

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")

在创建图之前手动设置后端(在这种情况下为TkAgg).

to set your backend (TkAgg in this case) by hand before creating your plot.