Ban*_*Gap 5 python numpy matplotlib
有一个包含形状的 3D 数据的数组,例如 (64,64,64),您如何通过此数据集绘制由点和法线给出的平面(类似于晶体学中的 hkl 平面)?类似于在 MayaVi 中通过在数据中旋转平面来完成的操作。
在大多数情况下,生成的图将包含非方形平面。这些可以用 matplotlib(某种非矩形补丁)来完成吗?
编辑:我几乎自己解决了这个问题(见下文),但仍然想知道如何在 matplotlib 中绘制非矩形补丁......?
编辑:由于下面的讨论,我重申了这个问题。
我有这个问题的倒数第二个解决方案。通过使用第二个答案来部分解决:根据法线向量和 Matlab 或 matplotlib 中的点绘制平面:
# coding: utf-8
import numpy as np
from matplotlib.pyplot import imshow,show
A=np.empty((64,64,64)) #This is the data array
def f(x,y):
return np.sin(x/(2*np.pi))+np.cos(y/(2*np.pi))
xx,yy= np.meshgrid(range(64), range(64))
for x in range(64):
A[:,:,x]=f(xx,yy)*np.cos(x/np.pi)
N=np.zeros((64,64))
"""This is the plane we cut from A.
It should be larger than 64, due to diagonal planes being larger.
Will be fixed."""
normal=np.array([-1,-1,1]) #Define cut plane here. Normal vector components restricted to integers
point=np.array([0,0,0])
d = -np.sum(point*normal)
def plane(x,y): # Get plane's z values
return (-normal[0]*x-normal[1]*y-d)/normal[2]
def getZZ(x,y): #Get z for all values x,y. If z>64 it's out of range
for i in x:
for j in y:
if plane(i,j)<64:
N[i,j]=A[i,j,plane(i,j)]
getZZ(range(64),range(64))
imshow(N, interpolation="Nearest")
show()
Run Code Online (Sandbox Code Playgroud)
这不是最终的解决方案,因为绘图不限于具有 az 值的点,不考虑大于 64 * 64 的平面,并且必须在 (0,0,0) 处定义平面。