我正在尝试获取10点的2D等高线图
我尝试使用griddata生成我的网格,但是它似乎不起作用,并且我只能在插值网格中看到NAN。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import griddata
xi = np.linspace(0,7500.0,100)
yi = np.linspace(0,7500.0,100)
indie_coords_y=[195,695,1195,1695,2195,2695,3195,3695,4195,4695]
indie_coords_x=[87,90,92,95,97,100,103,105,107,110]
z1_final=[12,13,14,15,16,17,18,19,20,21]
zi = griddata((indie_coords_x, indie_coords_y), z1_final, (xi[None,:],
yi[:,None]), method='linear')
CS = plt.contourf(xi,yi,zi,cmap='jet', vmin=min(z1_final),
vmax=max(z1_final))
当我使用上面的代码时,我看到我的zi数组只有NAN值,而我希望看到一些轮廓
任何人都可以帮忙
答案 0 :(得分:0)
我修改了输入数据(随机播放indie_coords_y
)。
同样,必须对网格的所有点执行插值。 np.meshgrid
用于构造完整的网格。 .flatten()
用于将网格转换为点列表(即,形状为number_of_points x number_of_dim的数组)。插值后,reshape
用于将点列表转换回网格(两个n×n数组)。
现在插值和图形都可以使用了
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import griddata
# Data
indie_coords_y = [195, 2195, 3195, 2695, 3695, 4695, 695, 1195, 1695, 4195] # Modified!
# using np.random.shuffle(indie_coords_y)
indie_coords_x = [87,90,92,95,97,100,103,105,107,110]
z1_final = [12,13,14,15,16,17,18,19,20,21]
# Interpolation
xi = np.linspace(80, 120.0, 30) # modified range
yi = np.linspace(0, 5000.0, 30)
X_grid, Y_grid = np.meshgrid(xi, yi) # Create a grid (i.e. 100x100 arrays)
zi = griddata((indie_coords_x, indie_coords_y), z1_final,
(X_grid.flatten(), Y_grid.flatten()), method='linear')
Z_grid = zi.reshape( X_grid.shape )
# Graph
CS = plt.contourf(X_grid, Y_grid, Z_grid, cmap='jet')
plt.plot(indie_coords_x, indie_coords_y, 'ko', label='data points')
plt.plot(X_grid.flatten(), Y_grid.flatten(), 'r,', label='interpolation points')
plt.xlabel('x'); plt.ylabel('y');
plt.colorbar(); plt.legend();
图形为: