Matplotlib - 强制绘图显示然后返回主代码 [英] Matplotlib - Force plot display and then return to main code

查看:98
本文介绍了Matplotlib - 强制绘图显示然后返回主代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is a MWE of what I'm after, adapted from this question:

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

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

What I want is: I call the function to make the plot, the plot window appears, and then I get to go back to the prompt so I can input some value (based on that image that just displayed) and carry on with the code (the window can then close or remain there, I don't care).

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

What I get instead is that the window with the plot only appears after the code is completed, which is no good.

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

I've tried the following with the same results, the plot window appears at the end of the code and not before:

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(),也会发生同样的情况.

The same happens if I change draw() for show().

我尝试了以下方法:

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

这会导致 Canopy 中出现 Python kernel has crashed 错误,消息为:

which results in a Python kernel has crashed error in Canopy with the message:

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.

我应该提到我在基于 Ubuntu 12.04elementary OS 中运行 Canopy.

I should mention I'm running Canopy in elementary OS which is based in Ubuntu 12.04.

还尝试了

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

This displays empty plot windows as the code advances (ie: the user hits [enter]) and only displays the images after the code is finished.

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

This solution (also in the same question) doesn't even display the plot windows:

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),它可以直接消除阻塞.

You may use plt.show(block=False), which gets rid of the blocking directly.

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

For your example, this could read

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

这篇关于Matplotlib - 强制绘图显示然后返回主代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆