我试图使用OpenCV实时绘制来自摄像机的一些数据.但是,实时绘图(使用matplotlib)似乎不起作用.
我把这个问题分成了这个简单的例子:
fig = plt.figure()
plt.axis([0, 1000, 0, 1])
i = 0
x = list()
y = list()
while i < 1000:
temp_y = np.random.random()
x.append(i)
y.append(temp_y)
plt.scatter(i, temp_y)
i += 1
plt.show()
Run Code Online (Sandbox Code Playgroud)
我希望这个例子可以单独绘制1000个点.实际发生的是窗口弹出第一个点显示(确定),然后在填充图表的其余部分之前等待循环完成.
有什么想法,为什么我没有看到一次填充一个点?
多年来,我一直在努力在matplotlib中获得高效的实时绘图,直到今天我仍然不满意.
我想要一个redraw_figure更新图形"实时"(如代码运行)的函数,并且如果我在断点处停止,将显示最新的图.
这是一些演示代码:
import time
from matplotlib import pyplot as plt
import numpy as np
def live_update_demo():
plt.subplot(2, 1, 1)
h1 = plt.imshow(np.random.randn(30, 30))
redraw_figure()
plt.subplot(2, 1, 2)
h2, = plt.plot(np.random.randn(50))
redraw_figure()
t_start = time.time()
for i in xrange(1000):
h1.set_data(np.random.randn(30, 30))
redraw_figure()
h2.set_ydata(np.random.randn(50))
redraw_figure()
print 'Mean Frame Rate: %.3gFPS' % ((i+1) / (time.time() - t_start))
def redraw_figure():
plt.draw()
plt.pause(0.00001)
live_update_demo()
Run Code Online (Sandbox Code Playgroud)
在运行代码时,绘图应该更新,并且我们应该在之后的任何断点处停止时看到最新数据redraw_figure().问题是如何最好地实施redraw_figure()
在上面的实现(plt.draw(); plt.pause(0.00001))中,它可以工作,但速度非常慢(~3.7FPS)
我可以实现它:
def redraw_figure():
plt.gcf().canvas.flush_events()
plt.show(block=False)
Run Code Online (Sandbox Code Playgroud)
并且它运行得更快(~11FPS),但是当您在断点处停止时,情节不是最新的(例如,如果我在线上放置断点t_start = ...,则不会出现第二个图). …
我正在用Python编写软件.我需要将Matplotlib时间动画嵌入到自制的GUI中.以下是有关它们的更多详细信息:
GUI也是用Python编写的,使用PyQt4库.我的GUI与您在网上可以找到的常见GUI没有太大区别.我只是继承QtGui.QMainWindow并添加一些按钮,布局,......
Matplotlib动画基于animation.TimedAnimation类.这是动画的代码:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
import matplotlib.animation as animation
class CustomGraph(animation.TimedAnimation):
def __init__(self):
self.n = np.linspace(0, 1000, 1001)
self.y = 1.5 + np.sin(self.n/20)
#self.y = np.zeros(self.n.size)
# The window
self.fig = plt.figure()
ax1 = self.fig.add_subplot(1, 2, 1)
self.mngr = plt.get_current_fig_manager()
self.mngr.window.setGeometry(50,100,2000, 800)
# ax1 settings
ax1.set_xlabel('time')
ax1.set_ylabel('raw data')
self.line1 = Line2D([], [], color='blue')
ax1.add_line(self.line1)
ax1.set_xlim(0, 1000)
ax1.set_ylim(0, 4)
animation.TimedAnimation.__init__(self, self.fig, interval=20, blit=True)
def …Run Code Online (Sandbox Code Playgroud)