Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Matplotlib三维散点图中设置查看位置_Python_Numpy_Matplotlib_3d_Geometry - Fatal编程技术网

Python 在Matplotlib三维散点图中设置查看位置

Python 在Matplotlib三维散点图中设置查看位置,python,numpy,matplotlib,3d,geometry,Python,Numpy,Matplotlib,3d,Geometry,我在三维立方体中有一组点。我想得到这些点在2d中的视觉投影,这取决于观察者的位置。到目前为止,我一直在尝试在3d中绘制我的点,并设置仰角和方位角,这样我就可以在侧面看到一个立方体 这是一个简单的例子,但我需要能够将我的代码推广到观察者的任何(x,y,z)位置。以下是我迄今为止所尝试的: import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D coord = np.ran

我在三维立方体中有一组点。我想得到这些点在2d中的视觉投影,这取决于观察者的位置。到目前为止,我一直在尝试在3d中绘制我的点,并设置仰角和方位角,这样我就可以在侧面看到一个立方体

这是一个简单的例子,但我需要能够将我的代码推广到观察者的任何(x,y,z)位置。以下是我迄今为止所尝试的:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

coord = np.random.uniform(2.,4., (10000,3)) # cube points

los = np.array([10.,3.,3])  #observer position ( looking at side of cube)

center = np.array([3.,3.,3.]) #cube center

def elev(los, target):
    diff = target - los
    cosel = np.sum(los*diff)/np.sqrt(np.sum(los**2.) *  np.sum(diff**2.))
    el = np.degrees(np.arccos(cosel))
    return el

def azi(los, target):
    diff = target - los
    cosazi = (-los[2]*los[0]*diff[0] - los[2]*los[1]*diff[1] + \\
(los[0]**2.+los[1]**2)*diff[2]) / np.sqrt((los[0]**2.+los[1]**2.)* \\
(los[0]**2.+los[1]**2.+los[2]**2)*(diff[0]**2.+diff[1]**2.+diff[2]**2)) 
    sinazi = (-los[2]*diff[0] + los[0]*diff[1]) /  \\
np.sqrt((los[0]**2.+los[1]**2.)*(diff[0]**2.+diff[1]**2.+diff[2]**2.))
    tanazi = sinazi/cosazi
    azi = np.degrees(np.arctan(tanazi))
    return azi

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(coord[:,0], coord[:,1], coord[:,2], edgecolor = "none", alpha = 0.3)

ax.view_init(elev=elev(los, center), azim=azi(los,center))
plt.show()
我应该看到一个完美的正方形,但我的视角是一个角度。怎么了


我从这里借用了仰角和方位角公式:

问题是matplotlib中的仰角和方位角是相对于轴原点的,而不是绘制对象的中心。然后需要将对象的坐标转换为原点,并转换相对于对象的视点

然后,通过将观察者的位置转换为球面极坐标,很容易得到方位角和仰角

注:高程θ是从基准面测量的,因此θ=arcsin(z/r)(而不是arccos)。方位角需要根据观察者所在的象限进行校正