半径可变的3D圆柱体

时间:2018-07-31 09:07:03

标签: python 3d surface

我想根据随半径变化的函数创建一个圆柱体(双曲面)。所以我有一个功能:

def f(x):
     return np.sqrt(1+(x/constant)**(2))

,当然也沿y方向。我想要的是,此函数旋转并看起来像一个双曲面(见图)。我创建了这些表面图,但没有使用f(x)之类的函数。

enter image description here

2 个答案:

答案 0 :(得分:0)

我从维基百科获得了这个脚本:

from __future__ import division
import numpy as np
from numpy import pi, cos, sin, sqrt, outer, ones
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(111, projection='3d')
u = np.linspace(0, 2 * pi, 100)
v = np.linspace(0, pi, 100)
one_v = ones(100)


# Hyperboloid
v = 2*v/pi - 1
x3 = 6 * outer(cos(u), sqrt(1 + v**2))
y3 = 6 * outer(sin(u), sqrt(1 + v**2)) - 16
z3 = 12 * outer(one_v, v)
ax.plot_surface(x3, y3, z3, rstride=5, cstride=5, cmap='Spectral',
                linewidth=0.5)

# Fix aspect ratio and axes details
ax.set_xlim(-13, 13)
ax.set_ylim(-13, 13)
ax.set_zlim(-13, 13)
ax.view_init(elev=35, azim=-45)
plt.axis('off')
plt.savefig('Gaussian curvature.svg', transparent=True)
plt.show()

但是我不知道如何实现我的功能。我想要我的功能并旋转。

答案 1 :(得分:0)

我这样做了,但这不是100%正确的。在x,y = 0处,函数应为= w0 * np.sqrt(1)= w0 = 1.701。事实并非如此。

import numpy as np
from numpy import pi, cos, sin, sqrt, outer, ones
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(111, projection='3d')
u = np.linspace(0, 2 * pi, 100)
v = np.linspace(0, pi, 100)
one_v = ones(100)

w0=1.701
lamb=0.90846
d_in1=45
foc1=38.35
zR=np.pi*w0**(2)/(lamb)


x=np.linspace(-30,30,100)
def f(x):
    return w0*np.sqrt(1+(x/zR)**(2))

# Hyperboloid
v = 2*v/pi - 1
x3 = 6 * outer(cos(u), sqrt(1 + v**2))
y3 = 6 * outer(sin(u), sqrt(1 + v**2)) 
z3 = f(x)
ax.plot_surface(x3, y3, z3, rstride=5, cstride=5, cmap='Spectral',
                linewidth=0.5)

# Fix aspect ratio and axes details
#ax.set_xlim(-13, 13)
#ax.set_ylim(-13, 13)
#ax.set_zlim(-13, 13)
#ax.view_init(elev=35, azim=-45)
#plt.axis('off')
plt.savefig('Gaussian curvature.svg', transparent=True)
plt.show()