在Python解释器中的这些指令之后,你会看到一个带有图形的窗口:

from matplotlib.pyplot import *
plot([1,2,3])
show()
# other code

不幸的是,我不知道如何在程序进行进一步计算时继续交互式地探索show()创建的图形。

这可能吗?有时计算很长,如果在检查中间结果时进行计算将会有所帮助。


当前回答

如果你在控制台工作,即IPython,你可以使用plt.show(block=False),正如在其他答案中指出的那样。但如果你很懒,你可以输入:

plt.show(0)

这是一样的。

其他回答

如果你想打开多个图形,同时保持它们全部打开,这段代码适合我:

show(block=False)
draw()

您可能需要阅读matplotlib文档中的这个文档,标题为:

在python shell中使用matplotlib

plt.figure(1)
plt.imshow(your_first_image)

plt.figure(2)
plt.imshow(your_second_image)

plt.show(block=False) # That's important 

raw_input("Press ENTER to exist") # Useful when you run your Python script from the terminal and you want to hold the running to see your figures until you press Enter

在我的例子中,我希望在计算窗口时弹出几个窗口。作为参考,方法如下:

from matplotlib.pyplot import draw, figure, show
f1, f2 = figure(), figure()
af1 = f1.add_subplot(111)
af2 = f2.add_subplot(111)
af1.plot([1,2,3])
af2.plot([6,5,4])
draw() 
print 'continuing computation'
show()

一个非常有用的matplotlib的OO接口指南。

在许多情况下,将图像保存为硬盘上的.png文件会更方便。原因如下:

优点:

在这个过程中,你可以随时打开、查看和关闭它。当应用程序运行较长时间时,这尤其方便 时间。 什么都不会弹出,你也不会被迫打开窗户。这在处理许多数字时特别方便。 您的图像可用于以后的参考,并且在关闭图形窗口时不会丢失。

缺点:

我唯一能想到的是,你将不得不去查找文件夹,并打开自己的图像。