在Python解释器中的这些指令之后,你会看到一个带有图形的窗口:
from matplotlib.pyplot import *
plot([1,2,3])
show()
# other code
不幸的是,我不知道如何在程序进行进一步计算时继续交互式地探索show()创建的图形。
这可能吗?有时计算很长,如果在检查中间结果时进行计算将会有所帮助。
在Python解释器中的这些指令之后,你会看到一个带有图形的窗口:
from matplotlib.pyplot import *
plot([1,2,3])
show()
# other code
不幸的是,我不知道如何在程序进行进一步计算时继续交互式地探索show()创建的图形。
这可能吗?有时计算很长,如果在检查中间结果时进行计算将会有所帮助。
当前回答
在我的系统上,show()没有阻塞,尽管我希望脚本在继续之前等待用户与图形交互(并使用'pick_event'回调来收集数据)。
为了阻止执行直到绘图窗口关闭,我使用了以下方法:
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(x,y)
# set processing to continue when window closed
def onclose(event):
fig.canvas.stop_event_loop()
fig.canvas.mpl_connect('close_event', onclose)
fig.show() # this call does not block on my system
fig.canvas.start_event_loop_default() # block here until window closed
# continue with further processing, perhaps using result from callbacks
但是请注意,canvas.start_event_loop_default()产生了以下警告:
C:\Python26\lib\site-packages\matplotlib\backend_bases.py:2051: DeprecationWarning: Using default event loop until function specific to this GUI is implemented
warnings.warn(str,DeprecationWarning)
尽管脚本仍然在运行。
其他回答
在许多情况下,将图像保存为硬盘上的.png文件会更方便。原因如下:
优点:
在这个过程中,你可以随时打开、查看和关闭它。当应用程序运行较长时间时,这尤其方便 时间。 什么都不会弹出,你也不会被迫打开窗户。这在处理许多数字时特别方便。 您的图像可用于以后的参考,并且在关闭图形窗口时不会丢失。
缺点:
我唯一能想到的是,你将不得不去查找文件夹,并打开自己的图像。
您可能需要阅读matplotlib文档中的这个文档,标题为:
在python shell中使用matplotlib
虽然没有直接回答OPs的请求,但我发布了这个变通方法,因为它可能会帮助一些人在这种情况下:
我用pyinstaller创建了一个.exe,因为我不能在我需要生成图形的地方安装python,所以我需要python脚本来生成图形,将其保存为.png,关闭它并继续下一个,在循环中实现为几个图形或使用一个函数。
为此,我使用:
import matplotlib.pyplot as plt
#code generating the plot in a loop or function
#saving the plot
plt.savefig(var+'_plot.png',bbox_inches='tight', dpi=250)
#you can allways reopen the plot using
os.system(var+'_plot.png') # unfortunately .png allows no interaction.
#the following avoids plot blocking the execution while in non-interactive mode
plt.show(block=False)
#and the following closes the plot while next iteration will generate new instance.
plt.close()
其中“var”标识循环中的情节,因此它不会被覆盖。
在我的系统上,show()没有阻塞,尽管我希望脚本在继续之前等待用户与图形交互(并使用'pick_event'回调来收集数据)。
为了阻止执行直到绘图窗口关闭,我使用了以下方法:
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(x,y)
# set processing to continue when window closed
def onclose(event):
fig.canvas.stop_event_loop()
fig.canvas.mpl_connect('close_event', onclose)
fig.show() # this call does not block on my system
fig.canvas.start_event_loop_default() # block here until window closed
# continue with further processing, perhaps using result from callbacks
但是请注意,canvas.start_event_loop_default()产生了以下警告:
C:\Python26\lib\site-packages\matplotlib\backend_bases.py:2051: DeprecationWarning: Using default event loop until function specific to this GUI is implemented
warnings.warn(str,DeprecationWarning)
尽管脚本仍然在运行。
Try
import matplotlib.pyplot as plt
plt.plot([1,2,3])
plt.show(block=False)
# other code
# [...]
# Put
plt.show()
# at the very end of your script to make sure Python doesn't bail out
# before you finished examining.
show()文档说:
在非交互模式下,显示所有图形并阻塞直到图形关闭;在交互模式下,除非在从非交互模式切换到交互模式之前创建了图形,否则没有效果(不推荐)。在这种情况下,它会显示数字,但不会阻塞。 单个实验关键字参数block可以被设置为True或False以覆盖上面描述的阻塞行为。