Exemple de comment tracer une surface en 3d avec Matplotlib:

from mpl_toolkits.mplot3d import Axes3Dfrom matplotlib import cmfrom matplotlib.ticker import LinearLocator, FormatStrFormatterimport matplotlib.pyplot as pltimport numpy as npimport mathf = 0.1theta = math.radians(0.0) # Converts angle x from degrees to radians.sigma_x = 7.0sigma_y = 7.0radius = 20M = np.zeros((radius*2,radius*2))def ChangeBase(x,y,theta):x_theta = x * math.cos(theta) + y * math.sin(theta)y_theta = y * math.cos(theta) - x * math.sin(theta)return x_theta, y_thetadef GaborFunction(x,y,theta,f,sigma_x,sigma_y):r1 = ChangeBase(x,y,theta)[0] / sigma_xr2 = ChangeBase(x,y,theta)[1] / sigma_yarg = - 0.5 * ( r1**2 + r2**2 )return math.exp(arg) * math.cos(2*math.pi*f*ChangeBase(x,y,theta)[0])x = -float(radius)for i in range(radius*2):y = -float(radius)for j in range(radius*2):M[i,j] = GaborFunction(x,y,theta,f,sigma_x,sigma_y)y = y + 1x = x + 1fig = plt.figure(figsize=(10, 8), dpi=120)ax = fig.gca(projection='3d')X = np.arange(-20, 20, 1.0)Y = np.arange(-20, 20, 1.0)X, Y = np.meshgrid(X, Y)surf = ax.plot_surface(X, Y, M, rstride=1, cstride=2, cmap=cm.Greys_r,linewidth=1, antialiased=False)ax.set_zlim(-1.01, 1.01)ax.zaxis.set_major_locator(LinearLocator(10))ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))fig.colorbar(surf, shrink=0.5, aspect=5)plt.savefig('GaborFilter_3d.png')plt.show()
Recherches associées
| Liens | Site |
|---|---|
| mplot3d tutorial | matplotlib Documentation |
| how to set “camera position” for 3d plots using python/matplotlib? | stackoverflow |
