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

筅森魡賤 提交于 2020-11-28 02:09:13

问题


I have two orbits which occur at different heights. I plot them in 3D, but I wish to join them together with a surface. So far I have this picture:

which I get using this script:

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()

As you see - this doesn't look very good. My motivation for wanting to do this is that I have many more of these orbits that I need to plot, all of them at different heights. As the height changes, the orbits get smaller. I feel the best way to visualize this is to join them up with a surface - in this way, the orbits will trace out a deformed, double-barreled cylinder, which will shrink and pinch off at the end. Is there a way to do this?

I would like to be able to do something in the style of this:


回答1:


You'll want to find the equation for the line connecting the ith point in the first orbit to the ith point in the second orbit. Then you can use i and z as parameters, varying over all the possible values to find X and Y.

Example:

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()



来源:https://stackoverflow.com/questions/32513262/how-to-join-these-two-3d-lines-together-with-a-surface-in-pythons-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!