这是一个我想要的最小工作示例,改编自这个问题:
我希望的是:我调用函数绘制图形,然后图形窗口出现,并且我能回到提示符,这样我就可以输入一些值(基于刚刚显示的图像)并继续使用代码(窗口可以关闭或保留在那里,我不在意)。
实际上发生的是:只有在代码完成后才会出现包含图形的窗口,这并不好。
添加1:
我尝试了以下操作,但结果相同,即图形窗口出现在代码结尾而非之前。
如果我将
我应该提到我正在运行基于Ubuntu 12.04的elementary OS中的Canopy。
这将在代码执行过程中(即用户按下[Enter]键)显示空的图形窗口,并且仅在代码完成后显示图像。
同一问题中的另一解决方案甚至不会显示绘图窗口:
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.04的elementary OS中的Canopy。
添加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.
Canopy
不是很了解,但你可以尝试通过import matplotlib
和matplotlib.use('GTKAgg')
或其他后端来查看是否可行。请注意,在导入任何其他绘图相关内容之前,这两行代码应该放在最前面。 - David Zwickerpython
不允许在结束之前显示图表... - Gabriel