在matplotlib中提高重绘轮廓图的速度

UN4*_*UN4 4 python plot matplotlib contour

我有一个python程序,用于将文件中的数据绘制为该文本文件中每一行的轮廓图。目前,我的界面中有3个独立的轮廓图。不管是从文件中读取数据还是在执行脚本之前将其加载到内存中,我只能从轮廓图获得〜6fps。

我还尝试仅使用一个轮廓线和其余法线图,但是速度仅增加到7fps。我不认为画几条线在计算上很费力。有没有办法使它大大加快?理想情况下,至少要达到30fps才是好事。

我绘制轮廓的方法是,对于数据的每一行,我都删除了前一条:

for coll in my_contour[0].collections:
    coll.remove()
Run Code Online (Sandbox Code Playgroud)

并添加一个新的

my_contour[0] = ax[0].contour(x, y, my_func, [0])
Run Code Online (Sandbox Code Playgroud)

在代码的开头,我必须plt.ion()在添加绘图时对其进行更新。

任何帮助,将不胜感激。

谢谢

Imp*_*est 8

这是有关如何contour在动画中使用绘图的示例。它使用matplotlib.animation.FuncAnimation它可以很容易地打开和关闭发短信。使用blit = True时,它在我的计算机上以〜64 fps的速度运行,而不会增加〜55 fps。注意,interval当然必须允许快速动画。将其设置为interval=10(毫秒)将允许高达100 fps,但是绘图时间将其限制为比此速度慢。

import matplotlib.pyplot as plt
import matplotlib.animation
import numpy as np
import time

x= np.linspace(0,3*np.pi)
X,Y = np.meshgrid(x,x)
f = lambda x,y, alpha, beta :(np.sin(X+alpha)+np.sin(Y*(1+np.sin(beta)*.4)+alpha))**2
alpha=np.linspace(0, 2*np.pi, num=34)
levels= 10
cmap=plt.cm.magma


fig, ax=plt.subplots()
props = dict(boxstyle='round', facecolor='wheat')
timelabel = ax.text(0.9,0.9, "", transform=ax.transAxes, ha="right", bbox=props)
t = np.ones(10)*time.time()
p = [ax.contour(X,Y,f(X,Y,0,0), levels, cmap=cmap ) ]

def update(i):
    for tp in p[0].collections:
        tp.remove()
    p[0] = ax.contour(X,Y,f(X,Y,alpha[i],alpha[i]), levels, cmap= cmap) 
    t[1:] = t[0:-1]
    t[0] = time.time()
    timelabel.set_text("{:.3f} fps".format(-1./np.diff(t).mean()))  
    return p[0].collections+[timelabel]

ani = matplotlib.animation.FuncAnimation(fig, update, frames=len(alpha), 
                                         interval=10, blit=True, repeat=True)
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

请注意,在上面的动画gif中,显示的帧速率较慢,因为保存图像的过程需要更长的时间。