2010-06-10 136 views
19

我有一些由外部程序生成的曲面数据作为XYZ值。我想创建下列图表,使用matplotlib:使用matplotlib绘制曲面/等值线图中的三元数据点

  • 表面图
  • 等高线图
  • 与曲面图叠加
  • 等高线图

我已经看过了几个例子用来绘制表面和matplotlib中的轮廓 - 但是,Z值似乎是X和Y的函数,即Y〜f(X,Y)。

我假设我会以某种方式转换我的Y变量,但我还没有看到任何示例,它显示了如何执行此操作。所以,我的问题是这样的:给定一组(X,Y,Z)点,我如何从这些数据生成曲面和等高线图?

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

+1

我发布了一个如何将数据放入二维数组以便能够使用matplotlib的表面图的示例:http://stackoverflow.com/a/30539444/3585557。另外,看看这些相关/类似/重复的帖子:http://stackoverflow.com/q/9170838/3585557,http://stackoverflow.com/q/12423601/3585557,http://stackoverflow.com/ q /三百五十八万五千五百五十七分之二千一百一十六万一千八百八十四,http://stackoverflow.com/q/26074542/3585557,http://stackoverflow.com/q/283​​89606/3585557,http://stackoverflow.com/q/29547687/3585557 – 2015-05-30 13:16:44

回答

23

用于做等高线图你需要你的数据插值到规则网格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]]) 

轮廓在3Dhttp://matplotlib.sourceforge.net/examples/mplot3d/contour3d_demo.html

>>> fig = figure() 
>>> ax = Axes3D(fig) 
>>> ax.contour(xi, yi, zi) # ax.contourf for filled contours 
>>> show() 
+0

+1为片段。这有很大帮助。你能否解释你在表面片段中使用的变量(xim和yim)?我无法在任何地方看到它们。 – morpheous 2010-06-10 09:50:11

+0

xim和yim具有来自xi和yi的坐标矩阵。我编辑了答案,添加了一些帮助片段(meshgrid) – remosu 2010-06-10 10:00:38

+0

真棒答案! – ine 2010-06-30 18:02:10

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 +格子:

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

随着熊猫和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() 

Example Image