Why is plt.imshow so much quicker than plt.pcolor ?

女生的网名这么多〃 提交于 2019-12-24 00:43:51

问题


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).

I generate a lot of heatmaps, where I've been told pcolor is the way to go (I now use seaborn).

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

回答1:


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.

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)

and the results I get are:

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

Clearly, for this particular example, pcolormesh is the most efficient one.



来源:https://stackoverflow.com/questions/36513406/why-is-plt-imshow-so-much-quicker-than-plt-pcolor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!