问题描述
我试图从2D矩阵中找出尽可能多的数据可视化工具(奖励点到任何其他用于查看2D矩阵的好方法)。
I am trying to figure out as much data visualization tools as I can from 2D matrices (bonus points to any other good methods for looking at 2D matrices).
我生成了很多热图,我被告知 pcolor
是要走的路(我现在使用 seaborn
)。
I generate a lot of heatmaps, where I've been told pcolor
is the way to go (I now use seaborn
).
为什么 plt.imshow
当他们做类似的操作时,比 plt.pcolor
要快得多吗?
Why is plt.imshow
SO much quicker than plt.pcolor
when they are doing really similar operations?
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
推荐答案
部分回答你的问题,plt.imshow比plt.pcolor 快得多,因为他们没有做类似的操作。事实上,他们做了很多不同的事情。
Answering partially to your question, plt.imshow is much quicker than plt.pcolor because they are not doing similar operations. In fact, they do very different things.
根据文档,matplotlib.pyplot.pcolor返回一个matplotlib.collections.PolyCollection,与pcolormesh相比,它可能很慢,后者返回一个matplotlib.collections.QuadMesh对象。另一方面,imshow返回一个matplotlib.image.AxesImage对象。我用pcolor,imshow和pcolormesh进行了测试:
According to the documentation, matplotlib.pyplot.pcolor returns a matplotlib.collections.PolyCollection, which can be slow compared to pcolormesh, which returns a matplotlib.collections.QuadMesh object. imshow, in the other hand, returns a matplotlib.image.AxesImage object. I did a test with pcolor, imshow and 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是最有效的一个。
Clearly, for this particular example, pcolormesh is the most efficient one.
这篇关于为什么plt.imshow比plt.pcolor快得多?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!