多年来,我一直在努力在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 = ...,则不会出现第二个图). …
我有一个3D数据阵列(2个空间维度和1个时间维度),我正在尝试使用matplotlib.animate生成动画轮廓图.我使用此链接作为基础:
http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/
这是我的尝试:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
from numpy import array, zeros, linspace, meshgrid
from boutdata import collect
# First collect data from files
n = collect("n") # This is a routine to collect data
Nx = n.shape[1]
Nz = n.shape[2]
Ny = n.shape[3]
Nt = n.shape[0]
fig = plt.figure()
ax = plt.axes(xlim=(0, 200), ylim=(0, 100))
cont, = ax.contourf([], [], [], 500)
# initialisation function
def init():
cont.set_data([],[],[])
return cont, …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种更新动画中轮廓线的方法,该方法不需要每次重新绘制图形。
我发现大多数对此问题的回答都让人回想起ax.contour,但是由于我的轮廓叠加在另一幅图像上,所以这太慢了。
我发现的唯一看起来很接近回答问题的答案是通过无效链接来回答:使用FuncAnimation在matplotlib中对轮廓图进行动画处理
编辑:这可能是预期的链接。
示例代码:
#!/usr/bin/env python
import matplotlib.pylab as plt
import matplotlib.animation as anim
from matplotlib.colors import LinearSegmentedColormap as lsc
import numpy
#fig = 0; ax = 0; im = 0; co = 0
image_data = numpy.random.random((100,50,50))
contour_data = numpy.random.random((100,50,50))
def init():
global fig, ax, im, co
fig = plt.figure()
ax = plt.axes()
im = ax.imshow(image_data[0,:,:])
co = ax.contour(contour_data[0,:,:])
def func(n):
im.set_data(image_data[n,:,:])
co.set_array(contour_data[n,:,:])
init()
ani = anim.FuncAnimation(fig, func, frames=100)
plt.show()
Run Code Online (Sandbox Code Playgroud)
干杯。