Matplotlib - 强制绘图显示,然后返回主代码

问题描述 投票:0回答:4

这是我所追求的MWE,改编自这个问题

from matplotlib.pyplot import plot, draw, show

def make_plot():
    plot([1,2,3])
    draw()
    print 'continue computation'

print('Do something before plotting.')
# Now display plot in a window
make_plot()

answer = raw_input('Back to main and window visible? ')
if answer == 'y':
    print('Excellent')
else:
    print('Nope')

show()

我想要的是:我调用函数来绘图,出现绘图窗口,然后我返回到提示,这样我就可以输入一些值(基于刚刚显示的图像)并继续代码(窗口可以关闭或保留在那里,我不在乎)。

我得到的是带有绘图的窗口仅在代码完成后出现,这不好。


添加1

我尝试了以下方法,得到了相同的结果,绘图窗口出现在代码末尾而不是之前:

from matplotlib.pyplot import plot, ion, draw ion() # enables interactive mode plot([1,2,3]) # result shows immediately (implicit draw()) # at the end call show to ensure window won't close. draw() answer = raw_input('Back to main and window visible? ') if answer == 'y': print('Excellent') else: print('Nope')

如果我将

draw()

 更改为 
show()
,也会发生同样的情况。


添加2

我尝试过以下方法:

from multiprocessing import Process from matplotlib.pyplot import plot, show def plot_graph(*args): for data in args: plot(data) show() p = Process(target=plot_graph, args=([1, 2, 3],)) p.start() print 'computation continues...' print 'Now lets wait for the graph be closed to continue...:' p.join()

这会导致

Python kernel has crashed

 中出现 
Canopy
 错误,并显示以下消息:

The kernel (user Python environment) has terminated with error code -6. This may be due to a bug in your code or in the kernel itself. Output captured from the kernel process is shown below. [IPKernelApp] To connect another client to this kernel, use: [IPKernelApp] --existing /tmp/tmp9cshhw.json QGtkStyle could not resolve GTK. Make sure you have installed the proper libraries. [xcb] Unknown sequence number while processing queue [xcb] Most likely this is a multi-threaded client and XInitThreads has not been called [xcb] Aborting, sorry about that. python: ../../src/xcb_io.c:274: poll_for_event: La declaración `!xcb_xlib_threads_sequence_lost' no se cumple.

我应该提一下,我正在

Canopy

 运行 
elementary OS
,它基于 
Ubuntu 12.04


添加3

还尝试了

这个问题中发布的解决方案:

import numpy from matplotlib import pyplot as plt if __name__ == '__main__': x = [1, 2, 3] plt.ion() # turn on interactive mode for loop in range(0,3): y = numpy.dot(x, loop) plt.figure() plt.plot(x,y) plt.show() _ = raw_input("Press [enter] to continue.")

随着代码前进(即:用户点击 [enter]),这会显示空的绘图窗口,并且仅在代码完成后显示图像。

此解决方案(也在同一问题中)甚至不显示绘图窗口:

import numpy from matplotlib import pyplot as plt if __name__ == '__main__': x = [1, 2, 3] plt.ion() # turn on interactive mode, non-blocking `show` for loop in range(0,3): y = numpy.dot(x, loop) plt.figure() # create a new figure plt.plot(x,y) # plot the figure plt.show() # show the figure, non-blocking _ = raw_input("Press [enter] to continue.") # wait for input from the user plt.close() # close the figure to show the next one.
    
python matplotlib enthought
4个回答
28
投票
你可以使用

plt.show(block=False)

,直接解除阻塞。

对于您的示例,这可以阅读

from matplotlib.pyplot import plot, show def make_plot(): plot([1,2,3]) show(block=False) print('continue computation') print('Do something before plotting.') # Now display plot in a window make_plot() answer = input('Back to main and window visible? ') if answer == 'y': print('Excellent') else: print('Nope')
    

14
投票
所提出的解决方案都不适合我。我使用 Python 3.6 下(当前)最新的 Matplotlib 2.1,使用三种不同的 IDE

PyCharmSpyderPyzo 对它们进行了测试。

对我有用的,虽然不是最佳的,是使用

plt.pause

 命令:

import matplotlib.pyplot as plt def make_plot(): plt.plot([1, 2, 3]) # plt.show(block=False) # The plot does not appear. # plt.draw() # The plot does not appear. plt.pause(0.1) # The plot properly appears. print('continue computation') print('Do something before plotting.') # Now display plot in a window make_plot() answer = input('Back to main and window visible? ') if answer == 'y': print('Excellent') else: print('Nope')
    

1
投票
我无法让它与

Canopy

 一起工作(至少现在还不能),但我可以让代码运行起来,就像我想要使用 
Geany
 IDE 一样。这是对我有用的代码,这是对问题中第一段代码的一个非常小的修改,其中 
show()
 命令从文件末尾移动到 
make_plot()
 命令的正下方:

from matplotlib.pyplot import plot, draw, show def make_plot(): plot([1,2,3]) draw() print 'Plot displayed, waiting for it to be closed.' print('Do something before plotting.') # Now display plot in a window make_plot() # This line was moved up <---- show() answer = raw_input('Back to main after plot window closed? ') if answer == 'y': print('Move on') else: print('Nope')

它并没有完全达到我想要的效果,但它已经足够接近了:它向用户显示一个绘图,等待绘图窗口关闭,然后继续执行代码。理想情况下,它不必等到绘图窗口关闭才能继续执行代码,但我想这总比没有好。

上面

Add 2 部分中的代码也以相同的方式工作,并且不需要在 Geany

 中进行修改,但我更喜欢这个,因为它更简单。我会更新这个答案如果(什么时候?)我让它与
Canopy
一起工作。


0
投票
这对我有用,尽管不是最佳的。我失去了互动性,我什至无法关闭剧情!正在寻找解决方案...

#!/usr/bin/env python3 import matplotlib.pyplot as plt def make_plot(): plt.plot([1, 2, 3]) plt.show(block=False) input('Back to main and window visible? ') #Here is the trick print('continue computation') print('Do something before plotting.') # Now display plot in a window make_plot() answer = input('Quit now ?') if answer == 'y': print('Excellent') else: print('Nope')
    
© www.soinside.com 2019 - 2024. All rights reserved.