使用matplotlib在表面/轮廓图中绘制三元组数据点

22 投票
3 回答
38813 浏览
提问于 2025-04-15 23:47

我有一些由外部程序生成的表面数据,这些数据是以XYZ值的形式存在的。我想用matplotlib来创建以下几种图表:

  • 表面图
  • 轮廓图
  • 叠加了表面图的轮廓图

我查看了几个关于在matplotlib中绘制表面和轮廓的例子,但我发现Z值似乎是X和Y的函数,也就是说,Y是X和Y的某种关系。

我猜我可能需要以某种方式转换我的Y变量,但我还没有看到任何例子说明该怎么做。

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

顺便说一下,为了澄清,我并不想创建散点图。虽然我在标题中提到了matplotlib,但如果使用rpy(2)能让我创建这些图表,我也不介意。

3 个回答

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)
2

使用pandas和numpy来导入和处理数据,然后用matplotlib的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()

示例图像

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()

撰写回答