8

假设您有一条二维曲线,例如:

from matplotlib import pylab
t = numpy.linspace(-1, 1, 21)
z = -t**2
pylab.plot(t, z)

产生

http://i.imgur.com/feQzk.png

我想进行革命以实现 3d 绘图(请参阅http://reference.wolfram.com/mathematica/ref/RevolutionPlot3D.html)。绘制 3d 表面不是问题,但它不会产生我期望的结果:

http://i.imgur.com/ljXHQ.png

如何在 3d 图中执行这条蓝色曲线的旋转?

4

1 回答 1

4

你的图似乎使用笛卡尔网格。matplotlib 网站上有一些关于 3D 圆柱函数的示例,例如 Z = f(R)(此处: http: //matplotlib.org/examples/mplot3d/surface3d_radial_demo.html)。那是你要找的吗?以下是我使用您的函数 Z = -R**2 得到的结果:Z = -R**2 函数的图

并为您的函数添加截止,请使用以下示例:(需要 matplotlib 1.2.0)

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

fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)

Z = -(abs(X) + abs(Y))

## 1) Initial surface
# Flatten mesh arrays, necessary for plot_trisurf function
X = X.flatten()
Y = Y.flatten()
Z = Z.flatten()

# Plot initial 3D surface with triangles (more flexible than quad)
#surfi = ax.plot_trisurf(X, Y, Z, cmap=cm.jet, linewidth=0.2)

## 2) Cut off
# Get desired values indexes
cut_idx = np.where(Z > -5)

# Apply the "cut off"
Xc = X[cut_idx]
Yc = Y[cut_idx]
Zc = Z[cut_idx]

# Plot the new surface (it would be impossible with quad grid)
surfc = ax.plot_trisurf(Xc, Yc, Zc, cmap=cm.jet, linewidth=0.2)

# You can force limit if you want to compare both graphs...
ax.set_xlim(-5,5)
ax.set_ylim(-5,5)
ax.set_zlim(-10,0)

plt.show()

冲浪的结果:

冲浪

和冲浪:

冲浪

于 2012-11-21T13:51:25.280 回答