Nya*_*oil 8 python geometry matplotlib python-2.7
对于我正在大学工作的项目的一部分,我正在尝试使用Python重建地球并使用它来绘制表面上的特定位置并绘制各种方向的圆圈,以便它们与我必须给出的卫星数据对齐从数据集表示给定时间的飞机位置.
我开始只是绘制一个线框并在线框上绘制我需要的点(全部按比例缩放地球及其地理区域).
我遇到的问题是当我在一个球体上绘制点时,地球上的图像重叠在上面,当球体旋转超过某个点时,这些点会消失.所以,最初的问题:我怎么能阻止他们消失?
其次; 我似乎找不到任何方法来绘制以球体为中心的圆圈 - 例如,围绕赤道的圆圈然后操纵相同的想法在圆球表面上绘制圆圈以给出如下图像:
我知道这是来自谷歌地图,但我很好奇是否可以在Python中完成(我假设如此).
我目前的代码是:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from itertools import product, combinations
import PIL
#Plot the Earth
f = plt.figure(1, figsize=(13,13))
ax = f.add_subplot(111, projection='3d')
u, v = np.mgrid[0:2*np.pi:30j, 0:np.pi:20j]
x=6371*np.cos(u)*np.sin(v)
y=6371*np.sin(u)*np.sin(v)
z=6371*np.cos(v)
ax.plot_wireframe(x, y, z, color="b")
#GES ground station @ Perth & AES @ KLIA
ax.scatter([-2368.8],[4881.1],[-3342.0],color="r",s=100)
ax.scatter([-1293.0],[6238.3],[303.5],color="k",s=100)
#Load earthmap with PIL
bm = PIL.Image.open('earthmap.jpg')
#It's big, so I'll rescale it, convert to array, and divide by 256 to get RGB values that matplotlib accept
bm = np.array(bm.resize([d/3 for d in bm.size]))/256.
#d/1 is normal size, anything else is smaller - faster loading time on Uni HPC
#Coordinates of the image - don't know if this is entirely accurate, but probably close
lons = np.linspace(-180, 180, bm.shape[1]) * np.pi/180
lats = np.linspace(-90, 90, bm.shape[0])[::-1] * np.pi/180
#Repeat code specifying face colours
x = np.outer(6371*np.cos(lons), np.cos(lats)).T
y = np.outer(6371*np.sin(lons), np.cos(lats)).T
z = np.outer(6371*np.ones(np.size(lons)), np.sin(lats)).T
ax.plot_surface(x, y, z, rstride=4, cstride=4, facecolors = bm)
plt.show()
Run Code Online (Sandbox Code Playgroud)
如果有任何办法,我可以得到它,所以积分停止消失,甚至只在赤道上绘制一个圆圈,这将是伟大的!
谢谢!
小智 0
我相信你的问题的第一部分已经大部分得到解决。关于你的问题的第二部分,你一般可以尝试以下方法,
%matplotlib ipympl
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Creating an arbitrary sphere
f = plt.figure(1, figsize=(10, 10))
ax = f.add_subplot(111, projection='3d')
u, v = np.mgrid[0:2*np.pi:100j, 0:np.pi:100j]
x=np.cos(u)*np.sin(v)
y=np.sin(u)*np.sin(v)
z=np.cos(v)
ax.plot_surface(x, y, z, color="b", alpha=0.3) #low alpha value allows for better visualization of scatter points
def draw_circle_on_sphere(p:float, a:float, v:float):
'''
Parametric equation determined by the radius and angular positions (both polar and azimuthal relative to the z-axis) of the circle on the spherical surface
Parameters:
p (float): polar angle
a (float): azimuthal angle
v (float): radius is controlled by sin(v)
Returns:
Circular scatter points on a spherical surface
'''
u = np.mgrid[0:2*np.pi:30j]
x = np.sin(v)*np.cos(p)*np.cos(a)*np.cos(u) + np.cos(v)*np.sin(p)*np.cos(a) - np.sin(v)*np.sin(a)*np.sin(u)
y = np.sin(v)*np.cos(p)*np.sin(a)*np.cos(u) + np.cos(v)*np.sin(p)*np.sin(a) + np.sin(v)*np.cos(a)*np.sin(u)
z = -np.sin(v)*np.sin(p)*np.cos(u) + np.cos(v)*np.cos(p)
return ax.scatter(x, y, z, color="r")
_ = draw_circle_on_sphere(3*np.pi/2, np.pi/4, np.pi/4) # example
Run Code Online (Sandbox Code Playgroud)