Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python CLI程序,显示matplotlib绘图并继续使用程序_Python_Matplotlib_Command Line Interface - Fatal编程技术网

Python CLI程序,显示matplotlib绘图并继续使用程序

Python CLI程序,显示matplotlib绘图并继续使用程序,python,matplotlib,command-line-interface,Python,Matplotlib,Command Line Interface,可能重复: 我正在编写一个命令行界面python程序,用于分析一些数据。它向用户询问了一系列问题,并在脚本中的一些地方显示了matplotlib pyplot neds,但我想显示它并继续使用脚本,如下所示: import matplotlib.pyplot as plt import numpy as np plt.figure() plt.plot(np.arange(10),np.arange(10)**2) plt.show() print 'continuing the prog

可能重复:

我正在编写一个命令行界面python程序,用于分析一些数据。它向用户询问了一系列问题,并在脚本中的一些地方显示了matplotlib pyplot neds,但我想显示它并继续使用脚本,如下所示:

import matplotlib.pyplot as plt
import numpy as np

plt.figure()
plt.plot(np.arange(10),np.arange(10)**2)

plt.show()
print 'continuing the program'
我曾尝试将
plt.draw()
与子绘图一起使用,但在脚本中似乎不起作用

编辑: 我使用了
plt.ion()
除了打印窗口没有响应,放大工具等按钮没有显示之外,其他哪种工作方式都是无效的
plt.show()
在用户关闭小部件/窗口之前不会返回。我认为在很多情况下,这种行为是好的。为什么在用户花费时间查看精彩的图表时,脚本仍要继续执行?:-)但是,如果需要继续执行程序,请使用
线程化
模块。在新线程中调用
plt.show()
,并在主线程终止之前加入该线程

编辑:

看起来事情并不是那么简单。我创建了以下
test.py

import threading
from matplotlib import pyplot as p
import time

p.plot([_ for _ in xrange(5)])

t = threading.Thread(target=p.show)
t.start()

for i in xrange(5):
    print "lala %s" % i
    time.sleep(1)

print "Waiting for plot thread to finish..."
t.join()
print "Finished."
测试它会导致以下错误:

14:43:42 $ python test.py
lala 0
lala 1
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python2.6/threading.py", line 532, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.6/threading.py", line 484, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/lib/pymodules/python2.6/matplotlib/backends/backend_tkagg.py", line 73, in show
    manager.show()
  File "/usr/lib/pymodules/python2.6/matplotlib/backends/backend_tkagg.py", line 385, in show
    if not self._shown: self.canvas._tkcanvas.bind("<Destroy>", destroy)
  File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 988, in bind
    return self._bind(('bind', self._w), sequence, func, add)
  File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 938, in _bind
    needcleanup)
  File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 1101, in _register
    self.tk.createcommand(name, f)
RuntimeError: main thread is not in main loop
14:43:42$python test.py
拉拉0
拉拉一号
线程1中的异常:
回溯(最近一次呼叫最后一次):
文件“/usr/lib/python2.6/threading.py”,第532行,在引导程序内部
self.run()
文件“/usr/lib/python2.6/threading.py”,第484行,运行中
自我目标(*自我参数,**自我参数)
文件“/usr/lib/pymodules/python2.6/matplotlib/backends/backend_tkagg.py”,第73行,在show中
manager.show()
文件“/usr/lib/pymodules/python2.6/matplotlib/backends/backend_tkagg.py”,显示第385行
如果不是self._显示:self.canvas.bind(“,destroy)
文件“/usr/lib/python2.6/lib-tk/Tkinter.py”,第988行,在bind中
返回self.\u bind(('bind',self.\u w),sequence,func,add)
文件“/usr/lib/python2.6/lib-tk/Tkinter.py”,第938行,在_-bind中
(需要清理)
文件“/usr/lib/python2.6/lib tk/Tkinter.py”,第1101行,在寄存器中
self.tk.createcommand(名称,f)
运行时错误:主线程不在主循环中

我由此推断,需要从主线程调用
p.show()
。也许你必须反过来做:在另一个线程中获取用户输入。

因为我需要获取关于漂亮图形的用户输入:p线程有意义干杯