Best way to build a 3D matrix in python

I am trying to visualize 3D data. This is a complete 3D matrix: each coordinate (x, y, z) matters, unlike a surface or a set of separate data vectors. The way I'm trying to do this is to build an opaque cube, where each edge of the cube displays the sum of data in orthogonal dimension.

Some examples of data are mainly blob centered at (3,5,7):

import numpy as np (x,y,z) = np.mgrid[0:10,0:10, 0:10] data = np.exp(-((x-3)**2 + (y-5)**2 + (z-7)**2)**(0.5)) edge_yz = np.sum(data,axis=0) edge_xz = np.sum(data,axis=1) edge_xy = np.sum(data,axis=2) 

Thus, the idea would be to create a 3D graph showing a cube; on each surface of the cube the corresponding two-dimensional matrix edge_ * will be displayed. It would be like building 3 4-sided polygons in the corresponding three-dimensional positions (or 6, if you would also make the back sides of the cube), except that each polygon is actually a matrix of values โ€‹โ€‹that will be displayed in color.

My best approximation at the moment is to compute large matrices containing skewed versions of the edge, and combine them into one large two-dimensional matrix and imshow (), which is a large matrix. It seems rather awkward, and does a lot of work that some kind of engine in matplotlib or m3plot or something that I'm sure is already doing. It also only works for viewing a static image from one viewing angle, but thatโ€™s not what I need to overcome at the moment.

Is there a good way to build these cube faces in real 3D using an existing python tool? Is there a better way to build a 3D matrix?

+6
source share
2 answers

Falko's suggestion to use contouring with a small number of finalists. This is a bit limited, because at least my version of the outline has several errors, when it sometimes displays one of the planes in front of the other planes, it should be behind, but so far only projecting either the three front or three back sides of the cube will do:

 import numpy as np import math import matplotlib.pyplot as plot import mpl_toolkits.mplot3d.axes3d as axes3d def cube_marginals(cube, normalize=False): c_fcn = np.mean if normalize else np.sum xy = c_fcn(cube, axis=0) xz = c_fcn(cube, axis=1) yz = c_fcn(cube, axis=2) return(xy,xz,yz) def plotcube(cube,x=None,y=None,z=None,normalize=False,plot_front=False): """Use contourf to plot cube marginals""" (Z,Y,X) = cube.shape (xy,xz,yz) = cube_marginals(cube,normalize=normalize) if x == None: x = np.arange(X) if y == None: y = np.arange(Y) if z == None: z = np.arange(Z) fig = plot.figure() ax = fig.gca(projection='3d') # draw edge marginal surfaces offsets = (Z-1,0,X-1) if plot_front else (0, Y-1, 0) cset = ax.contourf(x[None,:].repeat(Y,axis=0), y[:,None].repeat(X,axis=1), xy, zdir='z', offset=offsets[0], cmap=plot.cm.coolwarm, alpha=0.75) cset = ax.contourf(x[None,:].repeat(Z,axis=0), xz, z[:,None].repeat(X,axis=1), zdir='y', offset=offsets[1], cmap=plot.cm.coolwarm, alpha=0.75) cset = ax.contourf(yz, y[None,:].repeat(Z,axis=0), z[:,None].repeat(Y,axis=1), zdir='x', offset=offsets[2], cmap=plot.cm.coolwarm, alpha=0.75) # draw wire cube to aid visualization ax.plot([0,X-1,X-1,0,0],[0,0,Y-1,Y-1,0],[0,0,0,0,0],'k-') ax.plot([0,X-1,X-1,0,0],[0,0,Y-1,Y-1,0],[Z-1,Z-1,Z-1,Z-1,Z-1],'k-') ax.plot([0,0],[0,0],[0,Z-1],'k-') ax.plot([X-1,X-1],[0,0],[0,Z-1],'k-') ax.plot([X-1,X-1],[Y-1,Y-1],[0,Z-1],'k-') ax.plot([0,0],[Y-1,Y-1],[0,Z-1],'k-') ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') plot.show() 

plot_front = True plot_front = True plot_front = False plot_front = False Other data (not shown) Other data (not shown)

+4
source

Take a look at MayaVI . The contour3d () function may be what you want.

Here is the answer that I gave to a similar question with an example of code and the final schedule fooobar.com/questions/974335 / ...

+1
source

Source: https://habr.com/ru/post/974332/


All Articles