2016-12-12 20 views
0

z値に対応する特定のx値とy値が配列で区切られている場合、どのように等高線プロットを作成しますか?たとえば、次のようにPythonのX、Y、Zに対応する3つの異なる配列を持つ等高線図の作成

Array 1 (X): 
1 
4 
6 
7 
8 
2 
6 

Array 2 (Y): 
7 
7 
8 
9 
0 
1 
2 

Array 3 (Z): 
8 
9 
7 
1 
2 
2 
3 

私はX1、Y1 = np.meshgrid(X、Y)を行い、何とかZ列を整形する必要がありますか? meshgridを使用せずにこれを行う別の方法はありますか?また、4番目の配列を追加し、特定のZ1に対応する同じx値とy値を持つZ1という名前を付けると、この等高線図を最初の等高線図と共にプロットできますか?

答えて

2

をあなたが規則的な格子を持っていない場合は、三角形の面補間を使用することは良い選択かもしれません。

この例と上記の例では、長いデータがある場合は、プロットの境界を確認するだけです。

import numpy as np 
import seaborn as sns 
import matplotlib.pyplot as plt 
import matplotlib.tri as tri 

sns.set(style="white") 

x = np.array([1,4,6,7,8,2,6]) 
y = np.array([7,7,8,9,0,1,2]) 
z = np.array([8,9,7,1,2,2,3]) 

fig = plt.figure(figsize=(10, 10)) 
ax = fig.add_subplot(111) 

nptsx, nptsy = 100, 100 
xg, yg = np.meshgrid(np.linspace(x.min(), x.max(), nptsx), 
        np.linspace(y.min(), y.max(), nptsy)) 

triangles = tri.Triangulation(x, y) 
tri_interp = tri.CubicTriInterpolator(triangles, z) 
zg = tri_interp(xg, yg) 

# change levels here according to your data 
levels = np.linspace(0, 10, 5) 
colormap = ax.contourf(xg, yg, zg, levels, 
         cmap=plt.cm.Blues, 
         norm=plt.Normalize(vmax=z.max(), vmin=z.min())) 

# plot data points 
ax.plot(x, y, color="#444444", marker="o", linestyle="", markersize=10) 

# add a colorbar 
fig.colorbar(colormap, 
      orientation='vertical', # horizontal colour bar 
      shrink=0.85) 

# graph extras: look at xlim and ylim 
ax.set_xlim((0, 10)) 
ax.set_ylim((0, 10)) 
ax.set_aspect("equal", "box") 

plt.show() 

この

が出力される。

enter image description here

1

私はあなたが補間を行う必要があると思う:

import numpy as np 
import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import axes3d 
from scipy import interpolate 
x = np.array([1,4,6,7,8,2,6]) 
y = np.array([7,7,8,9,0,1,2]) 
z = np.array([8,9,7,1,2,2,3]) 
points = np.column_stack((x,y)) 
values = z.T 

gridx, gridy = np.mgrid[0:8:100j, 0:8:100j] 
gridz = interpolate.griddata(points, values, (gridx, gridy), method='cubic') 

fig = plt.figure(figsize=(12,5)) 

ax1 = fig.add_subplot(121,projection='3d') 
ax1.plot3D(x,y,z, 'k.', ms=10) 
ax1.contour(gridx,gridy,gridz) 

ax2 = fig.add_subplot(122,projection='3d') 
ax2.plot3D(x,y,z, 'k.', ms=10) 
ax2.plot_wireframe(gridx, gridy, gridz,rstride=5,cstride=5) 
plt.savefig('contour_wire.png') 

これが与える:

enter image description here

+1

これは非常に有用であるが、私はそれぞれx、y、zのアレイの411点の大規模なデータセットを持っていることを言います。このプログラムで何を変える必要がありますか? – Cosmoman

+0

@Cosmoman:z = f(x、y)のような解析関数がありますか? – Mahdi

+0

いいえ、私はちょうど特定のz値(配列3)に対応する(x(配列1)、y(配列2)を持つ)3つの配列を持っています。 – Cosmoman

関連する問題