gpt4 book ai didi

python - 为什么 plt.imshow 比 plt.pcolor 快得多?

转载 作者:太空宇宙 更新时间:2023-11-04 03:15:15 56 4
gpt4 key购买 nike

我正在尝试从 2D 矩阵中找出尽可能多的数据可视化工具(加分点是查看 2D 矩阵的任何其他好方法)。

我生成了很多热图,有人告诉我 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/

56 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com