【问题标题】:Plotting a rectangular area on the surface of a sphere在球体表面绘制矩形区域
【发布时间】:2017-07-25 21:12:05
【问题描述】:

我正在尝试在球体表面上画出一个矩形区域。

这是我的球体代码:

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

fig = plt.figure()

ax = fig.gca(projection='3d')

ax.set_aspect("equal")  

theta, phi =  np.mgrid[0:2*np.pi : 20j ,0:np.pi : 20j]

r = 6.3

x = r * np.cos(phi)*np.sin(theta)
y = r * np.sin(phi)*np.sin(theta)
z = r * np.cos(theta)

ax.plot_wireframe(x,y,z, color = "k")
plt.show()

积分将从纬度/经度转换为购物车坐标。

lat1x = 46.49913179 * (2*np.pi/360)
lat2x = 46.4423682 * (2*np.pi/360)
long1y = -119.4049072 * (2*np.pi/360)
long2y = -119.5048141 * (2*np.pi/360)

lat3x = 46.3973998 * (2*np.pi/360)
lat4x = 46.4532495 * (2*np.pi/360)
long3y = -119.4495392 * (2*np.pi/360)
long4y = -119.3492884 * (2*np.pi/360)


xw1 = r * np.cos(lat1x)*np.cos(long1y)
yw1 = r * np.cos(lat1x)*np.sin(long1y)
zw1 = r * np.sin(lat1x)

xw2 = r * np.cos(lat2x)*np.cos(long2y)
yw2 = r * np.cos(lat2x)*np.sin(long2y)
zw2 = r * np.sin(lat2x)

xw3 = r * np.cos(lat3x)*np.cos(long3y)
yw3 = r * np.cos(lat3x)*np.sin(long3y)
zw3 = r * np.sin(lat3x)

xw4 = r * np.cos(lat4x)*np.cos(long4y)
yw4 = r * np.cos(lat4x)*np.sin(long4y)
zw4 = r * np.sin(lat4x) 

p1 = [xw1,yw1,zw1]
p2 = [xw2,yw2,zw2]
p3 = [xw3,yw3,zw3]
p4 = [xw4,yw4,zw4]

ax.scatter(p1,p2,p3,p4, color = "r")

这些是点,在那里转换为笛卡尔坐标我无法让它们出现在球体的表面上。它们还应该形成一个粗略的矩形形状。我希望能够连接这些点以在球体表面上绘制一个矩形。顺便说一句,矩形意味着非常小

【问题讨论】:

  • “我正在尝试”似乎是一种委婉说法。你真的尝试过什么吗?在这种情况下,“追踪”是什么意思?要绘制的坐标是哪些?预期的情节如何?

标签: python python-3.x numpy matplotlib geopy


【解决方案1】:

您转换为笛卡尔坐标可能是错误的。就像在创建球体时一样,您可能想要使用通常的

但这当然取决于球体上“纬度”和“经度”的定义方式。

更重要的是,散点图不正确。总是scatter(x,y,z),第一个参数x坐标,第二个参数y坐标,第三个参数z坐标。

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

fig = plt.figure()

ax = fig.gca(projection='3d')

ax.set_aspect("equal")  

theta, phi =  np.mgrid[0:2*np.pi : 20j ,0:np.pi : 20j]

r = 6.3

x = r * np.cos(phi)*np.sin(theta)
y = r * np.sin(phi)*np.sin(theta)
z = r * np.cos(theta)

ax.plot_wireframe(x,y,z, color = "k")


lat1x = 46.49913179 * (2*np.pi/360)
lat2x = 46.4423682 * (2*np.pi/360)
long1y = -119.4049072 * (2*np.pi/360)
long2y = -119.5048141 * (2*np.pi/360)

lat3x = 46.3973998 * (2*np.pi/360)
lat4x = 46.4532495 * (2*np.pi/360)
long3y = -119.4495392 * (2*np.pi/360)
long4y = -119.3492884 * (2*np.pi/360)

def to_cartesian(lat,lon):
    x = r * np.cos(lon)*np.sin(lat)
    y = r * np.sin(lon)*np.sin(lat)
    z = r * np.cos(lat)
    return [x,y,z]

p1 = to_cartesian(lat1x,long1y)
p2 = to_cartesian(lat2x,long2y)
p3 = to_cartesian(lat3x,long3y)
p4 = to_cartesian(lat4x,long4y)

X = np.array([p1,p2,p3,p4])
ax.scatter(X[:,0],X[:,1],X[:,2], color = "r")


plt.show()

由于矩形非常小,您需要放大相当多才能看到它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    • 1970-01-01
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    相关资源
    最近更新 更多