Matplotlib:使用pcolormesh进行平移和缩放时缓慢重绘

时间:2015-02-16 13:45:35

标签: python matplotlib

我试图用plt.pcolormesh绘制1000x1000点色彩图。 它可以工作,但它的平移和缩放速度和地狱一样慢:它只能提供1 fps。

有没有办法加快速度?

这是我的简单代码:

import matplotlib
import matplotlib.pyplot as plt

import numpy as np

r_array = np.linspace(0, 2, 1000)
phi_array = np.linspace(0, 2 * np.pi, 1000)

r_grid, phi_grid, = np.meshgrid(r_array, phi_array)


z_grid = r_grid + phi_grid
x_grid = r_grid * np.cos(phi_grid)
y_grid = r_grid * np.sin(phi_grid)

plt.pcolormesh(x_grid, y_grid, z_grid)
plt.show()

1 个答案:

答案 0 :(得分:0)

也许这只是为另一个减慢交易,但是如果只使用scipy.interpolate.griddata将数据转换为简单的图像矩阵呢?计算需要一段时间,但平移和缩放非常快。

import matplotlib.pyplot as plt
import numpy as np
import scipy.interpolate

fig,axs = plt.subplots(1,2,figsize=(8,4))

r_array = np.linspace(0, 2, 400)
phi_array = np.linspace(0, 2 * np.pi, 400)

r_grid, phi_grid, = np.meshgrid(r_array, phi_array)

z_grid = r_grid + phi_grid
x_grid = r_grid * np.cos(phi_grid)
y_grid = r_grid * np.sin(phi_grid)

x = np.linspace(np.min(x_grid),np.max(x_grid),500)
y = np.linspace(np.min(y_grid),np.max(y_grid),500)

X,Y = np.meshgrid(x,y)

data1 = scipy.interpolate.griddata((x_grid.ravel(),y_grid.ravel()),z_grid.ravel(),(X,Y),method='linear')
data2 = scipy.interpolate.griddata((x_grid.ravel(),y_grid.ravel()),z_grid.ravel(),(X,Y),method='nearest')

extent = (np.min(x_grid),np.max(x_grid),np.min(y_grid),np.max(y_grid))
axs[0].imshow(data1,interpolation='nearest',aspect='auto',extent=extent)
axs[1].imshow(data2,interpolation='nearest',aspect='auto',extent=extent)

axs[0].set_title('griddata, method=\'linear\'')
axs[1].set_title('griddata, method=\'nearest\'')

plt.tight_layout()

plt.show()

enter image description here