更新时间:2023-02-19 23:16:23
部分回答你的问题,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.