Cartory地图可视化

# 从整体上来看,先看边界层高度的情况
# 整理气象数据
# 读数据
import netCDF4 as nc
import pandas as pd
import numpy as np

file1 = r'C:\Users\LHW\Desktop\Hubei task120210918\meteology\adaptor.mars.internal-1631981155.7723448-28568-14-37c609d7-a545-4a66-b4e3-3f73bf08819b.nc'
dataset1 = nc.Dataset(file1)
lon1=dataset1.variables['longitude'][:]
lat1=dataset1.variables['latitude'][:]
BLH=dataset1.variables['blh'][:]

BLH20190203=np.mean(BLH[745:2160,:,:],axis=0)
BLH20200203=np.mean(BLH[3625:5064,:,:],axis=0)
delta=BLH20190203-BLH20200203

from scipy import interpolate
import matplotlib.pyplot as plt
import numpy as np

# 2. Construct the interpolation function on the original grid 
xx, yy = np.meshgrid(lon1, lat1)
f= interpolate.interp2d(xx,yy,delta,kind='linear') ## this is the interpolation function

# 3. Use the interpolation function to calculate values on a finer grid
xnew = np.linspace(lon1.min(), lon1.max(), 401) # we use 101 points to get even spacing
ynew = np.linspace(lat1.min(), lat1.max(), 401)
Zinterp = f(xnew, ynew)

import geopandas
from shapely import geometry
import matplotlib.pyplot as plt
import json



import matplotlib.pyplot as plt
import matplotlib.path as mpath
import matplotlib.ticker as mticker
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
import cartopy.mpl.ticker as cticker
import cartopy.io.shapereader as shpreader
import seaborn as sns
from matplotlib import rcParams
import matplotlib as mpl
mpl.rcParams["font.family"] = 'Times New Roman'  #默认字体类型
mpl.rcParams["mathtext.fontset"] = 'cm' #数学文字字体
mpl.rcParams["font.size"] = 26  #字体大小

font = {
    
    'family' : 'Times New Roman',
        'color'  : 'black',
        'weight' : 'normal',
        'size'   : 25,
        }
fig1 = plt.figure(figsize=(14,7))

########################################################################################另外三幅子图
ax2 = fig1.add_subplot(1,1,1, projection=ccrs.PlateCarree())
gl=ax2.gridlines(crs=ccrs.PlateCarree(), draw_labels=True,
                  linewidth=1, color='gray', alpha=0.5, linestyle='--')
gl.bottom_labels=False #关闭上部经纬标签                                  
gl.right_labels=False

gl.xlocator=mticker.FixedLocator(np.arange(107,117,2))      
gl.ylocator=mticker.FixedLocator(np.arange(27,35,2)) 

# gl.xlabel_style={'size':3}#修改经纬度字体大小                             
# gl.ylabel_style={'size':3}
ax2.spines['geo'].set_linewidth(2)#调节边框粗细

ax2.add_feature(cfeature.COASTLINE.with_scale('110m'))
img_extent1=[107,117,28,34]
ax2.set_extent(img_extent1, ccrs.PlateCarree())

lon,lat=np.meshgrid(xnew,ynew)
c7 = ax2.pcolormesh(lon,lat,Zinterp,transform=ccrs.PlateCarree(),cmap=plt.cm.bwr_r,vmin=-70,vmax=70,alpha=0.8)

position=fig1.add_axes([0.85, 0.131, 0.04, 0.75])#位置[左,下,宽度,高度]
cb=plt.colorbar(c7,cax=position)
cb.ax.tick_params(labelsize=24)  #设置色标刻度字体大小
cb.outline.set_linewidth(2)

#########################################################################绘制省边界线
data = geopandas.read_file('中国.json')
a=data.geometry.to_json()
polygon_dict = json.loads(a)
features=len(polygon_dict["features"][:])
point_list=[]
for i in range(features):
    #print('features****************************************')
    coordinates=len(polygon_dict["features"][i]["geometry"]["coordinates"][:])
    for j in range(coordinates):
        #print('coordinates-----------------------------------')
        rows=int(len(np.array(polygon_dict["features"][i]["geometry"]["coordinates"][j]).flatten())/2)
        
        point=(np.array(polygon_dict["features"][i]["geometry"]["coordinates"][j]).flatten()).reshape(rows,2)
        #print(point)
        ax2.plot(point[:,0],point[:,1],transform=ccrs.PlateCarree(),linewidth=2,color='k')
        point_list.append(point)
ini=point_list[0]
num=len(point_list)
for k in range(1,num):
    ini=np.vstack((ini,point_list[k]))
########################################################################绘制市边界线
data = geopandas.read_file('湖北省.json')
a=data.geometry.to_json()
polygon_dict = json.loads(a)
features=len(polygon_dict["features"][:])
point_list=[]
for i in range(features):
    #print('features****************************************')
    coordinates=len(polygon_dict["features"][i]["geometry"]["coordinates"][:])
    for j in range(coordinates):
        #print('coordinates-----------------------------------')
        rows=int(len(np.array(polygon_dict["features"][i]["geometry"]["coordinates"][j]).flatten())/2)
        
        point=(np.array(polygon_dict["features"][i]["geometry"]["coordinates"][j]).flatten()).reshape(rows,2)
        #print(point)
        ax2.scatter(point[:,0][::2],point[:,1][::2],transform=ccrs.PlateCarree(),marker='.',s=1,color='k')
        point_list.append(point)
ini=point_list[0]
num=len(point_list)
for k in range(1,num):
    ini=np.vstack((ini,point_list[k]))
#############################################################################

plt.savefig('test.jpg',dpi=300)

猜你喜欢

转载自blog.csdn.net/weixin_45577825/article/details/120453270