我正在尝试从二维矩阵中找出尽可能多的数据可视化工具(任何其他查看二维矩阵的好方法都会得到加分)。
我生成了很多热图,我被告知 pcolor
是可行的方法(我现在使用 seaborn
)。
当它们执行非常相似的操作时,为什么 plt.imshow
比 plt.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
最佳答案
部分回答您的问题,plt.imshow 比 plt.pcolor 快得多因为它们没有执行类似的操作。事实上,他们做的事情非常不同。
根据文档,matplotlib.pyplot.pcolor 返回一个 matplotlib.collections.PolyCollection,与返回 matplotlib.collections.QuadMesh 对象的 pcolormesh 相比,它可能会很慢。另一方面,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 是最有效的。
关于python - 为什么 plt.imshow 比 plt.pcolor 快得多?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36513406/