栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

为什么用Matplotlib绘制这么慢?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

为什么用Matplotlib绘制这么慢?

首先,(尽管这根本不会改变性能)考虑清理代码,类似于:

import matplotlib.pyplot as pltimport numpy as npimport timex = np.arange(0, 2*np.pi, 0.01)y = np.sin(x)fig, axes = plt.subplots(nrows=6)styles = ['r-', 'g-', 'y-', 'm-', 'k-', 'c-']lines = [ax.plot(x, y, style)[0] for ax, style in zip(axes, styles)]fig.show()tstart = time.time()for i in xrange(1, 20):    for j, line in enumerate(lines, start=1):        line.set_ydata(np.sin(j*x + i/10.0))    fig.canvas.draw()print 'FPS:' , 20/(time.time()-tstart)

在上面的示例中,我得到了大约10fps。

请注意,根据您的实际使用情况,matplotlib可能不是一个很好的选择。它面向的是出版物质量的数字,而不是实时显示。

但是,您可以做很多事情来加快此示例的速度。

速度如此之慢的主要原因有两个。

1)调用会重

fig.canvas.draw()
所有内容 。这是您的瓶颈。就您而言,您无需重新绘制诸如轴边界,刻度线标签等内容。

2)在您的情况下,有很多带有很多刻度标签的子图。这些需要很长时间才能绘制出来。

这两种都可以通过使用blitting进行修复。

为了高效地进行blit,您必须使用特定于后端的代码。在实践中,如果您真的担心平滑的动画,那么无论如何,通常都将matplotlib图嵌入某种gui工具包中,所以这不是什么大问题。

但是,在不了解您正在做什么的情况下,我无法为您提供帮助。

尽管如此,仍然有一种中立的方式来完成它,仍然相当快。

import matplotlib.pyplot as pltimport numpy as npimport timex = np.arange(0, 2*np.pi, 0.1)y = np.sin(x)fig, axes = plt.subplots(nrows=6)fig.show()# We need to draw the canvas before we start animating...fig.canvas.draw()styles = ['r-', 'g-', 'y-', 'm-', 'k-', 'c-']def plot(ax, style):    return ax.plot(x, y, style, animated=True)[0]lines = [plot(ax, style) for ax, style in zip(axes, styles)]# Let's capture the background of the figurebackgrounds = [fig.canvas.copy_from_bbox(ax.bbox) for ax in axes]tstart = time.time()for i in xrange(1, 2000):    items = enumerate(zip(lines, axes, backgrounds), start=1)    for j, (line, ax, background) in items:        fig.canvas.restore_region(background)        line.set_ydata(np.sin(j*x + i/10.0))        ax.draw_artist(line)        fig.canvas.blit(ax.bbox)print 'FPS:' , 2000/(time.time()-tstart)

这给了我约200fps。

为了使此操作更加方便,

animations
最新版本的matplotlib中提供了一个模块。

举个例子:

import matplotlib.pyplot as pltimport matplotlib.animation as animationimport numpy as npx = np.arange(0, 2*np.pi, 0.1)y = np.sin(x)fig, axes = plt.subplots(nrows=6)styles = ['r-', 'g-', 'y-', 'm-', 'k-', 'c-']def plot(ax, style):    return ax.plot(x, y, style, animated=True)[0]lines = [plot(ax, style) for ax, style in zip(axes, styles)]def animate(i):    for j, line in enumerate(lines, start=1):        line.set_ydata(np.sin(j*x + i/10.0))    return lines# We'd normally specify a reasonable "interval" here...ani = animation.FuncAnimation(fig, animate, xrange(1, 200),         interval=0, blit=True)plt.show()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/624661.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号