使用matplotlib在曲面

使用matplotlib在曲面

本文介绍了使用matplotlib在曲面/轮廓图中绘制3元数据点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些外部程序生成的表面数据作为XYZ值.我想使用matplotlib创建以下图形:

I have some surface data that is generated by an external program as XYZ values. I want to create the following graphs, using matplotlib:

  • 表面图
  • 轮廓图
  • 等高线图与表面图重叠

我看了几个在matplotlib中绘制表面和轮廓的示例-但是,Z值似乎是X和Y的函数,即Y〜f(X,Y).

I have looked at several examples for plotting surfaces and contours in matplotlib - however, the Z values seems to be a function of X and Y i.e. Y ~ f(X,Y).

我认为我将需要转换我的Y变量,但是我还没有看到任何显示该操作方法的示例.

I assume that I will somehow need to transform my Y variables, but I have not seen any example yet, that shows how to do this.

所以,我的问题是这样的:给定一组(X,Y,Z)点,如何从该数据生成曲面和轮廓图?

So, my question is this: given a set of (X,Y,Z) points, how may I generate Surface and contour plots from that data?

顺便说一句,为澄清起见,我不想创建散点图.另外,尽管我在标题中提到了matplotlib,但我不反对使用rpy(2),如果那样的话,我可以创建这些图表.

BTW, just to clarify, I do NOT want to create scatter plots. Also although I mentioned matplotlib in the title, I am not averse to using rpy(2), if that will allow me to create these charts.

推荐答案

用于绘制等高线图,您需要将数据插值到常规网格 http://www.scipy.org/Cookbook/Matplotlib/Gridding_irregularly_spaced_data

for do a contour plot you need interpolate your data to a regular grid 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()

这篇关于使用matplotlib在曲面/轮廓图中绘制3元数据点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 09:27