Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么plt.imshow比plt.pcolor快得多?_Python_Image Processing_Matplotlib_Heatmap_Imshow - Fatal编程技术网

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

Python 为什么plt.imshow比plt.pcolor快得多?,python,image-processing,matplotlib,heatmap,imshow,Python,Image Processing,Matplotlib,Heatmap,Imshow,我试图从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)[N

我试图从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,这可能比pcolormesh返回matplotlib.collections.QuadMesh对象慢。另一方面,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是最有效的

一个可能重复的问题?见: