2016-04-09 94 views
2

我想从2D矩阵中找出尽可能多的数据可视化工具(对于查看2D矩阵的任何其他好方法都是额外的)。为什么plt.imshow比plt.pcolor快得多?

我生成了很多热点地图,我被告知pcolor是要走的路(我现在使用seaborn)。

为什么plt.imshowplt.pcolor要快得多?

def image_gradient(m,n): 
    """ 
    Create image arrays 
    """ 
    A_m = np.arange(m)[:, None] 
    A_n = np.arange(n)[None, :] 
    return(A_m.astype(np.float)+A_n.astype(np.float)) 

A_100x100 = image_gradient(m,n) 

%timeit plt.pcolor(A_100x100) 
%timeit plt.imshow(A_100x100) 

1 loop, best of 3: 636 ms per loop 
1000 loops, best of 3: 1.4 ms per loop 
+0

可能存在重复的问题?参见:http://stackoverflow.com/questions/7470288/matplotlib-pcolor-very-slow-alternatives – Alejandro

回答

2

部分回答你的问题,plt.imshow比plt.pcolor 更快,因为他们没有做类似的操作。事实上,他们做了非常不同的事情。

根据文档,matplotlib.pyplot.pcolor返回一个matplotlib.collections.PolyCollection,与pcolormesh相比,它会比较慢,它会返回一个matplotlib.collections.QuadMesh对象。另一方面,imshow返回一个matplotlib.image.AxesImage对象。我做了令pColor,imshow和pcolormesh测试:

def image_gradient(m,n): 
    """ 
    Create image arrays 
    """ 
    A_m = np.arange(m)[:, None] 
    A_n = np.arange(n)[None, :] 
    return(A_m.astype(np.float)+A_n.astype(np.float)) 

m = 100 
n = 100 

A_100x100 = image_gradient(m,n) 

%time plt.imshow(A_100x100) 
%time plt.pcolor(A_100x100) 
%time plt.pcolormesh(A_100x100) 

和我得到的结果是:

imshow() 
CPU times: user 76 ms, sys: 0 ns, total: 76 ms 
Wall time: 74.4 ms 
pcolor() 
CPU times: user 588 ms, sys: 16 ms, total: 604 ms 
Wall time: 601 ms 
pcolormesh() 
CPU times: user 0 ns, sys: 4 ms, total: 4 ms 
Wall time: 2.32 ms 

显然,对于这个特殊的例子,pcolormesh是最有效的一个。

相关问题