如何绘制具有非恒定半径的圆柱体

时间:2016-07-17 10:05:29

标签: python matplotlib

我编写了代码来生成一个具有固定半径的圆柱体:

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

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

theta = np.linspace(-2*pi,2*pi, 600)
Z = np.linspace(0,1,700)

Z,theta = np.meshgrid(Z, theta)

R = 0.1
X = (R*np.cos(theta))
Y = (R*np.sin(theta))

ax.plot_surface(X,Y,Z,linewidth = 0,facecolor = 'r', shade = True, alpha = 0.6)
plt.show()

如何更改此值以使圆柱半径可以变化。例如,圆柱体一端的半径从0.1开始,每个连续的圆圈开始。绘制的半径比以前多0.01?换句话说,我想连接'不同半径的圆圈一起形成一个半径不定的圆柱体。

1 个答案:

答案 0 :(得分:3)

更改了R = np.linspace(0,1,700)

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

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

theta = np.linspace(-2*pi,2*pi, 600)
Z = np.linspace(0,1,700)

Z,theta = np.meshgrid(Z, theta)

R = np.linspace(0,1,700)
X = (R*np.cos(theta))
Y = (R*np.sin(theta))

ax.plot_surface(X,Y,Z,linewidth = 0,facecolor = 'r', shade = True, alpha = 0.6)
plt.show()

您还可以尝试R = np.sin(np.linspace(0,1,700) * 4) + 1

这样的功能