在matplotlib中提高重绘轮廓图的速度 [英] Increase the speed of redrawing contour plot in matplotlib

查看:106
本文介绍了在matplotlib中提高重绘轮廓图的速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a python program that plots the data from a file as a contour plot for each line in that text file. Currently, I have 3 separate contour plots in my interface. It does not matter if I read the data from a file or I load it to the memory before executing the script I can only get ~6fps from the contour plots.

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

I also tried using just one contour and the rest normal plots but the speed only increased to 7fps. I don't believe that it is so computationally taxing to draw few lines. Is there a way to make it substantially faster? Ideally, it would be nice to get at least 30fps.

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

The way I draw the contour is that for each line of my data I remove the previous one:

for coll in my_contour[0].collections:
    coll.remove()

并添加一个新的

my_contour[0] = ax[0].contour(x, y, my_func, [0])

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

At the beginning of the code, I have plt.ion() to update the plots as I add them.

任何帮助将不胜感激.

谢谢

推荐答案

以下是有关如何在动画中使用contour绘图的示例.它使用matplotlib.animation.FuncAnimation,可以轻松打开和关闭闪烁功能. 使用blit = True时,它在我的计算机上以〜64 fps的速度运行,而不会增加〜55 fps.注意,interval当然必须允许快速动画播放;将其设置为interval=10(毫秒)将允许高达100 fps,但是绘制时间将其限制为比该速度慢.

Here is an example on how to use a contour plot in an animation. It uses matplotlib.animation.FuncAnimation which makes it easy to turn blitting on and off. With blit=True it runs at ~64 fps on my machine, without blitting ~55 fps. Note that the interval must of course allow for the fast animation; setting it to interval=10 (milliseconds) would allow for up to 100 fps, but the drawing time limits it to something slower than that.

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()

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

Note that in the animated gif above a slower frame rate is shown, since the process of saving the images takes a little longer.

这篇关于在matplotlib中提高重绘轮廓图的速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆