2016-09-27 17 views
2

私はPythonの初心者です。私は、各カップルのためのZ値を有するmatplotlib x、y、z値からの2Dプロット

IはXのリストを持っているが

x_list = [-1,2,10,3] 

値とIは、Yのリストが

y_list = [3,-3,4,7] 

の値を有します。概略的に、これはそのように動作します:

X Y Z 
-1 3 5 
2 -3 1 
10 4 2.5 
3 7 4.5 

とZ値がz_list = [5,1,2.5,4.5]に格納されています。 X軸のX値、Y軸のY値、および各カップルのZ値を強度マップで表した2Dプロットを取得する必要があります。 これは私が失敗し、試してみましたものです:

X, Y = np.meshgrid(x_list, y_list) 
fig, ax = plt.subplots() 
extent = [x_list.min(), x_list.max(), y_list.min(), y_list.max()] 
im=plt.imshow(z_list, extent=extent, aspect = 'auto') 
plt.colorbar(im) 
plt.show() 

これが正しく行わ取得する方法は?ここで

答えて

1

はそれを行うための一つの方法です:

import matplotlib.pyplot as plt 
import nupmy as np 
from matplotlib.colors import LogNorm 

x_list = np.array([-1,2,10,3]) 
y_list = np.array([3,-3,4,7]) 
z_list = np.array([5,1,2.5,4.5]) 

N = int(len(z_list)**.5) 
z = z_list.reshape(N, N) 
plt.imshow(z, extent=(np.amin(x_list), np.amax(x_list), np.amin(y_list), np.amax(y_list)), norm=LogNorm(), aspect = 'auto') 
plt.colorbar() 
plt.show() 

enter image description here

私は、このリンクをたどっ:How to plot a density map in python?

1

問題はimshow(z_list, ...)は基本的に、z_list(n,m)型の配列であることを期待するということです値のグリッド。 imshow関数を使用するには、各格子点にZ値を設定する必要があります。これは、より多くのデータを収集したり、補間したりすることで実現できます。ここで

は、線形補間を使用してデータを使用して、例です。

from scipy.interpolate import interp2d 

# f will be a function with two arguments (x and y coordinates), 
# but those can be array_like structures too, in which case the 
# result will be a matrix representing the values in the grid 
# specified by those arguments 
f = interp2d(x_list,y_list,z_list,kind="linear") 

x_coords = np.arange(min(x_list),max(x_list)+1) 
y_coords = np.arange(min(y_list),max(y_list)+1) 
Z = f(x_coords,y_coords) 

fig = plt.imshow(Z, 
      extent=[min(x_list),max(x_list),min(y_list),max(y_list)], 
      origin="lower") 

# Show the positions of the sample points, just to have some reference 
fig.axes.set_autoscale_on(False) 
plt.scatter(x_list,y_list,400,facecolors='none') 

enter image description here

あなたはそれがで示さx_listy_listで指定されたサンプル・ポイントで正しい値を(表示されていることがわかります半円)が、内挿の性質とサンプル点の数が少ないため、他の場所でのバラツキが大きくなります。

関連する問題