in Education by
I have three 3D mesh matrices (X, Y, Z) corresponding to the xyz coordinate space. I also have a 3D Numpy matrix A where A[i,j,k] contains a float that is associated with the point (x,y,z) where x=X[i,j,k], y=Y[i,j,k], and z=Z[i,j,k]. The float values are continuous within A (i.e. the change in value between adjacent elements of A are typically small). Is there a way to plot the surface that corresponds to a given float value in A using Matplotlib or any other Python-based graphics package? For example, if given a value 2.34, I am interested in getting a plotted contour surface of the matrix A wherever 2.34 (plus or minus some tolerance) shows up? So far, I have been able to recover the xyz coordinates of all values in A that are within some tolerance of the target value and then make a 3D scatter plot using this (code below). Perhaps there is also a way of plotting a surface from these points? def clean (A, t, dt): # function for making A binary for t+-dt # t is the target value I want in the matrix A with tolerance dt new_A = np.copy(A) new_A[np.logical_and(new_A > t-dt, new_A < t+dt)] = -1 new_A[new_A != -1] = 0 new_A[new_A == -1] = 1 return (new_A) def get_surface (X, Y, Z, new_A): x_vals = [] y_vals = [] z_vals = [] # Retrieve (x,y,z) coordinates of surface for i in range(new_A.shape[0]): for j in range(new_A.shape[1]): for k in range(new_A.shape[2]): if new_A[i,j,k] == 1.0: x_vals.append(X[i,j,k]) y_vals.append(Y[i,j,k]) z_vals.append(Z[i,j,k]) return (np.array(x_vals), np.array(y_vals), np.array(z_vals)) cleaned_A = clean (A, t=2.5, dt=0.001) x_f, y_f, z_f = get_surface (X, Y, Z, cleaned_A ) fig = plt.figure() ax = fig.add_subplot(111, projection='3d', aspect='equal') ax.scatter(x_f, y_f, z_f, color='g', s=1) I have also tried ax.plot_trisurf(x_f,y_f,z_f), but this gives me an poorly connected plot. I'm guessing that the ordering of values in my arrays might be affecting this, in which case is there a package that can do some kind of 3D interpolated surface plot with random ordering of points (e.g. through minimizing the surface area or something like that?) The object that I am interested in is roughly spherical (i.e. two z's per (x,y)). I can't seem to find any working examples of someone triangulating over a closed 3D surface, but maybe I'm not looking in the right places. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
After a lot of digging around, I think I've arrived at a solution that works (for a sphere at least--will update my answer when I try out deformations of a sphere). Many thanks to the comments which helped me think down the right path. I am basically using a ConvexHull for the triangulation from scipy.spatial: from matplotlib.tri import Triangulation from scipy.spatial import ConvexHull def clean (A, t, dt): # function for making A binary for t+-dt # t is the target value I want in the matrix A with tolerance dt new_A = np.copy(A) new_A[np.logical_and(new_A > t-dt, new_A < t+dt)] = -1 new_A[new_A != -1] = 0 new_A[new_A == -1] = 1 return (new_A) def get_surface (X, Y, Z, new_A): x_vals = [] y_vals = [] z_vals = [] # Retrieve (x,y,z) coordinates of surface for i in range(new_A.shape[0]): for j in range(new_A.shape[1]): for k in range(new_A.shape[2]): if new_A[i,j,k] == 1.0: x_vals.append(X[i,j,k]) y_vals.append(Y[i,j,k]) z_vals.append(Z[i,j,k]) return (np.array(x_vals), np.array(y_vals), np.array(z_vals)) cleaned_A = clean (A, t=2.5, dt=0.001) x_f, y_f, z_f = get_surface (X, Y, Z, cleaned_A ) Xs = np.vstack((x_f, y_f, z_f)).T hull = ConvexHull(Xs) x, y, z = Xs.T tri = Triangulation(x, y, triangles=hull.simplices) fig = plt.figure() ax = fig.add_subplot(111, projection='3d', aspect='equal') ax.plot_trisurf(tri, z, color='g', alpha=0.1)

Related questions

0 votes
    I need to iterate through the elements in a numpy array so I can treat any zero elements separately. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I need to iterate through the elements in a numpy array so I can treat any zero elements separately. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    What are the steps to use shape for a 1D array, 2D array and 3D/ND array respectively in NumPy?...
asked Apr 24, 2021 in Technology by JackTerrance
0 votes
    How Do I Make 3d Plots/visualizations Using Numpy/scipy?...
asked Apr 25, 2021 in Technology by JackTerrance
0 votes
    How do you create a 3D array in NumPy?...
asked Apr 24, 2021 in Technology by JackTerrance
0 votes
    _________ display contours of a 3d surface in 2d. (a) aes_contour (b) geom_contour (c) aes_gem (d) ... Analysis of R Programming Select the correct answer from above options...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    You can create a scatter plot matrix using the __________ method in pandas.tools.plotting. (a) sca_matrix (b ... and answers pdf, Data Science interview questions for beginners...
asked Oct 28, 2021 in Education by JackTerrance
0 votes
    I have the 4D array, which is defined as follows: B = np.array( [[[[0.5000, 0.5625], [0.5000, ... loops and ideally one function call. Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    A uniform surface charge of σ = 2 μC/m^2, is situated at z = 2 plane. What is ... theory proposed by,electromagnetic theory engineering physics,electromagnetic theory nptel...
asked Nov 11, 2021 in Education by JackTerrance
0 votes
    Shown below is the input NumPy array. Delete column two and replace it with the new column given below....
asked Apr 24, 2021 in Technology by JackTerrance
0 votes
    How can you identify the datatype of a given NumPy array?...
asked Apr 24, 2021 in Technology by JackTerrance
0 votes
    The electric field intensity of a surface with permittivity 3.5 is given by 18 units. What ... proposed by,electromagnetic theory engineering physics,electromagnetic theory nptel...
asked Nov 11, 2021 in Education by JackTerrance
...