pwtools.mpl.Data2D

class pwtools.mpl.Data2D(x=None, y=None, xx=None, yy=None, zz=None, X=None, Y=None, Z=None, XY=None)[source]

Bases: object

Container which converts between different x-y-z data formats frequently used by scipy.interpolate.bispl{rep,ev} and mpl_toolkits.mplot3d fuctions.

2D because the data is a 2D scalar field, i.e. z(x,y). See also Interpol2D.

Naming conventions: * lowercase: 1d array * uppercase: 2d array

num.Interpol2D.points = num.PolyFit.points = mpl.Data2D.XY num.Interpol2D.values = num.PolyFit.values = mpl.Data2D.zz

__init__(x=None, y=None, xx=None, yy=None, zz=None, X=None, Y=None, Z=None, XY=None)[source]

Use either x, y or xx, yy or X, Y or XY to define the x-y data. z-data is optional. For that, use Z or zz. Conversion to all forms is done automatically.

Parameters:
  • x (1d arrays, (nx,) and (ny,)) – These are the raw x and y “axes”.

  • y (1d arrays, (nx,) and (ny,)) – These are the raw x and y “axes”.

  • X (2d arrays (nx, ny)) – Like np.meshgrid but transposed to have shape (nx,ny), see also meshgridt()

  • Y (2d arrays (nx, ny)) – Like np.meshgrid but transposed to have shape (nx,ny), see also meshgridt()

  • Z (2d arrays (nx, ny)) – Like np.meshgrid but transposed to have shape (nx,ny), see also meshgridt()

  • xx (1d arrays (nx*ny)) – “Double-loop” versions of x,y,Z, input for ax3d.scatter() or bisplrep().

  • yy (1d arrays (nx*ny)) – “Double-loop” versions of x,y,Z, input for ax3d.scatter() or bisplrep().

  • zz (1d arrays (nx*ny)) – “Double-loop” versions of x,y,Z, input for ax3d.scatter() or bisplrep().

  • XY (np.array([xx,yy]).T)

Examples

>>> from pwtools.mpl import Data2D, fig_ax3d
>>> from pwtools import num
>>> from scipy.interpolate import bisplrep, bisplev
>>> x = linspace(-5,5,10)
>>> y = linspace(-5,5,10)
>>> X,Y = num.meshgridt(x,y)
>>> Z = X**2+Y**2
>>> data = Data2D(x=x,y=y,Z=Z)
>>> xi = linspace(-5,5,50)
>>> yi = linspace(-5,5,50)
>>> ZI = bisplev(xi,yi,bisplrep(data.xx, data.yy, data.zz))
>>> spline = Data2D(x=xi, y=yi, Z=ZI)
>>> fig,ax3d = fig_ax3d()
>>> ax3d.scatter(data.xx, data.yy, data.zz, color='b')
>>> ax3d.plot_wireframe(data.X, data.Y, data.Z, color='g')
>>> ax3d.plot_surface(spline.X, spline.Y, spline.Z, cstride=1,
...                   rstride=1, color='r')

Notes

X,Y = num.meshgridt(x,y) are the transposed versions of X,Y = numpy.meshgrid() which returns shape (ny,nx). The shape (nx,ny), which we use, is more intuitive and also used in ax3d.plot_surface etc. The output of scipy.interpolate.bisplev is also (nx,ny).

nx = 10
ny = 5
x = linspace(...,nx)
y = linspace(...,ny)

To calculate z=f(x,y) on the x,y-grid, use num.meshgridt() or X.T, Y.T from numpy.meshgrid():

X,Y = num.meshgridt(x,y)
Z = X**2 + Y**2

X,Y,Z are good for data generation and plotting (ax3d.plot_wireframe()). But the input to bisplrep() must be flat X,Y,Z (xx,yy,zz) like so:

xx = X.flatten()
yy = Y.flatten()
zz = Z.flatten()

The same, as explicit loops:

xx = np.empty((nx*ny), dtype=float)
yy = np.empty((nx*ny), dtype=float)
zz = np.empty((nx*ny), dtype=float)
for ii in range(nx):
    for jj in range(ny):
        idx = ii*ny+jj
        xx[idx] = x[ii]
        yy[idx] = y[jj]
        zz[idx] = x[ii]**2 + y[jj]**2

or:

XY = np.array([k for k in itertools.product(x,y)])
xx = XY[:,0]
yy = XY[:,1]
zz = xx**2 + yy**2

Construct the spline and evaluate:

spl = bisplrep(xx,yy,zz,...)
ZI = bisplev(x,y)

ZI has the correct shape: (nx, ny), which is the shape of np.outer(x,y).

The “inverse meshgrid” operation to transform xx, yy to x, y is done by using numpy.unique. We assumes that xx and yy are generated like in the nested loop above. For otherwise ordered xx, yy this will fail.

Methods

copy()

Copy object.

update(**kwds)

Update object with new or more input data.