我有一些由外部程序生成的表面数据作为 XYZ 值。我想使用 matplotlib 创建以下图表:
- 曲面图
- 等高线图
- 等高线图与曲面图重叠
我查看了几个在 matplotlib 中绘制曲面和轮廓的示例 - 但是,Z 值似乎是 X 和 Y 的函数,即 Y ~ f(X,Y)。
我假设我需要以某种方式转换我的 Y 变量,但我还没有看到任何示例来说明如何执行此操作。
所以,我的问题是:给定一组 (X,Y,Z) 点,我如何从这些数据中生成曲面图和等高线图?
顺便说一句,为了澄清,我不想创建散点图。此外,虽然我在标题中提到了 matplotlib,但我并不反对使用 rpy(2),如果这样可以让我创建这些图表。
最佳答案
为了制作等高线图,您需要将数据插入到常规网格http://www.scipy.org/Cookbook/Matplotlib/Gridding_irregularly_spaced_data
一个简单的例子:
>>> xi = linspace(min(X), max(X))
>>> yi = linspace(min(Y), max(Y))
>>> zi = griddata(X, Y, Z, xi, yi)
>>> contour(xi, yi, zi)
对于表面 http://matplotlib.sourceforge.net/examples/mplot3d/surface3d_demo.html
>>> from mpl_toolkits.mplot3d import Axes3D
>>> fig = figure()
>>> ax = Axes3D(fig)
>>> xim, yim = meshgrid(xi, yi)
>>> ax.plot_surface(xim, yim, zi)
>>> show()
>>> help(meshgrid(x, y))
Return coordinate matrices from two coordinate vectors.
[...]
Examples
--------
>>> X, Y = np.meshgrid([1,2,3], [4,5,6,7])
>>> X
array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3],
[1, 2, 3]])
>>> Y
array([[4, 4, 4],
[5, 5, 5],
[6, 6, 6],
[7, 7, 7]])
3D 轮廓 http://matplotlib.sourceforge.net/examples/mplot3d/contour3d_demo.html
>>> fig = figure()
>>> ax = Axes3D(fig)
>>> ax.contour(xi, yi, zi) # ax.contourf for filled contours
>>> show()
关于python - 使用 matplotlib 在曲面/等高线图中绘制 3 元组数据点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3012783/