2010-06-10 89 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

相關問題