numpy 使用matplotlib.pyplot在3d图形中显示额外的线

h9a6wy2h  于 2023-08-05  发布在  其他
关注(0)|答案(1)|浏览(96)

我试图创建一个3d图形绘制行星的轨道,代码工程罚款2d投影,但3d遇到一些额外的线(棕色线)。我找不到问题所在。

import matplotlib.pyplot as plt
import numpy as np

Mass = [332837, 0.055, 0.85, 1.000, 0.107, 317.85, 95.159, 14.500, 17.204, 0.003]
a = ["", 0.387, 0.723, 1.000, 1.523, 5.202, 9.576, 19.293, 30.246, 39.509]
e = ["", 0.21, 0.01, 0.02, 0.09, 0.05, 0.06, 0.05, 0.01, 0.25]
beta = ["", 7.00, 3.39, 0.00, 1.85, 1.31, 2.49, 0.77, 1.77, 17.5]

theta = np.linspace(0, 2*np.pi, 1000)

def get_planet_data(index, projection="2d"):

    r = (a[index] * (1 - e[index]**2))/ (1 - e[index] * np.cos(theta))
    x = r*np.cos(theta)
    y = r*np.sin(theta)
    if projection == "3d":
        angle = beta[index] * np.pi / 180
        xx = x*np.cos(angle)
        yy = y
        zz = x*np.sin(angle)
        return xx, yy, zz
    return x, y

fig = plt.figure()
ax = plt.axes(projection='3d')
ax.set_xlim(-40, 40)
ax.set_ylim(-40, 40)
ax.set_zlim(-40, 40)
xList = []
yList = []
zList = []
for i in range(1, 10):
    data = get_planet_data(i, "3d")
    xList.append(data[0])
    yList.append(data[1])
    zList.append(data[2])

# ax.plot(xList[0], yList[0], zList[0],
#         xList[1], yList[1], zList[1],
#         xList[2], yList[2], zList[2],
#         xList[3], yList[3], zList[3])
ax.plot(xList[4], yList[4], zList[4],
        xList[5], yList[5], zList[5],
        xList[6], yList[6], zList[6],
        xList[7], yList[7], zList[7],
        xList[8], yList[8], zList[8])

plt.show()

字符串
我发现matplotlib在3d绘图方面不如2d好,于是我尝试使用pyqtgraph或mayavi作为替代品。

lmvvr0a8

lmvvr0a81#

我对3D绘图API没有太多的经验。但看起来它与在一次plot调用中绘制所有线有关。尝试将其分解为for循环。
(我还更多地使用了numpy来摆脱其他循环。

import matplotlib.pyplot as plt
import numpy as np

plt.close("all")

a = np.array([0.387, 0.723, 1.000, 1.523, 5.202, 9.576, 19.293, 30.246, 39.509])
e = np.array([0.21, 0.01, 0.02, 0.09, 0.05, 0.06, 0.05, 0.01, 0.25])
beta = np.array([7.00, 3.39, 0.00, 1.85, 1.31, 2.49, 0.77, 1.77, 17.5])

theta = np.linspace(0, 2*np.pi, 1000)

def get_planet_data(index, projection="2d"):
    r = (a[index] * (1 - e[index]**2))/ (1 - e[index] * np.cos(theta))
    x = r*np.cos(theta)
    y = r*np.sin(theta)
    if projection == "3d":
        angle = beta[index]*np.pi/180
        xx = x*np.cos(angle)
        yy = y
        zz = x*np.sin(angle)
        return xx, yy, zz
    return x, y

fig = plt.figure()
ax = plt.axes(projection="3d")
ax.set_xlim(-40, 40)
ax.set_ylim(-40, 40)
ax.set_zlim(-40, 40)

xx, yy, zz = get_planet_data(np.arange(9)[:,None], "3d")
for i in range(4, 9):
    ax.plot(xx[i], yy[i], zz[i],)

plt.show()

字符串


的数据

相关问题