在matplotlib中,为什么用较细的线绘制更快? [英] In matplotlib, why is it faster to plot with thinner lines?

查看:151
本文介绍了在matplotlib中,为什么用较细的线绘制更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天偶然发现了这一点:如果线宽小于1.0,似乎在matplotlib中绘制线要快得多.我只在Mac上测试过,但是效果似乎很强.

I stumbled across this today: it seems that it is much faster to plot lines in matplotlib if the linewidth is less than 1.0. I have only tested this on the Mac, but the effect seems very strong.

例如,如果您尝试这段代码,您将看到数据绘制的速度快了10倍,线宽为0.5,而不是线宽为1.0.

For instance, if you try this code, you will see that the data plots about 10x faster with a linewidth of 0.5 rather than a linewidth of 1.0.

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0,10,20000)
y = np.sin(x) + np.random.random(len(x))*0.1

plt.ion()
plt.show()

plt.plot(x,y,lw=0.5)
plt.draw()

plt.figure()

plt.plot(x,y,lw=1.0)
plt.draw()

我使用以下代码绘制了线宽和速度之间的关系图:

I used this code to make a graph of the relationship between linewidth and speed:

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

x = np.linspace(0,10,10000)
y = np.sin(x) + np.random.random(len(x))*0.1

plt.ion()
plt.show()

linewidths = np.linspace(2,0,20)
times = []

for lw in linewidths:
    t = time.time()

    plt.plot(x,y,lw=lw)
    plt.draw()

    times.append(time.time()-t)

    plt.figure()



plt.ioff()
plt.plot(linewidths[1:],times[1:],'ro')

plt.xlabel('Linewidth (points)')
plt.ylabel('Time (seconds)')
plt.show()

结果如下:

使用小于1.0的线宽可提供约10倍的加速,而1.0以后,时间线性增加.我仅在数据点数量很大(大于约5000个点)时才观察到这种效果.对我来说有意义的是,如果我要求matplotlib显示更多像素,则绘制该图可能会花费更长的时间,但是我并不期望使用稍小的线宽(0.5对1.0)会大大提高速度.

Using a linewidth less than 1.0 provides a ~10x speedup, and after 1.0, the time increases linearly. I only observe this effect if the number of datapoints is large, greater than about 5000 points or so. It makes sense to me that if I ask matplotlib to display more pixels, then it might take a little longer to make the plot, but I was not expecting a huge speedup for using a slightly smaller linewidth (0.5 versus 1.0).

谁能解释为什么会这样?我很高兴发现它,因为它可以更快地显示大型数据集.

Can anyone explain why this occurs? I am happy to have discovered it, as it makes it much faster to display large datasets.

有人建议这可能是特定于MacOSX后端的.这似乎有可能.如果我尝试将图以png格式保存,而不是将其绘制到屏幕上,则时间似乎更加随机分布:

Some suggested that this might be specific to the MacOSX backend. This seems likely. If I try to save the plots in png format instead of plotting them to the screen, the times seem more randomly distributed:

推荐答案

有人可以用更彻底的答案代替它,但是这种效果似乎是MacOSX后端所特有的,因为在保存图形时它不会出现作为png.绘制时间似乎也受到Matplotlib版本(1.3.x与1.3.0)的影响.但是,Mac用户似乎可以通过将线宽减小到小于1.0的值来享受大型数据集的加速.

Someone can probably replace this with a more thorough answer, but it appears that this effect is unique to the MacOSX backend, since it does not appear when saving the figures as png. The plotting time seems to also be affected by the version of Matplotlib (1.3.x versus 1.3.0). But, it seems the Mac users can enjoy a speedup for large datasets by decreasing the linewidth to a value smaller than 1.0.

这篇关于在matplotlib中,为什么用较细的线绘制更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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