matplotlib如何在3D数据集中绘制几乎任意平面? [英] How can an almost arbitrary plane in a 3D dataset be plotted by matplotlib?

查看:293
本文介绍了matplotlib如何在3D数据集中绘制几乎任意平面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个数组,其中包含形状为3D的3D数据. (64,64,64),如何通过该数据集绘制由点和法线给定的平面(类似于晶体学中的hkl平面)? 类似于在MayaVi中通过旋转数据平面来完成的操作.

There is an array containing 3D data of shape e.g. (64,64,64), how do you plot a plane given by a point and a normal (similar to hkl planes in crystallography), through this dataset? Similar to what can be done in MayaVi by rotating a plane through the data.

在大多数情况下,生成的图将包含非正方形平面. 可以使用matplotlib(某种非矩形的补丁)来完成这些操作吗?

The resulting plot will contain non-square planes in most cases. Can those be done with matplotlib (some sort of non-rectangular patch)?

编辑:我自己几乎解决了这个问题(见下文),但仍然想知道如何在matplotlib中绘制非矩形的补丁...?

Edit: I almost solved this myself (see below) but still wonder how non-rectangular patches can be plotted in matplotlib...?

编辑:由于下面的讨论,我重申了这个问题.

Edit: Due to discussions below I restated the question.

推荐答案

我对这个问题有倒数第二个解决方案.通过使用第二个答案来部分解决问题在法线向量和Matlab或matplotlib中的一个点上:

I have the penultimate solution for this problem. Partially solved by using the second answer to Plot a plane based on a normal vector and a point in Matlab or 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()

这不是最终的解决方案,因为绘图不限于z值的点,不考虑大于64 * 64的平面,并且必须将平面定义为(0,0,0).

It's not the ultimate solution since the plot is not restricted to points having a z value, planes larger than 64 * 64 are not accounted for and the planes have to be defined at (0,0,0).

这篇关于matplotlib如何在3D数据集中绘制几乎任意平面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆