如何绘制关于y轴的多项式函数的旋转实体?

时间:2017-04-24 10:47:40

标签: python matplotlib plot 3d

关于y轴的函数y = x**2的旋转实体可以使用下面的代码绘制

import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as axes3d
from matplotlib import cm

np.seterr(divide='ignore', invalid='ignore')

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')


ll, ul = 0, 1 
u = np.linspace(ll, ul, 60)
v = np.linspace(0, 2*np.pi, 60)
U, V = np.meshgrid(u, v)

Z = U

X = np.sqrt(Z)*np.cos(V)
Y = np.sqrt(Z)*np.sin(V)

ax.set_xlabel('Y axis')
ax.set_ylabel('X axis')
ax.set_zlabel('Z axis')

ax.plot_surface(X, Y, Z, cmap=plt.cm.YlGnBu_r)

plt.show()

这段代码所做的只是绘制函数x = sqrt(y)的旋转实体,它与y = x**2相反。但是如何为以下函数绘制革命的实体:

y = x**5 + x**4 + x**3 + x**2 + x

1 个答案:

答案 0 :(得分:2)

(U,V)参数空间到(X,Y,Z)坐标的映射可以非常灵活。 通常U被选为np.linspace(ll, ul, 100),并且被视为等于Y(如果y是旋转轴)。但是你不必那样使用U。 相反,U可以代表半径:

ll, ul = 0, 1 
u = np.linspace(ll, ul, 100)
v = np.linspace(0, 2*np.pi, 60)
U, V = np.meshgrid(u, v)

然后XYZ可以用半径U来定义:

Y = U**5 + U**4 + U**3 + U**2 + U
X = U*np.cos(V)
Z = U*np.sin(V)
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as axes3d
from matplotlib import cm

np.seterr(divide='ignore', invalid='ignore')

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ll, ul = 0, 1 
u = np.linspace(ll, ul, 100)
v = np.linspace(0, 2*np.pi, 60)
U, V = np.meshgrid(u, v)

Y = U**5 + U**4 + U**3 + U**2 + U
X = U*np.cos(V)
Z = U*np.sin(V)

ax.set_xlabel('Y axis')
ax.set_ylabel('X axis')
ax.set_zlabel('Z axis')

ax.plot_surface(X, Y, Z, cmap=plt.cm.YlGnBu_r)

plt.show()

enter image description here