我试着在三个轴上画一个弹簧的对数螺线。使用参数方程式:
x=a*exp(b*th)*cos(th)
y=a*exp(b*th)*sin(th)
使用以下代码:
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')
n=100
a=0.5
b=0.20
th=np.linspace(0, 500, 10000)
x=a*exp(b*th)*cos(th)
y=a*exp(b*th)*sin(th)
ax.plot(x, y)
ax.legend()
plt.show()
我得到了:
但是,我想沿着Z axis
拉伸螺旋,以得到类似于下面的结果,但使用对数螺旋作为基础:
你怎么能做到呢?如何通过向Z axis
添加条件来修改函数
发布于 2018-02-01 21:33:46
由于95%的螺旋线上的点都集中在绘图中间的一个点上,因此将绘图范围限制在如下所示的范围内是有意义的
th=np.linspace(475, 500, 10000)
然后,只需在绘图函数plot(x,y,z)
中指定该范围,使用z值的线性范围就可以直接在绘图中显示所需的曲线。
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['legend.fontsize'] = 10
fig = plt.figure()
ax = fig.gca(projection='3d')
a=0.5
b=0.20
th=np.linspace(475, 500, 10000)
x=a*np.exp(b*th)*np.cos(th)
y=a*np.exp(b*th)*np.sin(th)
z = np.linspace(0,2, len(th))
ax.plot(x, y, z)
#ax.legend()
plt.show()
请注意,我在这里清理了导入。例如,如果您从math
导入cos
,但后来又将所有内容(*
)从pylab导入到名称空间中,则使用的函数cos
是numpy cos
函数,而不是来自数学的函数(数学cos函数无论如何都不会在这里工作)。总而言之:根本不要使用pylab。
发布于 2018-02-01 21:19:51
使用哪种z
有点取决于你。从情节本身来看,很难说,但我猜它是线性的(最简单的选择)。
使用您的代码并添加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()
您可以使用a
和b
参数来获得所需的椭圆形状。您还可以修改z
的定义,使其成为指数或增长的对数。或者完全不同的东西。
顺便说一句,你的导入有点多余,可能来自一个包的一些函数被另一个包遮蔽了。
https://stackoverflow.com/questions/48563526
复制相似问题