如何在 Python 的 matplotlib 中将这两条 3D 线与表面连接在一起 [英] How to join these two 3D lines together with a surface in Python's matplotlib

查看:22
本文介绍了如何在 Python 的 matplotlib 中将这两条 3D 线与表面连接在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

我使用这个脚本:

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:

推荐答案

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

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.

示例:

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

这篇关于如何在 Python 的 matplotlib 中将这两条 3D 线与表面连接在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆