我有一些表面的数据是由外部程序作为XYZ值。我想创建的以下图表,使用matplotlib:

  • 表面积
  • 轮廓图
  • 轮廓图复盖的面积

我已经看了几个实例绘制的表面和轮廓在matplotlib-但是,Z值似乎是一个功能的X和Y即Y-f(X,Y)。

我认为我会以某种方式需要改变我的Y变量,但是我还没有看到任何实例,显示如何做到这一点。

因此,我的问题是这个:给予一个组(X、Y、Z)点,我怎么可能产生的表面和轮廓图从这些数据?

顺便说一句,只是为了澄清,我不想创建分散的地块。还有虽然我提及的matplotlib在标题,我不反对使用解算(2)如果将允许我创建这些图表。

有帮助吗?

解决方案

对于做一个 轮廓图 你需要插你的数据常规的电网 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()

其他提示

与大熊猫和顽固进口和处理数据、与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()

例的图像

轮廓图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)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top