matplotlib を使用した曲面/等高線プロットでの 3 タプル データ点のプロット

StackOverflow https://stackoverflow.com/questions/3012783

  •  26-09-2019
  •  | 
  •  

質問

外部プログラムによって XYZ 値として生成されたいくつかのサーフェス データがあります。matplotlib を使用して次のグラフを作成したいと考えています。

  • 表面プロット
  • 等高線図
  • 等高線プロットと表面プロットを重ねた

matplotlib で表面と等高線をプロットするためのいくつかの例を見てきましたが、Z 値は X と Y の関数であるようです。Y ~ f(X,Y)。

何らかの方法で Y 変数を変換する必要があると思いますが、これを行う方法を示す例はまだ見ていません。

そこで、私の質問は次のとおりです。(X,Y,Z) 点のセットが与えられた場合、そのデータから表面プロットと等高線プロットを生成するにはどうすればよいでしょうか?

ところで、念のため言っておきますが、私は散布図を作成したくありません。また、タイトルで matplotlib について言及しましたが、これらのチャートを作成できるのであれば、rpy(2) を使用することに抵抗はありません。

役に立ちましたか?

解決

をするために 等高線図 データを通常のグリッドに補間する必要があります http://www.scipy.org/Cookbook/Matplotlib/Gridding_irregulatory_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()

他のヒント

輸入へパンダと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()

する

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