简体   繁体   English

在python中创建3D矩阵的2D投影

[英]Create 2D projection of 3D matrix in python

Short version: I have a NxNxN matrix full of different values. 简短版:我有一个充满不同值的NxNxN矩阵。 I want to create a 2D projection of it looking exactly like this: http://tinyurl.com/bellfkn (3D if possible too!) 我想为其创建一个2D投影,看起来就像这样: http : //tinyurl.com/bellfkn (如果可能,也可以是3D!)

Long version: I have made a density matrix of dimension NxNxN with the following loop: 长版:我使用以下循环制作了尺寸为NxNxN的密度矩阵:

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. massmat是一个numpy数组。

So basically I now have a NxNxN matrix with certain cells containing in this case, a density (units of g/cm^3). 因此,基本上我现在有了一个NxNxN矩阵,其中某些单元格在这种情况下包含密度(单位为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? 有没有办法将其转换为2D投影-密度的侧面视图,并带有指示密集区域和较不密集区域的色条?

In Matlab I would just do: 在Matlab中,我只会做:

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. 它为我提供了密度投影-我想在Python中做同样的事情。 Is there a way to view the whole NxNxN matrix in a 3D projection too? 有没有办法在3D投影中查看整个NxNxN矩阵? Just like the link but in 3D. 就像链接一样,但使用3D。 That would be great. 那很好啊。

You can use a very similar code in numpy and matplotlib: 您可以在numpy和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) 您还有更多命令,但这只是由于matlab和matplotlib中的grafics方法不同(提示:从长远来看,matplotlib方法会更好)

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). 如果要从另一个方向进行项目,只需更改axis参数(请记住,python的索引从0开始,而不是像matlab那样从1开始)。

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. 顺便说一句,如果您需要查看一些3D数据,我强烈建议您浪费一些时间来探索mayavi。 It's still a python library, and it's really powerful for 3d imaging: 它仍然是一个python库,对于3d成像确实非常强大:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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