Matplotlib:mplot3d¶
日期 | 2010-03-30(最后修改),2006-02-17(创建) |
---|
以下示例展示了使用 matplotlib 的简单 3D 绘图。matplotlib 的 3D 功能是通过整合 John Porter 的 mplot3d 模块添加的,因此不再需要额外下载,以下示例将在更新的 matplotlib 安装中运行。'''注意,此代码在 matplotlib-0.98 分支中不受支持,但如果您需要此功能,可以使用最新的 0.99 版本或 0.91 维护版本。''' 或者,Mayavi2 项目为广泛的 3D 绘图提供了一个类似 pylab 的 API:http://code.enthought.com/projects/mayavi/docs/development/html/mayavi/mlab.html
请注意,此页面上的并非所有示例都是最新的,因此其中一些可能无法正常工作。有关其他示例,请参阅 http://matplotlib.sourceforge.net/examples/mplot3d/
3D 绘图示例
In [ ]
#!python
from numpy import *
import pylab as p
#import matplotlib.axes3d as p3
import mpl_toolkits.mplot3d.axes3d as p3
# u and v are parametric variables.
# u is an array from 0 to 2*pi, with 100 elements
u=r_[0:2*pi:100j]
# v is an array from 0 to 2*pi, with 100 elements
v=r_[0:pi:100j]
# x, y, and z are the coordinates of the points for plotting
# each is arranged in a 100x100 array
x=10*outer(cos(u),sin(v))
y=10*outer(sin(u),sin(v))
z=10*outer(ones(size(u)),cos(v))
线框图(在 0.87.5 上有效)
In [ ]
#!python
fig=p.figure()
ax = p3.Axes3D(fig)
ax.plot_wireframe(x,y,z)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
p.show()
3D 图
In [ ]
#!python
# this connects each of the points with lines
fig=p.figure()
ax = p3.Axes3D(fig)
# plot3D requires a 1D array for x, y, and z
# ravel() converts the 100x100 array into a 1x10000 array
ax.plot3D(ravel(x),ravel(y),ravel(z))
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
fig.add_axes(ax)
p.show()
散点图(在 0.87.5 上有效,显示了一些伪影)
In [ ]
#!python
fig=p.figure()
ax = p3.Axes3D(fig)
# scatter3D requires a 1D array for x, y, and z
# ravel() converts the 100x100 array into a 1x10000 array
ax.scatter3D(ravel(x),ravel(y),ravel(z))
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
p.show()
曲面图(在 0.87.5 上有效)
In [ ]
#!python
fig=p.figure()
ax = p3.Axes3D(fig)
# x, y, and z are 100x100 arrays
ax.plot_surface(x,y,z)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
p.show()
Contour3D(在 0.87.5 上有效)
In [ ]
#!python
delta = 0.025
x = arange(-3.0, 3.0, delta)
y = arange(-2.0, 2.0, delta)
X, Y = p.meshgrid(x, y)
Z1 = p.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = p.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
# difference of Gaussians
Z = 10.0 * (Z2 - Z1)
fig=p.figure()
ax = p3.Axes3D(fig)
ax.contour3D(X,Y,Z)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
p.show()
Contourf3D
In [ ]
#!python
# in mplt3D change:
# levels, colls = self.contourf(X, Y, Z, 20)
# to:
# C = self.contourf(X, Y, Z, *args, **kwargs)
# levels, colls = (C.levels, C.collections)
fig=p.figure()
ax = p3.Axes3D(fig)
ax.contourf3D(X,Y,Z)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
fig.add_axes(ax)
p.show()
2D 等高线图(在 0.87.5 上有效)
In [ ]
#!python
x=r_[-10:10:100j]
y=r_[-10:10:100j]
z= add.outer(x*x, y*y)
### Contour plot of z = x**2 + y**2
p.contour(x,y,z)
### ContourF plot of z = x**2 + y**2
p.figure()
p.contourf(x,y,z)
p.show()
要查看其他一些 3d 绘图功能的示例,请运行以下命令。有关更多信息,请参阅 matplotlib/axes3d.py 的源代码
In [ ]
#!python
# note that for the following to work you have to modify the test funcitons in your site-packages/matplotlib/axes3d.py like this:
#def test_xxxx():
# import pylab
# ax = Axes3D(pylab.figure())
# ....
# ....
# pylab.show()
# the following then work on 0.87.5
p3.test_bar2D()
p3.test_contour()
p3.test_scatter()
p3.test_scatter2D()
p3.test_surface()
# the following fail on 0.87.5
p3.test_plot()
p3.test_polys()
p3.test_wir
部分作者:Unknown[2],AndrewStraw,Unknown[9],Unknown[125],Unknown[123],Unknown[126],Unknown[83],GaelVaroquaux,Unknown[127],MartinSpacek
附件