英语教学法python栅格数据出图可视化
import numpy as np
s as ccrs
import cartopy.mpl.ticker as cticker
泰安东湖公园
import matplotlib.pyplot as plt
import cartopy.feature as cfeature
from mpl_toolkits.bamap import Bamap
import cartopy.io.shapereader as shpreader
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
from matplotlib import rcParams
from osgeo import gdal
import matplotlib.patches as mpatches
防疫诗config ={"font.family":'Times New Roman',"font.size":18,"mathtext.fontt":'stix'}
rcParams.update(config)
#------------------------------读取栅格------------------------
def Read_Tiif(fileName):
#输⼊信息,⽂件名
#输出信息,data_array,im_bands,im_geotrans,im_proj
#分别是,矩阵数据,波段数,放射变换矩阵信息,投影信息
datat = gdal.Open(fileName)
if datat ==None:
print(fileName+"⽂件⽆法打开")
return
im_width = datat.RasterXSize #栅格矩阵的列数
im_height = datat.RasterYSize #栅格矩阵的⾏数
im_bands = datat.RasterCount #波段数
if im_bands <1:
print("影像没有可使⽤数据!")
return
im_data = datat.ReadAsArray(0,0,im_width,im_height)#获取数据
im_geotrans = datat.GetGeoTransform()#获取仿射矩阵信息
6061是什么材质im_proj = datat.GetProjection()#获取投影信息
if im_bands ==1:
data_array = im_data[0:im_height,0:im_width]#获取第Band_num波段
if im_bands >1:
data_array = im_data[:,0:im_height,0:im_width]#获取第Band_num波段
return data_array,im_width,im_height,im_bands,im_geotrans,im_proj
#-----------函数:添加指北针--------------
def add_north(ax, labelsize=16, loc_x=0.1, loc_y=0.85, width=0.04, height=0.13, pad=0.14):
"""
画⼀个⽐例尺带'N'⽂字注释
主要参数如下
:param ax: 要画的坐标区域 Axes实例 a()获取即可
:param labelsize: 显⽰'N'⽂字的⼤⼩
:
param loc_x: 以⽂字下部为中⼼的占整个ax横向⽐例
:param loc_y: 以⽂字下部为中⼼的占整个ax纵向⽐例
:param width: 指南针占ax⽐例宽度
:param height: 指南针占ax⽐例⾼度
:param pad: ⽂字符号占ax⽐例间隙
:return: None
"""
minx, maxx = ax.get_xlim()
miny, maxy = ax.get_ylim()
ylen = maxy - miny
xlen = maxx - minx
left =[minx + xlen*(loc_x - width*.5), miny + ylen*(loc_y - pad)]
right =[minx + xlen*(loc_x + width*.5), miny + ylen*(loc_y - pad)]
top =[minx + xlen*loc_x, miny + ylen*(loc_y - pad + height)]
center =[minx + xlen*loc_x, left[1]+(top[1]- left[1])*.4]
triangle = mpatches.Polygon([left, top, right, center], color='k')
安危的意思
<(s='N',
x=minx + xlen*loc_x,
y=miny + ylen*(loc_y - pad + height),
fontsize=labelsize,
horizontalalignment='center',
verticalalignment='bottom')
ax.add_patch(triangle)
## 数据读⼊
input_file = r'D:\exe\anaconda\file\make_map\PT_1981_08_05.tif'
data_array,im_width,im_height,im_bands,im_geotrans,im_proj = Read_Tiif(input_file)
left_lon,width_res,uperlat,heigh_res = im_geotrans[0], im_geotrans[1], im_geotrans[3],im_geotrans[5]
#lon = np.linspace(left_lon,24,25) * im_width
lon = np.arange(im_width)* width_res + left_lon
lat = np.arange(im_height)* heigh_res + uperlat
## 位势⾼度
data_array = data_array
data_array[np.where(data_array >1)]= np.nan
data_array[np.where(data_array <-1)]= np.nan
data_array = anspo(1,0)
#hgt = HGT[:,:,5,:]
HHGT = data_array
[LON,LAT]= np.meshgrid(lon,lat)
LON = anspo(1,0)
LAT = anspo(1,0)
#print(U.variables.keys())
#创建画布
fig = plt.figure(figsize=(12,8))
ax=plt.axes(projection=ccrs.PlateCarree(central_longitude=85))
# 标注坐标轴
leftlon, rightlon, lowerlat, upperlat =(89,127,25,55)
img_extent =[leftlon, rightlon, lowerlat, upperlat]
ax.t_extent([leftlon, rightlon, lowerlat, upperlat], crs=ccrs.PlateCarree())
ax.t_xticks([90,95,100,105,110,115,120,125], crs=ccrs.PlateCarree())
ax.t_yticks([25,30,35,40,45,50,55], crs=ccrs.PlateCarree())
#zero_direction_label⽤来设置经度的0度加不加E和W
lon_formatter = LongitudeFormatter(zero_direction_label=Fal)
lat_formatter = LatitudeFormatter()
ax.xaxis.t_major_formatter(lon_formatter)
ax.yaxis.t_major_formatter(lat_formatter)
胸前长
ax.t_title('Nei Meng SSM(v)',loc='center',fontsize =20)
# 加载中国地图
china = shpreader.Reader(r'D:\exe\anaconda\file\make_map\shp\shp\neimengg.dbf').geometries()
#绘制中国国界省界九段线等等梦想翻译
ax.add_geometries(china, ccrs.PlateCarree(),facecolor='none', edgecolor='black',zorder =1)
# 绘制风场和位势图
urf(LON[:],LAT[:],HHGT[:],100,cmap='gist_rainbow',transform=ccrs.PlateCarree(),extend='both')
#urf(LON[:],LAT[:],HHGT[:],40,cmap='gist_rainbow',transform=ccrs.PlateCarree(),extend='both')#其中100是颜⾊的变化区间数⽬#Q = ax.quiver(LON[:], LAT[:], UUwnd[:], VVwnd[:], color='black',width=0.0018,scale=100,headwidth=4,transform=ccrs.PlateCarree()) # position=fig.add_axes([0.2, 0.1, 0.5, 0.03])#位置[左,下,右,上]
value_min = np.min(HHGT)
音乐三要素value_max = np.max(HHGT)
add_north(ax)
lorbar(cf,orientation='vertical',shrink=0.92,aspect=20,fraction=.03,pad=0.01)
#lorbar(cf,orientation='vertical',shrink=0.92,aspect=20,fraction=.03,pad=0.01)
#cbar = lorbar(cf,orientation='horizontal',fraction=0.07, pad=0.05)
lorbar)
plt.savefig(r'D:\exe\anaconda\file\make_map\plot33.png',dpi=800)
plt.show()