努力学编程啊 2022-09-02 10:57 采纳率: 50%
浏览 79
已结题

python坐标轴设置

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

img

我用的python版本为3.9

import os
os.environ['PROJ_LIB']='D:\\Anaconda\\Library\\share'
os.chdir("D:\硕士研究生")
os.getcwd()
from netCDF4 import Dataset
import numpy as np
import matplotlib.pyplot as plt
import 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]/30

lat_U2hPa=lat_U2hPa100_130-lat_U2hPa
lat_T2hPa=lat_T2hPa100_130-lat_T2hPa
lat_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]/35
for 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)

  • 写回答

2条回答 默认 最新

    报告相同问题?

    相关推荐 更多相似问题

    问题事件

    • 系统已结题 9月11日
    • 已采纳回答 9月3日
    • 创建了问题 9月2日

    悬赏问题

    • ¥15 js 实现内容输入框内文字高亮
    • ¥15 IP地址聚合什么情况才能同时聚合,什么情况又是先集合两个再加上最后一个
    • ¥15 mysql 选择求解答
    • ¥15 数据挖掘 决策树,文件报错
    • ¥15 编写生产者和消费者程序
    • ¥15 CSDN是个怎么样的平台
    • ¥15 java识别字符串内容并运算(输入为一行字符串,输出为整数)
    • ¥15 eclipse Java项目问题
    • ¥20 用记事本写一个login.jsp ,用以实现登陆界面,并通过post方式向Servlet传送页面数据
    • ¥15 关于链表的问题(为什么要在del函数结束后再创一个变量y)