0


【Python气象处理绘图第七弹–泰勒图绘制】

Python气象处理绘图第七弹–泰勒图绘制


泰勒图绘制


前言

在进行模式评估的过程中,常常需要评估模式的模拟性能,这通常由空间相关系数(CC),相对标准差(SD)及其中心化的均方根误差(RMSE)体现,这三者又常常可以由泰勒图具体体现。RMSE越接近0,CC和SD越接近1,模式模拟能力越好
泰勒图
23.泰勒图


一、数据预处理

对于气象格点数据,其通常为三维数据,但是计算其CC,SD,RMSE时通常需要转成一维数组进行计算。因此需要对气象格点数据进行预处理。

  1. import geopandas as gpd
  2. import salem
  3. # 创造掩膜数据
  4. shp ="E:/shapefile/青藏高原/青藏高原边界数据总集/TPBoundary_HF/TPBoundary_HF_LP.shp"
  5. tp_shp=gpd.read_file(shp)
  6. data0=xr.open_dataset('E:/CORDEX-DOMAIN/conclusion/pr/RX1day.nc')
  7. RX1day=data0.RX1day
  8. data1=xr.open_dataset('E:/CORDEX-DOMAIN/conclusion/pr/RX1day1.nc')
  9. RX1day1=data1.RX1day
  10. data2=xr.open_dataset('E:/CORDEX-DOMAIN/conclusion/pr/RX1day2.nc')
  11. RX1day2=data2.RX1day
  12. data3=xr.open_dataset('E:/CORDEX-DOMAIN/conclusion/pr/RX1day3.nc')
  13. RX1day3=data3.RX1day
  14. RX1day=RX1day.salem.roi(shape=tp_shp)
  15. RX1day1=RX1day1.salem.roi(shape=tp_shp)
  16. RX1day2=RX1day2.salem.roi(shape=tp_shp)
  17. RX1day3=RX1day3.salem.roi(shape=tp_shp)
  18. #将三维数组降为一维
  19. #[数据重组与换形](https://www.jianshu.com/p/3e2eecd6481d)
  20. RX1day=RX1day.stack(z=('time','lat','lon'))
  21. RX1day1=RX1day1.stack(z=('time','lat','lon'))
  22. RX1day2=RX1day2.stack(z=('time','lat','lon'))
  23. RX1day3=RX1day3.stack(z=('time','lat','lon'))
  24. DATA={"RX1day": RX1day,"RX1day1": RX1day1,"RX1day2": RX1day2,"RX1day3": RX1day3,}
  25. df = pd.DataFrame(DATA)
  26. df1=df.dropna(axis=0,subset =["RX1day","RX1day1","RX1day2","RX1day3"]) # 丢弃这四列中有缺失值的行

df和df1

二、使用步骤

1.引入库

代码如下(示例):

  1. from matplotlib.projections import PolarAxes
  2. from mpl_toolkits.axisartist import floating_axes
  3. from mpl_toolkits.axisartist import grid_finder
  4. import numpy as np
  5. import matplotlib.pyplot as plt

2.读入数据

代码如下(示例):

  1. def set_tayloraxes(fig, location):
  2. trans = PolarAxes.PolarTransform()
  3. r1_locs = np.hstack((np.arange(1,10)/10.0,[0.95,0.99]))
  4. t1_locs = np.arccos(r1_locs)
  5. gl1 = grid_finder.FixedLocator(t1_locs)
  6. tf1 = grid_finder.DictFormatter(dict(zip(t1_locs,map(str,r1_locs))))
  7. r2_locs = np.arange(0,2,0.25)
  8. r2_labels =['0 ','0.25 ','0.50 ','0.75 ','REF ','1.25 ','1.50 ','1.75 ']
  9. gl2 = grid_finder.FixedLocator(r2_locs)
  10. tf2 = grid_finder.DictFormatter(dict(zip(r2_locs,map(str,r2_labels))))
  11. ghelper = floating_axes.GridHelperCurveLinear(trans,extremes=(0,np.pi/2,0,1.75),
  12. grid_locator1=gl1,tick_formatter1=tf1,
  13. grid_locator2=gl2,tick_formatter2=tf2)
  14. ax = floating_axes.FloatingSubplot(fig, location, grid_helper=ghelper)
  15. fig.add_subplot(ax)
  16. ax.axis["top"].set_axis_direction("bottom")
  17. ax.axis["top"].toggle(ticklabels=True, label=True)
  18. ax.axis["top"].major_ticklabels.set_axis_direction("top")
  19. ax.axis["top"].label.set_axis_direction("top")
  20. ax.axis["top"].label.set_text("Correlation")
  21. ax.axis["top"].label.set_fontsize(14)
  22. ax.axis["left"].set_axis_direction("bottom")
  23. ax.axis["left"].label.set_text("Standard deviation")
  24. ax.axis["left"].label.set_fontsize(14)
  25. ax.axis["right"].set_axis_direction("top")
  26. ax.axis["right"].toggle(ticklabels=True)
  27. ax.axis["right"].major_ticklabels.set_axis_direction("left")
  28. ax.axis["bottom"].set_visible(False)
  29. ax.grid(True)
  30. polar_ax = ax.get_aux_axes(trans)
  31. rs,ts = np.meshgrid(np.linspace(0,1.75,100),
  32. np.linspace(0,np.pi/2,100))
  33. rms = np.sqrt(1+ rs**2-2*rs*np.cos(ts))
  34. CS = polar_ax.contour(ts, rs,rms,colors='gray',linestyles='--')
  35. plt.clabel(CS,inline=1, fontsize=10)
  36. t = np.linspace(0,np.pi/2)
  37. r = np.zeros_like(t)+1
  38. polar_ax.plot(t,r,'k--')
  39. polar_ax.text(np.pi/2+0.032,1.02," 1.00", size=10.3,ha="right", va="top",
  40. bbox=dict(boxstyle="square",ec='w',fc='w'))return polar_ax
  41. def plot_taylor(axes, refsample, sample,*args,**kwargs):
  42. std = np.std(refsample)/np.std(sample)
  43. corr = np.corrcoef(refsample, sample)
  44. theta = np.arccos(corr[0,1])
  45. t,r = theta,std
  46. d = axes.plot(t,r,*args,**kwargs)return d

两个函数的使用:

(1) set_tayloraxes(fig, location)

输入:

fig: 需要绘图的figure

location:图的位置,如111为1行1列第一个,122为1行2列第2个

输出:

polar_ax:泰勒坐标系

(2) plot_taylor(axes, refsample, sample, *args, **kwargs)

输入:

axes : setup_axes返回的泰勒坐标系

refsample :参照样本

sample :评估样本

args, *kwargs :plt.plot()函数的相关参数,设置点的颜色,形状等等。

  1. fig = plt.figure(figsize=(12,6))
  2. ax1 =set_tayloraxes(fig,121)
  3. d1 =plot_taylor(ax1,df1.RX1day,df1.RX1day1,'ro',markersize=8,label='RX1day')
  4. d2 =plot_taylor(ax1,df1.RX1day,df1.RX1day2,'yo',markersize=8,label='TXn')
  5. d3 =plot_taylor(ax1,df1.RX1day,df1.RX1day3,'mo',markersize=8,label='TX90p')
  6. d4 =plot_taylor(ax1,df1.RX1day,df1.RX1day,'go',markersize=8,label='TX10p')
  7. ax1.legend(loc=2)
  8. ax2 =set_tayloraxes(fig,122)
  9. d1 =plot_taylor(ax2,df1.RX1day,df1.RX1day1,'ro',markersize=8,label='RX1day')
  10. d2 =plot_taylor(ax2,df1.RX1day,df1.RX1day2,'yo',markersize=8,label='TXn')
  11. d3 =plot_taylor(ax2,df1.RX1day,df1.RX1day3,'mo',markersize=8,label='TX90p')
  12. d4 =plot_taylor(ax2,df1.RX1day,df1.RX1day,'go',markersize=8,label='TX10p')#legend位置
  13. ax2.legend(bbox_to_anchor=(1.32,0.8), borderaxespad=0)

fig

总结

以上就是对于泰勒图的绘制。
值得注意的是:

  1. 数据预处理成一维数据
  2. 剔除缺测值(参考dataframe)df1=df.dropna(axis=0,subset = ["RX1day","RX1day1","RX1day2","RX1day3"])
  3. 注意legend的位置Python matplotlib画图时图例说明(legend)放到图像外侧详解

本文转载自: https://blog.csdn.net/weixin_43347581/article/details/129682668
版权归原作者 努力努力再努力搬砖 所有, 如有侵权,请联系我们删除。

“【Python气象处理绘图第七弹–泰勒图绘制】”的评论:

还没有评论