简体   繁体   中英

Create 2D projection of 3D matrix in python

Short version: I have a NxNxN matrix full of different values. I want to create a 2D projection of it looking exactly like this: http://tinyurl.com/bellfkn (3D if possible too!)

Long version: I have made a density matrix of dimension NxNxN with the following loop:

ndim = 512
massmat = np.zeros((ndim,ndim,ndim)) 
for i in range(0,npoints):
        massmat[int(x1[i]),int(y1[i]),int(z1[i])] = massmat[int(x1[i]),int(y1[i]),int(z1[i])] + mpart

densemat = massmat/volumeofcell

massmat is a numpy array.

So basically I now have a NxNxN matrix with certain cells containing in this case, a density (units of g/cm^3). Is there a way to turn this into a 2D projection - a side-on view of the densities with a colorbar indicating dense areas and less dense areas?

In Matlab I would just do:

imageArray2Dmesh = mean(densemat, 3);
figure
sc(imageArray2Dmesh, 'pink')

And it gives me a density projection - I'd like to do the same but in Python. Is there a way to view the whole NxNxN matrix in a 3D projection too? Just like the link but in 3D. That would be great.

You can use a very similar code in numpy and matplotlib:

import numpy as np
import pylab as plt

imageArray2Dmesh = np.mean(mesh_reshape, axis=2);
plt.figure()
plt.pcolor(imageArray2Dmesh, cmap = ,cmap=plt.cm.pink)
plt.colorbar()
plt.show()

you have a couple of more command, but this is just due to different approaches for the grafics in matlab and matplotlib (hint: in the long run, the matplotlib way is way better)

If you want the project from another direction just change the axis parameter (remember that python has the indices from 0 and not from 1 like matlab).

For a projection from a generic direction...well, that is quite more difficult.

By the way, if you need to look at some 3D data I strongly suggest you to lose some time to explore mayavi. It's still a python library, and it's really powerful for 3d imaging:

http://docs.enthought.com/mayavi/mayavi/auto/examples.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM