, matplotlib , , , , (, ). , , - . , , , x y . , ( ) . , , x y . ( , ) , ( ) .
, 2D- , , ( quiver contour). meshgrid my x y, meshgrid . , , while .
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-3,3,0.2)
y = np.arange(-5,5,0.2)
z = np.arange(-5,5,0.2)
def grad(f):
gx = np.zeros_like(f)
gy = np.zeros_like(f)
gz = np.zeros_like(f)
for n in range(1,len(x)-1):
for m in range(1,len(y)-1):
for k in range(1,len(z)-1):
gx[n,m,k] = (T[n+1,m,k]-T[n-1,m,k])/(x[n+1]-x[n-1]);
gy[n,m,k] = (T[n,m+1,k]-T[n,m-1,k])/(y[m+1]-y[m-1]);
gz[n,m,k] = (T[n,m,k+1]-T[n,m,k-1])/(z[k+1]-z[k-1]);
return gx, gy, gz
T = np.zeros((len(x), len(y), len(z)))
for n in range(len(x)):
for m in range(len(y)):
for k in range(len(z)):
T[n,m,k] = np.sin((x[n] - y[m])/3.0) + 0.3*np.cos(y[m]) + z[k]**2
gx,gy,gz = grad(T)
Y, X= np.meshgrid(y,x)
plt.figure('WRONG with x and y')
plt.contour(y, x, T[:,:,round(len(z)/2)], 64)
plt.colorbar()
plt.quiver(y, x, 10*gx[:,:,round(len(z)/2)], 10*gy[:,:,round(len(z)/2)])
plt.xlabel("x")
plt.ylabel("y")
plt.axes().set_aspect('equal')
plt.figure('with X and Y')
plt.contour(X, Y, T[:,:,round(len(z)/2)], 64)
plt.colorbar()
plt.quiver(X, Y, 10*gx[:,:,round(len(z)/2)], 10*gy[:,:,round(len(z)/2)])
plt.xlabel("X")
plt.ylabel("Y")
plt.axes().set_aspect('equal')
plt.figure('with Y and X')
plt.contour(Y, X, T[:,:,round(len(z)/2)], 64)
plt.colorbar()
plt.quiver(Y, X, 10*gy[:,:,round(len(z)/2)], 10*gx[:,:,round(len(z)/2)])
plt.xlabel("Y")
plt.ylabel("X")
plt.axes().set_aspect('equal')
plt.show()
, , - nx != ny. , , , () .