python坐标轴设置

CSDN问答 2022-09-09 02:18:09 阅读数:70

Python设置坐标坐标轴

python
怎么设置y轴为指定经纬度?就像这幅图一样。这是代码

img

我用的python版本为3.9

import osos.environ['PROJ_LIB']='D:\\Anaconda\\Library\\share'os.chdir("D:\硕士研究生")os.getcwd()from netCDF4 import Datasetimport numpy as npimport matplotlib.pyplot as pltimport matplotlib.ticker as mticker#def colormap():# import matplotlib.colors as colors# cdict=[(1,1,1),(0,1,1),(0,157/255,1),(0,0,1),(9/255,130/255,175/255),(0,1,0),(8/255,175/255,20/255),(1,214/255,0),(1,152/255,0),(1,0,0),(221/255,0,27/255),(188/255,0,54/255),(121/255,0,109/255),(121/255,51/255,160/255),(195/255,163/255,212/255),]# return (colors.ListedColormap(cdict,'indexed'))data_file='201412-100.nc'nc_file=Dataset(data_file)#print(nc_file)var_names=nc_file.variables.keys()#print(var_names)all_vars=nc_file.variables.items()#print(all_vars)#print(nc_file.variables['u'])lev=nc_file.variables['t']#print(lev)lev1=nc_file.variables['latitude']#print(lev1)U=nc_file.variables["u"][:,:,:,:]T=nc_file.variables["t"][:,:,:,:]shape_U=np.shape(U)print(shape_U)lat_U2hPa=np.zeros((31,91),dtype='f')lat_U2hPa100_130=np.zeros((31,91),dtype='f')lon_U2hPa=np.zeros((31,360),dtype='f')lon_U2hPa35_70=np.zeros((31,360),dtype='f')lon_U2hPareal=np.zeros((360,31),dtype='f')lon_U2hPa35_70real=np.zeros((360,31),dtype='f')sum=np.zeros(31,dtype='f')sum1=np.zeros(31,dtype='f')lat_T2hPa=np.zeros((31,91),dtype='d')lat_T2hPa100_130=np.zeros((31,91),dtype='d')lon_T2hPa=np.zeros((31,360),dtype='d')lon_T2hPa35_70=np.zeros((31,360),dtype='d')lon_T2hPareal=np.zeros((360,31),dtype='f')lon_T2hPa35_70real=np.zeros((360,31),dtype='f')for i in np.arange(shape_U[0]): for j in np.arange(shape_U[2]): for k in np.arange(shape_U[3]): lat_U2hPa[i,j]+=U[i,1,j,k] lat_T2hPa[i,j]+=T[i,1,j,k] lat_U2hPa[i,j]=lat_U2hPa[i,j]/shape_U[3] lat_T2hPa[i,j]=lat_T2hPa[i,j]/shape_U[3]for i in np.arange(shape_U[0]): for j in np.arange(shape_U[2]): for k in np.arange(280,310): lat_U2hPa100_130[i,j]+=U[i,1,j,k] lat_T2hPa100_130[i,j]+=T[i,1,j,k] lat_U2hPa100_130[i,j]=lat_U2hPa100_130[i,j]/30 lat_T2hPa100_130[i,j]=lat_T2hPa100_130[i,j]/30lat_U2hPa=lat_U2hPa100_130-lat_U2hPalat_T2hPa=lat_T2hPa100_130-lat_T2hPalat_U2hPa=lat_U2hPa.transpose(1,0)lat_T2hPa=lat_T2hPa.transpose(1,0)for i in np.arange(shape_U[0]): for j in np.arange(shape_U[3]): for k in np.arange(20,55): lon_U2hPa35_70[i,j]+=U[i,1,k,j] lon_T2hPa35_70[i,j]+=T[i,1,k,j] lon_U2hPa35_70[i,j]=lon_U2hPa35_70[i,j]/35 lon_T2hPa35_70[i,j]=lon_T2hPa35_70[i,j]/35for i in np.arange(shape_U[0]): for j in np.arange(shape_U[3]): sum[i]+=lon_U2hPa35_70[i,j] sum1[i]+=lon_T2hPa35_70[i,j] sum[i]=sum[i]/shape_U[3] sum1[i]=sum1[i]/shape_U[3] for j in np.arange(shape_U[3]): lon_U2hPa[i,j]=lon_U2hPa35_70[i,j]-sum[i] lon_T2hPa[i,j]=lon_T2hPa35_70[i,j]-sum1[i] lon_U2hPa=lon_U2hPa.transpose(1,0)lon_T2hPa=lon_T2hPa.transpose(1,0)for i in np.arange(0,31): for j in np.arange(0,360): if j <180: lon_U2hPareal[j,i]=lon_U2hPa[j+180,i] lon_T2hPareal[j,i]=lon_T2hPa[j+180,i] lon_U2hPareal[j,i]=lon_U2hPa[j-180,i] lon_T2hPareal[j,i]=lon_T2hPa[j-180,i]print(np.shape(lon_U2hPa))print(np.shape(lon_T2hPareal))#绘图#print(np.shape(lat_U))x=np.arange(1,32)#print(np.shape(Y))y=nc_file.variables["latitude"][:]y1=nc_file.variables["longitude"][:]print(np.shape(y1))#print(np.shape(X))X,Y=np.meshgrid(x,y)X1,Y1=np.meshgrid(x,y1)fg,axes=plt.subplots(2,1)fg.set_size_inches(5,8)lvl=np.arange(-45,55,5)lv2=np.arange(-20,30,5)im1=axes[0].contourf(X,Y,lat_U2hPa,levels=lvl,cmap='jet',extend='both')im2=axes[0].contour(X,Y,lat_T2hPa,levels=lv2,colors='k')axes[0].set_xlabel('date',fontsize=10)axes[0].clabel(im2,fontsize=8)axes[0].yaxis.set_major_formatter(mticker.FormatStrFormatter('%.0f°N'))lv3=np.arange(-20,30,5)im3=axes[1].contourf(X1,Y1,lon_U2hPareal,levels=lvl,cmap='jet',extend='both')im4=axes[1].contour(X1,Y1,lon_T2hPareal,levels=lv3,colors='k')axes[1].clabel(im4,fontsize=8)axes[1].set_xlabel('date',fontsize=10)c1=np.arange(181)c2=np.arange(180)c3=c2[::-1]pallels=np.concatenate((c1,c3),axis=0)colum_y=[0,30,60,90,120,150,180,150,120,90,60,30]axes[1].set_yticks(pallels,colum_y)axes[1].yaxis.set_major_formatter(mticker.FormatStrFormatter('%.0f°E'))fg.text(0.15,0.5,'Dec',fontsize=10,horizontalalignment='right')fg.text(0.15,0.08,'Dec',fontsize=10,horizontalalignment='right')cbar=fg.colorbar(im1,ax=axes)
版权声明:本文为[CSDN问答]所创,转载请带上原文链接,感谢。 https://ask.csdn.net/questions/7781184