gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 21:08:56 25 4
gpt4 key购买 nike

这是我所追求的 MWE,改编自 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()

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

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


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

这会导致 Canopy 中出现 Python kernel has crashed 错误,并显示以下消息:

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


添加 3

还尝试了 this question 中发布的解决方案:

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.

最佳答案

你可以使用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')

关于python - Matplotlib - 强制绘图显示然后返回主代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17149646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com