Python Matplotlib-强制打印显示,然后返回主代码

Python Matplotlib-强制打印显示,然后返回主代码,python,matplotlib,enthought,Python,Matplotlib,Enthought,这是我所追求的MWE,改编自: 我想要的是:我调用函数进行绘图,绘图窗口出现,然后我返回到提示符,这样我可以输入一些值(基于刚才显示的图像)并继续代码(然后窗口可以关闭或保持在那里,我不在乎) 取而代之的是,只有在代码完成后,才出现带有绘图的窗口,这是不好的 加1 我尝试了以下方法,结果相同,绘图窗口显示在代码末尾,而不是之前: from matplotlib.pyplot import plot, ion, draw ion() # enables interactive mode plo

这是我所追求的MWE,改编自:

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

取而代之的是,只有在代码完成后,才出现带有绘图的窗口,这是不好的


加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')
如果我为
show()
更改
draw()
,也会发生同样的情况


加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内核崩溃
错误出现在
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
,该操作系统基于
ubuntu12.04


加3 还尝试了发布在以下位置的解决方案:

这将在代码前进时显示空打印窗口(即:用户点击[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.
您可以使用
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')

我无法让它与
corporation
一起工作(至少还没有),但我可以让代码像我想使用
Geany
IDE那样运行。这是适用于我的代码,它是对问题中的第一个代码块的一个非常小的修改,
show()
命令从文件末尾向上移动到
make\u 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')
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')
从matplotlib.pyplot导入绘图、绘制、显示
def make_plot():
绘图([1,2,3])
画()
打印“显示绘图,等待关闭”
打印('打印前执行某些操作')
#现在在窗口中显示绘图
绘制图()

#这条线向上移动了所提供的解决方案都不适合我。我使用三种不同的IDE测试了它们,并使用了(目前)Python3.6下最新的Matplotlib 2.1

对我来说,使用
plt.pause
命令是可行的,尽管不是最优的:

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

这个例子在我的例子中不起作用,绘图窗口在代码完成后仍然出现,而不是在我请求
答案
输入之前。也许这与我的IDE有关(Enthough的
Canopy v 1.0.1.1190
)?哦,是的,这个功能只是在最近的matplotlib版本中添加的。它在matplotlib 1.2下对我有效。您有更新的机会吗?我不太了解
Canopy
,但您可以通过
import matplotlib
matplotlib尝试不同的后端。使用('GTKAgg')
或任何其他后端来查看它是否工作。请注意,在导入任何其他与绘图相关的内容之前,这两行应该先完成。以下是matplotlib本身的帮助:通过谷歌搜索
matplotlib后端
也会指向以下有用的线程:回答晚了,但遗憾的是,所有后端都不起作用。我很困惑,
python
怎么可能不允许在结束前显示情节……谢谢!我知道这很旧,但除了这个,我找不到任何其他解决方案,而且这个很简单。我不得不增加时间,因为我有很多行要显示,但这是我找到的唯一有效的解决方案:-)