如何在 Python 的 matplotlib 中将这两条 3D 线与曲面连接在一起

How to join these two 3D lines together with a surface in Python's matplotlib

我有两个轨道,它们出现在不同的高度。我在 3D 中绘制它们,但我希望将它们与表面连接在一起。到目前为止,我有这张照片:

我使用这个脚本得到的:

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

font = {'size'   : 18}
matplotlib.rc('font', **font)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

#Read in data
data = np.genfromtxt('mu8.txt')
x, y = np.hsplit(data, 2)
N = len(x)
z = np.zeros(N)
z[:,] = 8.0

#Plot the first orbit
ax.plot(x, y, z, 'k-', linewidth=3.0)

#Read in data for second orbit
data = np.genfromtxt('mu9.txt')
x2, y2 = np.hsplit(data, 2)
N = len(x2)
z2 = np.zeros(N)
z2[:,] = 9.0

#Plot second orbit
ax.plot(x2, y2, z2, 'k-', linewidth=3.0)

#Join together the data for both orbits
xx = np.concatenate((x, x2), axis=0)
yy = np.concatenate((y, y2), axis=0)
zz = np.concatenate((z, z2), axis=0)

#Plot the surface
surf = ax.plot_surface(xx,yy,zz, color='m', alpha=0.3,
         linewidth=0)

#Set axis and things
ax.set_xticks([1.0,1.5,2])
ax.set_yticks([32,35,38])
ax.set_ylabel('$||u||_{2}$', fontsize=26, rotation=0, labelpad = 26)
ax.set_xlabel('$h$', fontsize=26)
ax.set_zlabel('$\mu$', fontsize=26, rotation=90)
plt.tight_layout()

plt.show()

如您所见 - 这看起来不太好。我想要这样做的动机是我需要绘制更多这样的轨道,所有这些轨道都在不同的高度。随着高度的变化,轨道变小。我觉得最好的可视化方法是将它们与一个表面连接起来——这样,轨道将描绘出一个变形的 double-barreled 圆柱体,它会在末端收缩并夹断。有没有办法做到这一点?

我希望能够以这种方式做一些事情:

您需要找到连接第一个轨道中的第 i 个点和第二个轨道中的第 i 个点的直线的方程式。然后你可以使用 i 和 z 作为参数,改变所有可能的值来找到 X 和 Y。

示例:

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
Z1 = 8.0
Z2 = 9.0
font = {'size'   : 18}
matplotlib.rc('font', **font)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

t = np.linspace(0, 2 * np.pi, 100)
x = np.cos(t)
y = np.sin(2 * t)
N = len(x)
z = np.zeros(N)
z[:,] = Z1

t = np.linspace(0, 2 * np.pi, 100)
x2 = 2 * np.cos(t)
y2 = 2 * np.sin(2*t)
N = len(x2)
z2 = np.zeros(N)
z2[:,] = Z2 

#Plot the first orbit
ax.plot(x, y, z, 'k-', linewidth=3.0)
#Plot second orbit
ax.plot(x2, y2, z2, 'k-', linewidth=3.0)


i, h = np.meshgrid(np.arange(len(x)), np.linspace(Z1, Z2, 10))
X = (x2[i] - x[i]) / (Z2 - Z1) * (h - Z1) + x[i]
Y = (y2[i] - y[i]) / (Z2 - Z1) * (h - Z1) + y[i]
surf = ax.plot_surface(X, Y, h, color='m', alpha=0.3,
         linewidth=0)


#Set axis and things
ax.set_xticks([1.0,1.5,2])
ax.set_yticks([32,35,38])
ax.set_ylabel('$||u||_{2}$', fontsize=26, rotation=0, labelpad = 26)
ax.set_xlabel('$h$', fontsize=26)
ax.set_zlabel('$\mu$', fontsize=26, rotation=90)
plt.tight_layout()

plt.show()