用Python绘制三轴对数螺旋线

2024-06-11 14:52:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着在三个轴上画一个弹簧的对数螺旋。 使用参数方程:

x=a*exp(b*th)*cos(th)
y=a*exp(b*th)*sin(th)

使用代码:

^{pr2}$

我得到:

enter image description here

但是,我想沿着Z axis拉伸螺旋线以得到类似于下面的结果,但是使用对数缓和曲线作为基础:

enter image description here

你怎么能做到?如何通过向Z axis添加条件来修改函数?在


Tags: 代码参数对数sincos条件基础曲线
2条回答

由于螺旋线的95%的点都集中在绘图中间的一个点上,因此将绘制范围限制为类似

th=np.linspace(475, 500, 10000)

然后使用z值的线性范围,只需在绘图函数plot(x,y,z)中指定该范围,就可以在绘图中直接得到所需的曲线。在

^{pr2}$

enter image description here

注意,我清理了这里的进口。E、 g.如果您从math导入cos,但随后将pylab中的所有(*)导入到命名空间中,则使用的函数cos是numpycos函数,而不是math中的函数(math cos函数在这里无论如何都不起作用)。总的来说:不要使用pylab。在

拿哪一个取决于你。从情节本身很难说,但我的猜测是它是线性的(最简单的选择)。在

使用您的代码并添加z轴,您可以这样做

import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
from math import exp,sin,cos
from pylab import *

mpl.rcParams['legend.fontsize'] = 10

fig = plt.figure()
ax = fig.gca(projection='3d')
a=0.05
b=0.10
# took the liberty of reducing the max value for th 
# as it was giving you values of the order of e42
th=np.linspace(0, 50, 10000)  
x=a*exp(b*th)*cos(th)
y=a*exp(b*th)*sin(th)
z=np.linspace(0,2, 10000)  # creating the z array with the same length as th
ax.plot(x, y, z)  # adding z as an argument for the plot
ax.legend()

plt.show()

您可以使用ab参数来获得所需的椭圆形。您还可以使用z的定义,使其增长为指数或对数。。或者完全是别的什么。在

顺便说一句,您的导入有点多余,可能一个包中的某些函数被另一个包跟踪了。在

相关问题 更多 >