使用matplotlib绘制三元组数据点的表面/等高线图

22
我有一些由外部程序生成的XYZ值的表面数据。我想使用matplotlib创建以下图形:
- 表面图 - 等高线图 - 等高线图覆盖表面图
我查看了matplotlib中绘制表面和等高线的几个示例,但发现Z值似乎是X和Y的函数,即Y〜f(X,Y)。
我假设我需要以某种方式转换我的Y变量,但我还没有看到任何示例,显示如何执行此操作。 因此,我的问题是:给定一组(X,Y,Z)点,如何从该数据生成表面和等高线图?
顺便说一句,我不想创建散点图。尽管我在标题中提到了matplotlib,但如果可以让我创建这些图表,我不反对使用rpy(2)。

1
我发布了一个示例,展示如何将数据放入2-D数组中以便使用matplotlib的表面绘图:https://dev59.com/pWox5IYBdhLWcg3wf0Xl#30539444。此外,请查看这些相关/类似/重复的帖子:https://dev59.com/pWox5IYBdhLWcg3wf0Xl,https://dev59.com/Ymct5IYBdhLWcg3wAY3q,https://dev59.com/i3vaa4cB1Zd3GeqPCWDE,http://stackoverflow.com/q/26074542/3585557,http://stackoverflow.com/q/28389606/3585557,https://dev59.com/zYnda4cB1Zd3GeqPAYnU。 - Steven C. Howell
3个回答

25

要进行等高线图,您需要将数据插值到一个规则的网格上。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()

+1 对于这段代码片段。这非常有帮助。您能否解释一下在 surface 代码片段中使用的变量(xim 和 yim)?我没有看到它们在任何地方被定义。 - morpheous
xim和yim具有由xi和yi生成的坐标矩阵。我编辑了答案,添加了一些help(meshgrid)的片段。 - remosu
1
如果数据已经呈网格形式,但仅格式化为三列X、Y和Z,则是否有一种简单的pythonic方法将其转换为适用于imshow/contour绘图的格式? - weemattisnot

2
使用pandas和numpy来导入和操作数据,使用matplot.pylot.contourf来绘制图像。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.mlab import griddata

PATH='/YOUR/CSV/FILE'
df=pd.read_csv(PATH)

#Get the original data
x=df['COLUMNNE']
y=df['COLUMNTWO']
z=df['COLUMNTHREE']

#Through the unstructured data get the structured data by interpolation
xi = np.linspace(x.min()-1, x.max()+1, 100)
yi = np.linspace(y.min()-1, y.max()+1, 100)
zi = griddata(x, y, z, xi, yi, interp='linear')

#Plot the contour mapping and edit the parameter setting according to your data (http://matplotlib.org/api/pyplot_api.html?highlight=contourf#matplotlib.pyplot.contourf)
CS = plt.contourf(xi, yi, zi, 5, levels=[0,50,100,1000],colors=['b','y','r'],vmax=abs(zi).max(), vmin=-abs(zi).max())
plt.colorbar()

#Save the mapping and save the image
plt.savefig('/PATH/OF/IMAGE.png')
plt.show()

示例图片


1

使用rpy2 + ggplot2绘制轮廓图:

from rpy2.robjects.lib.ggplot2 import ggplot, aes_string, geom_contour
from rpy2.robjects.vectors import DataFrame

# Assume that data are in a .csv file with three columns X,Y,and Z
# read data from the file
dataf = DataFrame.from_csv('mydata.csv')

p = ggplot(dataf) + \
    geom_contour(aes_string(x = 'X', y = 'Y', z = 'Z'))
p.plot()

rpy2 + lattice 绘制表面图:

from rpy2.robjects.packages import importr
from rpy2.robjects.vectors import DataFrame
from rpy2.robjects import Formula

lattice = importr('lattice')
rprint = robjects.globalenv.get("print")

# Assume that data are in a .csv file with three columns X,Y,and Z
# read data from the file
dataf = DataFrame.from_csv('mydata.csv')

p = lattice.wireframe(Formula('Z ~ X * Y'), shade = True, data = dataf)
rprint(p)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接