使用插值方法填充包含NaN值的网格,以便用等高线绘图。

huangapple go评论124阅读模式
英文:

Interpolating grid with NaN values for plotting with contour

问题

我有一个表格,是从一个更大的数据集中创建的,它给我提供了我想要绘制的 y、x 和 z 值。我创建了一个网格,然后对这些值进行插值,但我认为沿着行的插值函数并没有正确进行,因为在图中可以看到在纬度 78-80 附近有数据断裂点(参见图像),这让我认为沿着行的插值没有正确执行。是否有人有关于如何平滑这些数据的提示?

  1. aou_df = df_1994.pivot_table(index='CTDPRS', columns='LATITUDE', values='AOU')
  2. aou_df = aou_df.interpolate(method='linear', limit_area='inside', axis=0)
  3. ##绘制 AOU 1994
  4. y = ([4.2, 4.7, 4.8, 4.9, 5.4, 9.1, 9.6, 9.7, 10.0, 10.1, ... 4287.3],
  5. x = [72.13, 73.0, 73.49, 73.98, 74.5, 75.0, 75.45, 75.75, 75.94, 76.62, ... 90.0],
  6. z = [[-12.29372749, nan, nan, ..., nan, nan, nan],
  7. [nan, nan, -43.41465869, ..., nan, nan, nan],
  8. [nan, -54.49999783, nan, ..., nan, nan, nan],
  9. ...,
  10. [nan, nan, nan, ..., nan, nan, nan],
  11. [nan, nan, nan, ..., nan, 55.87256821, nan],
  12. [nan, nan, nan, ..., 55.39665852, nan, 55.05005376]])
  13. xi, yi = np.meshgrid(x, y, indexing='ij')
  14. # from matplotlib.colors import LogNorm
  15. plt.figure(figsize=(25, 10))
  16. levels = np.linspace(-135, 135)
  17. # cbar = plt.colorbar(ticks=(-85, -65, -45, -25, -5, 15, 35, 55, 75, 95, 115, 135))
  18. plt.contourf(xi, yi, z, cmap='jet', levels=levels, vmin=-135, vmax=135)
  19. plt.gca().invert_yaxis()
  20. plt.gca().invert_xaxis()
  21. cbar = plt.colorbar(ticks=(-135, -110, -85, -60, -35, 0, 35, 60, 85, 110, 135), extend='both')
  22. cbar.set_label('AOU', fontsize=18)
  23. cbar.ax.tick_params(labelsize=18)
  24. plt.xlabel('LAT', fontsize=18)
  25. plt.ylabel('Pressure (dbar)', fontsize=18)
  26. plt.ylim(bottom=1000)
  27. plt.xticks(fontsize=18)
  28. plt.yticks(fontsize=18)
  29. plt.plot(x, range(len(x)), 'gD', clip_on=False, markersize=10)
  30. # plt.xlim(left=80)

使用插值方法填充包含NaN值的网格,以便用等高线绘图。

英文:

I have a table I create from a larger dataset which gives me the y, x, and z values that I want to plot. I create a grid and then interpolate the values, but I don't think the function interpolation along the rows correctly since I can see data as break-up points around Latitude 78-80 in the plot (see image), this makes me think that the interpolation is not been done correctly along the rows. Does anyone have a tip on how to smooth this data?

  1. aou_df = df_1994.pivot_table(index='CTDPRS', columns = 'LATITUDE', values='AOU')
  2. aou_df = aou_df.interpolate(method='linear', limit_area='inside', axis =0 )
  3. ##Plotting AOU 1994
  4. y = ([ 4.2, 4.7, 4.8, 4.9, 5.4, 9.1, 9.6, 9.7,
  5. 10.0, 10.1,
  6. ...
  7. 3568.2, 3608.6, 3818.6, 3824.9, 3866.7, 3979.1, 4013.4, 4133.1,
  8. 4159.3, 4287.3],
  9. x= ([72.13, 73.0, 73.49, 73.98, 74.5, 75.0, 75.45, 75.75, 75.94,
  10. 76.62, 77.33, 77.78, 78.14, 78.15, 78.98, 79.98, 80.15, 80.16,
  11. 80.33, 80.71, 81.24, 81.58, 82.47, 83.17, 84.06, 84.85, 85.89,
  12. 87.16, 88.06, 88.79, 88.86, 88.95, 89.02, 90.0],
  13. z = [[-12.29372749, nan, nan, ..., nan,
  14. nan, nan],
  15. [ nan, nan, -43.41465869, ..., nan,
  16. nan, nan],
  17. [ nan, -54.49999783, nan, ..., nan,
  18. nan, nan],
  19. ...,
  20. [ nan, nan, nan, ..., nan,
  21. nan, nan],
  22. [ nan, nan, nan, ..., nan,
  23. 55.87256821, nan],
  24. [ nan, nan, nan, ..., 55.39665852,
  25. nan, 55.05005376]])
  26. xi, yi = np.meshgrid(x,y,indexing='ij')
  27. #from matplotlib.colors import LogNorm
  28. plt.figure(figsize=(25,10))
  29. levels = np.linspace(-135,135)
  30. #cbar = plt.colorbar(ticks=(-85,-65,-45,-25,-5,15,35,55,75,95, 115,135))
  31. plt.contourf(xi,yi,z, cmap = 'jet', levels=levels,vmin=-135, vmax=135)
  32. plt.gca().invert_yaxis()
  33. plt.gca().invert_xaxis()
  34. cbar = plt.colorbar(ticks=(-135,-110,-85,-60,-35,0,35,60,85,110,135), extend= 'both')
  35. cbar.set_label('AOU', fontsize=18)
  36. cbar.ax.tick_params(labelsize=18)
  37. plt.xlabel('LAT',fontsize=18)
  38. plt.ylabel('Pressure (dbar)' ,fontsize=18)
  39. plt.ylim(bottom = 1000)
  40. plt.xticks(fontsize=18)
  41. plt.yticks(fontsize=18)
  42. plt.plot(x,range(len(x)),'gD', clip_on=False, markersize=10)
  43. #plt.xlim(left = 80)

使用插值方法填充包含NaN值的网格,以便用等高线绘图。

答案1

得分: 1

好的,值在一个表格中...抱歉...我更新了代码...尝试运行...

尝试这样做... 你可以传递 method='Linear'method='Cubic'griddata

  1. from scipy.interpolate import griddata
  2. ### 获取有效数据
  3. xdata, ydata = np.meshgrid(x, y)
  4. not_nan = ~np.isnan(xdata) & ~np.isnan(ydata) & ~np.isnan(z)
  5. i, j = np.argwhere(not_nan).T
  6. xval = xdata[i, j]
  7. yval = ydata[i, j]
  8. zval = z[i, j]
  9. xy = np.column_stack((xval, yval))
  10. ### 网格
  11. nx_mesh = 100
  12. ny_mesh = 100
  13. xi, yi = np.meshgrid(np.linspace(xval.min(), xval.max(), nx_mesh),
  14. np.linspace(yval.min(), yval.max(), ny_mesh))
  15. ### 插值
  16. zi = griddata(xy, zval, (xi, yi), method='nearest')
  17. ### 将 xi, yi 和 zi 传递给等高线图....

编辑:这是使用 method='nearest' 的 Joao 代码的结果:
使用插值方法填充包含NaN值的网格,以便用等高线绘图。

这是 'linear' 结果:
使用插值方法填充包含NaN值的网格,以便用等高线绘图。

英文:

Ok, the values are in a table... I'm sorry... I updated the code... Try to run...

Try this... You can to pass method='Linear' and method='Cubic' to griddata

  1. from scipy.interpolate import griddata
  2. ### getting the valid data
  3. xdata, ydata = np.meshgrid(x, y)
  4. not_nan = ~np.isnan(xdata) & ~np.isnan(ydata) & ~np.isnan(z)
  5. i, j = np.argwhere(not_nan).T
  6. xval = xdata[i, j]
  7. yval = ydata[i, j]
  8. zval = z[i, j]
  9. xy = np.column_stack((xval, yval))
  10. ### mesh
  11. nx_mesh = 100
  12. ny_mesh = 100
  13. xi, yi = np.meshgrid(np.linspace(xval.min(), xval.max(), nx_mesh),
  14. np.linspace(yval.min(), yval.max(), ny_mesh))
  15. ### interpolation
  16. zi = griddata(xy, zval, (xi, yi), method='nearest')
  17. ### Pass xi, yi and zi to contour....

EDIT: This is the result from Joao's code using method='nearest'
使用插值方法填充包含NaN值的网格,以便用等高线绘图。

This is the 'linear' result:
使用插值方法填充包含NaN值的网格,以便用等高线绘图。

huangapple
  • 本文由 发表于 2023年5月25日 04:38:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76327245.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定