如何在matplotlib中进行3D旋转图? [英] How to do a 3D revolution plot in matplotlib?
问题描述
假设您有2D曲线,例如:
Suppose you have a 2D curve, given by e.g.:
from matplotlib import pylab
t = numpy.linspace(-1, 1, 21)
z = -t**2
pylab.plot(t, z)
产生
我想进行一次革命以获得3d图(请参阅 http://reference.wolfram.com/mathematica/ref/RevolutionPlot3D.html ).绘制3d曲面不是问题,但不会产生我期望的结果:
I would like to perform a revolution to achieve a 3d plot (see http://reference.wolfram.com/mathematica/ref/RevolutionPlot3D.html). Plotting a 3d surface is not the problem, but it does not produce the result I'm expecting:
如何在3d图中旋转此蓝色曲线?
How can I perform a rotation of this blue curve in the 3d plot ?
推荐答案
您在图形上绘制的图似乎使用笛卡尔网格.在matplotlib网站上有一些3D圆柱函数的示例,例如Z = f(R)(在这里: http://matplotlib.org/examples/mplot3d/surface3d_radial_demo.html ). 那是你要找的东西吗? 以下是我通过您的函数Z = -R ** 2获得的结果:
Your plot on your figure seems to use cartesian grid. There is some examples on the matplotlib website of 3D cylindrical functions like Z = f(R) (here: http://matplotlib.org/examples/mplot3d/surface3d_radial_demo.html). Is that what you looking for ? Below is what I get with your function Z = -R**2 :
并使用以下示例将截止添加到您的函数: (需要matplotlib 1.2.0)
And to add cut off to your function, use the following example: (matplotlib 1.2.0 required)
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()
surfi的结果:
和冲浪:
这篇关于如何在matplotlib中进行3D旋转图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!