天气学原理插图复现(八)——588线的绘制

使用数据:hgt.mon.ltm.1991-2020.nc

使用库:Matplotlib、NumPy、netCDF4、Cartopy

添加标签

import netCDF4 as nc
import matplotlib.pyplot as plt 
import numpy as np
import cartopy.crs as ccrs                                               
import cartopy.feature as cf
import matplotlib.ticker as mticker
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
from cartopy.util import add_cyclic_point
from scipy.interpolate import interp2d


#中文显示
plt.rcParams['font.sans-serif']= ['Microsoft YaHei'] 
plt.rcParams['axes.unicode_minus'] = False

def pa(para_month):
    para_level = 500
    f = nc.Dataset(r"hgt.mon.ltm.1991-2020.nc",'r')
    levels = f.variables['level'][:]
    lat = f.variables['lat'][:]
    lon0 = f.variables['lon'][:]
    time = nc.num2date(f.variables['time'][:],f.variables['time'].units).data
    months = np.array([i.month for i in time])
    hgt = f.variables['hgt'][:]
    index_level = np.where(levels==para_level)[0][0] 
    index_month = np.where(months==para_month)[0][0] 
    hgt = hgt[index_month][index_level,:,:]
    hgt, lon = add_cyclic_point(hgt, coord=lon0)
    Lon, Lat = np.meshgrid(lon,lat)
    return Lon,Lat,hgt

    
K=['red','green','blue','purple']
labels=['5月','6月','7月','8月']
K2=['red','green','blue']
labels2=['8月','9月','10月']
fig=plt.figure(figsize=(3,3),dpi=400)
#001
ax = plt.subplot(211,projection=ccrs.LambertConformal(central_longitude=125))
for i in [5,6,7,8]:
    Lon1,Lat1,hgt1=pa(i)
    #Lon2,Lat2,hgt2=cz(Lon1,Lat1,hgt1)
    ac=ax.contour(Lon1,Lat1, hgt1,range(5879, 5881,30),colors=K[i-5],linewidths=0.5,linestyles='-',transform=ccrs.PlateCarree())
    #ax.clabel(ac,fmt='%d月'%i,fontsize=2,inline=False)

#ax=fig.add_axes([0,0,1,1],projection=ccrs.LambertConformal(central_longitude=125))
ax.add_feature(cf.LAND.with_scale('110m'))
ax.add_feature(cf.OCEAN.with_scale('110m'))
ax.add_feature(cf.COASTLINE.with_scale('50m'),lw=0.2)
ax.add_feature(cf.RIVERS.with_scale('50m'),lw=0.4)
################################################################
gl=ax.gridlines(draw_labels=True,linewidth=0.1 ,x_inline=False, y_inline=False,color='k')
gl.top_labels=False #关闭上部经纬标签                                  
gl.right_labels=False
gl.xformatter = LONGITUDE_FORMATTER  #使横坐标转化为经纬度格式            
gl.yformatter = LATITUDE_FORMATTER                                        
gl.xlocator=mticker.FixedLocator(np.arange(100,170,10))                                   
gl.ylocator=mticker.FixedLocator(np.arange(0,50,10)) 
gl.xlabel_style={'size':2}#修改经纬度字体大小                             
gl.ylabel_style={'size':2}
ax.spines['geo'].set_linewidth(0.2)#调节边框粗细
ax.set_extent([100,160,5,40],crs=ccrs.PlateCarree())
#ax.set_title('LambertConformal地图',fontsize=5)
x=[361,362]
y=[91,92]
for i in range(len(labels)):
    plt.plot(x,y,color=K[i],label=labels[i],lw=0.5)
plt.legend(loc='upper left',fontsize=2.5)



#002
ax = plt.subplot(212,projection=ccrs.LambertConformal(central_longitude=125))
for i in [8,9,10]:
    Lon1,Lat1,hgt1=pa(i)
    ac=ax.contour(Lon1,Lat1, hgt1,range(5879, 5881,30),colors=K[i-8],linewidths=0.5,linestyles='-',transform=ccrs.PlateCarree())
    #ax.clabel(ac,fmt='%d月'%i,fontsize=2,inline=False)
#ax=fig.add_axes([0,0,1,1],projection=ccrs.LambertConformal(central_longitude=125))
ax.add_feature(cf.LAND.with_scale('110m'))
ax.add_feature(cf.OCEAN.with_scale('110m'))
ax.add_feature(cf.COASTLINE.with_scale('50m'),lw=0.2)
ax.add_feature(cf.RIVERS.with_scale('50m'),lw=0.4)
################################################################
gl=ax.gridlines(draw_labels=True,linewidth=0.1 ,x_inline=False, y_inline=False,color='k')
gl.top_labels=False #关闭上部经纬标签                                  
gl.right_labels=False
gl.xformatter = LONGITUDE_FORMATTER  #使横坐标转化为经纬度格式            
gl.yformatter = LATITUDE_FORMATTER                                        
gl.xlocator=mticker.FixedLocator(np.arange(100,170,10))                                   
gl.ylocator=mticker.FixedLocator(np.arange(0,50,10)) 
gl.xlabel_style={'size':2}#修改经纬度字体大小                             
gl.ylabel_style={'size':2}
ax.spines['geo'].set_linewidth(0.2)#调节边框粗细
ax.set_extent([100,160,5,40],crs=ccrs.PlateCarree())
#ax.set_title('LambertConformal地图',fontsize=5)
x=[361,362]
y=[91,92]
for i in range(len(labels2)):
    plt.plot(x,y,color=K[i],label=labels2[i],lw=0.5)
plt.legend(loc='upper left',fontsize=2.5)


plt.suptitle("图5-4-4   500hPa西太平洋副高脊的月平均位置",fontsize=4)
plt.savefig("图5-4-4(5-8,8-10月)2.png",dpi=800)
plt.show()

 直接书写

import netCDF4 as nc
import matplotlib.pyplot as plt 
import numpy as np
import cartopy.crs as ccrs                                               
import cartopy.feature as cf
import matplotlib.ticker as mticker
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
from cartopy.util import add_cyclic_point
from scipy.interpolate import interp2d


#中文显示
plt.rcParams['font.sans-serif']= ['Microsoft YaHei'] 
plt.rcParams['axes.unicode_minus'] = False

def pa(para_month):
    para_level = 500
    f = nc.Dataset(r"hgt.mon.ltm.1991-2020.nc",'r')
    levels = f.variables['level'][:]
    lat = f.variables['lat'][:]
    lon0 = f.variables['lon'][:]
    time = nc.num2date(f.variables['time'][:],f.variables['time'].units).data
    months = np.array([i.month for i in time])
    hgt = f.variables['hgt'][:]
    index_level = np.where(levels==para_level)[0][0] 
    index_month = np.where(months==para_month)[0][0] 
    hgt = hgt[index_month][index_level,:,:]
    hgt, lon = add_cyclic_point(hgt, coord=lon0)
    Lon, Lat = np.meshgrid(lon,lat)
    return Lon,Lat,hgt


    
K=['red','green','blue','purple']
fig=plt.figure(figsize=(3,3),dpi=400)
#001
ax = plt.subplot(211,projection=ccrs.LambertConformal(central_longitude=125))
for i in [5,6,7,8]:
    Lon1,Lat1,hgt1=pa(i)
    #Lon2,Lat2,hgt2=cz(Lon1,Lat1,hgt1)
    ac=ax.contour(Lon1,Lat1, hgt1,range(5879, 5881,30),colors=K[i-5],linewidths=0.5,linestyles='-',transform=ccrs.PlateCarree())
    #ax.clabel(ac,fmt='%d月'%i,fontsize=2,inline=False)

#ax=fig.add_axes([0,0,1,1],projection=ccrs.LambertConformal(central_longitude=125))
ax.add_feature(cf.LAND.with_scale('110m'))
ax.add_feature(cf.OCEAN.with_scale('110m'))
ax.add_feature(cf.COASTLINE.with_scale('50m'),lw=0.2)
ax.add_feature(cf.RIVERS.with_scale('50m'),lw=0.4)
################################################################
gl=ax.gridlines(draw_labels=True,linewidth=0.1 ,x_inline=False, y_inline=False,color='k')
gl.top_labels=False #关闭上部经纬标签                                  
gl.right_labels=False
gl.xformatter = LONGITUDE_FORMATTER  #使横坐标转化为经纬度格式            
gl.yformatter = LATITUDE_FORMATTER                                        
gl.xlocator=mticker.FixedLocator(np.arange(100,170,10))                                   
gl.ylocator=mticker.FixedLocator(np.arange(0,50,10)) 
gl.xlabel_style={'size':2}#修改经纬度字体大小                             
gl.ylabel_style={'size':2}
ax.spines['geo'].set_linewidth(0.2)#调节边框粗细
ax.set_extent([100,160,5,40],crs=ccrs.PlateCarree())
#ax.set_title('LambertConformal地图',fontsize=5)
ax.set_title("5-8月",fontsize=3,x=0.13,y=0.8,color='k')
ax.text(-2500000, 0, '5月(——)', color='red',fontsize=2,weight='bold')
ax.text(-2500000, -200000, '6月(——)', color='green',fontsize=2,weight='bold')
ax.text(-2500000, -400000, '7月(——)', color='blue',fontsize=2,weight='bold')
ax.text(-2500000, -600000, '8月(——)', color='purple',fontsize=2,weight='bold')


#002
ax = plt.subplot(212,projection=ccrs.LambertConformal(central_longitude=125))
for i in [8,9,10]:
    Lon1,Lat1,hgt1=pa(i)
    ac=ax.contour(Lon1,Lat1, hgt1,range(5879, 5881,30),colors=K[i-8],linewidths=0.5,linestyles='-',transform=ccrs.PlateCarree())
    #ax.clabel(ac,fmt='%d月'%i,fontsize=2,inline=False)
#ax=fig.add_axes([0,0,1,1],projection=ccrs.LambertConformal(central_longitude=125))
ax.add_feature(cf.LAND.with_scale('110m'))
ax.add_feature(cf.OCEAN.with_scale('110m'))
ax.add_feature(cf.COASTLINE.with_scale('50m'),lw=0.2)
ax.add_feature(cf.RIVERS.with_scale('50m'),lw=0.4)
################################################################
gl=ax.gridlines(draw_labels=True,linewidth=0.1 ,x_inline=False, y_inline=False,color='k')
gl.top_labels=False #关闭上部经纬标签                                  
gl.right_labels=False
gl.xformatter = LONGITUDE_FORMATTER  #使横坐标转化为经纬度格式            
gl.yformatter = LATITUDE_FORMATTER                                        
gl.xlocator=mticker.FixedLocator(np.arange(100,170,10))                                   
gl.ylocator=mticker.FixedLocator(np.arange(0,50,10)) 
gl.xlabel_style={'size':2}#修改经纬度字体大小                             
gl.ylabel_style={'size':2}
ax.spines['geo'].set_linewidth(0.2)#调节边框粗细
ax.set_extent([100,160,5,40],crs=ccrs.PlateCarree())
#ax.set_title('LambertConformal地图',fontsize=5)
ax.set_title("8-10月",fontsize=3,x=0.135,y=0.8,color='k')
ax.text(-2500000, 0, '  8月(——)', color='red',fontsize=2,weight='bold')
ax.text(-2500000, -200000, '  9月(——)', color='green',fontsize=2,weight='bold')
ax.text(-2500000, -400000, '10月(——)', color='blue',fontsize=2,weight='bold')

plt.suptitle("图5-4-4   500hPa西太平洋副高脊的月平均位置",fontsize=4)
plt.savefig("图5-4-4(5-8,8-10月).png",dpi=800)
plt.show()

直接添加标签一直添加不上,后来灵光一现,先在地图外画线再添加标签。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值