Python+Cartopy绘制已投影影像

36 篇文章 44 订阅

前言

数据是一副投影为UTM 50°N的地温影像, 使用ax.contourf绘制已投影栅格时需要指定参考坐标系ccrs 和每一个数据点的投影坐标系坐标。
(2) 数据读取部分的代码省略,是通过gdal读取的,readTifAsArray函数前面的博客中可以找到。

代码

指定投影并绘制

import cartopy.crs as ccrs
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter, LatitudeLocator
from matplotlib.pyplot import MultipleLocator
import matplotlib.ticker as mticker
# from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt

file = r'G:\MERSI-2_MOD_MYD\sub\MERSI2_sim_LST_sub'
data = readTifAsArray(file)[0]

proj = ccrs.UTM(50, southern_hemisphere=False) # 定义UTM投影, 50指定分区为50, southern_hemisphere 指定是否在南半球
fig = plt.figure(figsize=(9,6))
ax1 = plt.subplot(121, projection=proj)
ax2 = plt.subplot(122, )

# 下面获取每个数据点的投影坐标值,这里的xy坐标的极值是我在QGIS里获取的,应该通过读取仿射变换参数来自动读取和计算,相关代码前面博客里也有
lons = np.linspace(868010.906, 929010.906, data.shape[1])
lats = np.linspace(4819462.621, 4880462.621, data.shape[0])[::-1]

clevs = np.linspace(290, 315, 100)
map_car = ax1.contourf(lons, lats, data, clevs, transform=proj,
            cmap=plt.cm.jet)       

绘制经纬线,相关属性设置可参考官方文档

gl = ax1.gridlines(alpha=0.5, linestyle='--', draw_labels=True, 
                   dms=True, x_inline=False, y_inline=False, )
# ax1.set_extent([120, 123, 42, 45], crs=ccrs.PlateCarree()) # 设置子图显示范围
gl.top_labels = 1
gl.left_labels = 1
# gl.xlines = 1

设置经纬度坐标显示间隔为 0.2,mticker 更多设置参照 matplotlib.ticker文档

gl.xlocator = mticker.MultipleLocator(0.2) 
gl.ylocator = mticker.MultipleLocator(0.2)
gl.xformatter = LongitudeFormatter(number_format='.1f', degree_symbol='')

设置经纬度标识格式,x和y lable的样式可以和matlibplot的text一样通过字典来设置

lat_formatter = LatitudeFormatter(number_format='.1f',
                                  degree_symbol='')
# ax2.xaxis.set_major_formatter(lon_formatter)
gl.ylabel_style = {'color': 'blue', 'weight': 'bold','family': 'Times New Roman', 'rotation':90}
gl.xlabel_style = {'color': 'black', 'weight': 'bold', 'family': 'Times New Roman',}

子图2绘制

ax2.set_xticks(np.arange(0,10),)
ax2.text(6, 0.5,'gridlines', fontdict={'size':30,'color': 'red', 'family': 'Times New Roman', 'rotation':90})

绘制结果

在这里插入图片描述

在设置经纬度标识格式的时候我通过'rotation':90指定了纬度标识垂直显示,这样在多子图绘制时会比较美观。但实际上纬度标识并没有旋转90°。而ax2中的text通过'rotation':90成功垂直显示。这个问题目前没有解决,有知道怎么设置的同仁可以评论私信我,感谢!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值