In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
nx = 11
ny = 5
x1,y1 = 1,2
X,Y = np.meshgrid(np.linspace(0,x1,nx),np.linspace(0,y1,ny))
X.shape
Out[1]:
In [2]:
f = lambda X_,Y_:np.sin(X_**2+Y_**2)
Z = f(X,Y)
plt.contourf(X,Y,Z)
# To samo co:
# plt.contourf(X.T,Y.T,Z.T)
Out[2]:
In [ ]:
In [3]:
plt.imshow(Z,interpolation='nearest',origin='lower')
Out[3]:
In [4]:
X
Out[4]:
In [5]:
Y
Out[5]:
Porównanie z próbkowaniem "ręcznym":
In [6]:
i,j = 2,3
print ("dla x i y", X[i,j],Y[i,j],"jest", Z[i,j],f(X[i,j],Y[i,j]),\
"powinno byc rowne", f(x1/float(nx-1)*i,y1/float(ny-1)*j) )
Dobrze:
In [8]:
i,j = 2,3
print ("dla x i y" ,X[j,i],Y[j,i],"jest", Z[j,i],f(X[j,i],Y[j,i]),\
"powinno byc rowne", f(x1/float(nx-1)*i,y1/float(ny-1)*j))
Z jest row major więc można też napisać:
In [14]:
print Z[j,i],Z.flatten()[j*nx+i]
In [15]:
Yn, Xn = np.mgrid[0:ny,0:nx]
Xn.shape
Out[15]:
In [16]:
Xn
Out[16]:
In [17]:
Yn
Out[17]:
In [18]:
Xn/(float(nx-1)*x1)
Out[18]:
In [19]:
X1,Y1 = Xn*(x1/float(nx-1)),Yn*(y1/float(ny-1))
In [20]:
np.allclose(X, X1),np.allclose(Y, Y1),np.allclose(Y, Y1)
Out[20]:
Zresztą sprawdzmy:
In [21]:
Z.strides
Out[21]:
In [22]:
np.meshgrid(np.arange(nx),np.arange(ny))
Out[22]:
In [23]:
list(reversed(np.mgrid[0:ny,0:nx]))
Out[23]:
In [24]:
np.meshgrid(np.arange(ny),np.arange(nx),indexing='ij')
Out[24]:
In [25]:
np.mgrid[0:ny,0:nx]
Out[25]:
In [ ]: