python - Extracting coordinates from meshgrid data -
i have cubic grid shown in picture below.
i list vertices of each sub-cube, end nested list of sub-cubes corresponding list of vertices.
my initial attempt use generator,
import numpy np import matplotlib.pyplot plt mpl_toolkits.mplot3d import axes3d fig = plt.figure() ax = fig.add_subplot(111, projection='3d') dims = [9,9,9] spacer = 3 subboxcoords = np.array([(x, y, z) x in range(0, dims[0], spacer) y in range(0, dims[1], spacer) z in range(0, dims[2], spacer)]) ax.scatter(subboxcoords[:,0], subboxcoords[:,1], subboxcoords[:,2], c='k', marker='o') ax.set_xlabel('x label') ax.set_ylabel('y label') ax.set_zlabel('z label')
this give me desired shape coordinates ordered in manner vertices extraction of sub-boxes not straight forward. generalize boxes of arbitrary dimension hard coding in intervals not solution.
so, thought use meshgrid
,
nx,ny, nz = (3,3,3) x = np.linspace(0, 10, nx) y = np.linspace(0, 10, ny) z = np.linspace(0, 10, nz) xv, yv, zv = np.meshgrid(x, y, z, indexing='xy') ax.scatter(xv, yv, zv, c='g', marker='^')
this appears powerful way achieve want getting confused. there direct way access vertices in meshgrid
in manner vertex(x,y,z)
? or straight forward way extract sub-cubes?
it seems me solution tantalizingly close cant grasp it!
meshgrid
need, shape of array returned meshgrid
gets confusing. meshgrid returns 3 coordinate arrays, same shape. shape of each of xv, yv, zv
(len(x), len(y), len(z))
. so, extract coordinate @ corner (0, 2, 1)
, write xv[0, 2, 1], yv[0, 2, 1], zv[0, 2, 1]
to extract of subcubes' corners' coordinates, helps observe that, because of way arrays returned meshgrid
ordered sequentially, xv[:-1, :-1, :-1]
returns x-coordinates of near-left-bottom corners of each subcube. likewise, xv[1:, 1:, 1:]
returns far-right-top corners of each subcube. other 6 corners given other 6 combinations of slices :-1
, 1:
(xv[:-1, 1:, :-1]
gives far-left-top corner, example).
so, iterate through 8 combinations of :-1
, 1:
8 parallel arrays of 3 parallel arrays of x, y, z coordinates 8 corners of len(x)-1 * len(y-1) * len(z-1)
subcubes. (if need subcube corner coordinate arrays in particular shape or axis order, or if want use single index specify subcube rather three, use rollaxis
, swapaxis
, shape
needed.)
import numpy np import matplotlib.pyplot plt mpl_toolkits.mplot3d import axes3d import itertools nx, ny, nz = (3,3,3) x = np.linspace(0, 10, nx) y = np.linspace(0, 10, ny) z = np.linspace(0, 10, nz) xv, yv, zv = np.meshgrid(x, y, z, indexing='xy') slices = slice(none, -1), slice(1, none) cornerslices = list(itertools.product(slices, slices, slices)) corners = np.array([(xv[s], yv[s], zv[s]) s in cornerslices]) # shape of `corners` `(len(cornerslices), 3, len(x-1), len(y-1), len(z-1)` # axes of `corners` represent, in same order: corner index; cartesian # coordinate axis (the index [x, y, z]); x, y, , z indexes of subcube. # plot first subcube (subcube 0, 0, 0) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') subcube = corners[:, :, 0, 0, 0] subcubex = subcube [:, 0] subcubey = subcube [:, 1] subcubez = subcube [:, 2] ax.scatter(subcubex , subcubey , subcubez , c='g', marker='^')
there's invariably way indexes xv, yv, zv
instead of getting values, since values duplicated quite few times in corners
array. involve slicing arrays of indexes xv, yv, zv
instead of slicing arrays themselves. head spinning after getting far ndarray voodoo, i'll leave exercise.
Comments
Post a Comment