Python 空间绘图 - Cartopy 经纬度添加

2021-02-22 12:06:19 浏览数 (1)

本节提要:尽量符合实际应用要求的刻度与经纬度



基于很多同志询问添加经纬度办法,系统性重编了地图的经纬度添加方式。各种投影中以矩形投影PlateCarree最为方便,可以套用matplotlib.mticker的形式。在最新的0.18版本的cartopy中,虽然还不完善,但是终于能直接绘制兰勃脱下的标签了。墨卡托在官网上有示例。

一、PlateCarree投影下的两种投影方式

(1)gridlines方式

这个方式是cartopy自带的,也只能在cartopy中使用。需要引入cartopy的经纬度格式部件,然后正常添加。这种方式属于对全部投影的普适办法,但是在矩形投影上就不如matplotlib的方式。

代码语言:javascript复制
import matplotlib.pyplot as plt                                          
import cartopy.crs as ccrs                                               
import cartopy.feature as cf
import numpy as np
import matplotlib.ticker as mticker
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
plt.rcParams['font.sans-serif']=['SimHei']
fig=plt.figure(figsize=(2,2),dpi=400)
ax=fig.add_axes([0,0,1,1],projection=ccrs.PlateCarree(central_longitude=120))
ax.add_feature(cf.LAND.with_scale('110m'))
ax.add_feature(cf.OCEAN.with_scale('110m'))
ax.add_feature(cf.COASTLINE.with_scale('110m'),lw=0.4)
ax.add_feature(cf.RIVERS.with_scale('110m'),lw=0.4)
################################################################
gl=ax.gridlines(draw_labels=True,linestyle=":",linewidth=0.3,color='k')
gl.top_labels=False #关闭上部经纬标签                                  
gl.right_labels=False
gl.xformatter = LONGITUDE_FORMATTER  #使横坐标转化为经纬度格式            
gl.yformatter = LATITUDE_FORMATTER                                        
gl.xlocator=mticker.FixedLocator(np.arange(-180,180,30))      
gl.ylocator=mticker.FixedLocator(np.arange(-90,90,30)) 
gl.xlabel_style={'size':3}#修改经纬度字体大小                             
gl.ylabel_style={'size':3}
ax.spines['geo'].set_linewidth(0.5)#调节边框粗细
ax.set_title('gridlines经纬度风格',fontsize=5)

(2)matplotlib的坐标修改方式

这种方式在矩形投影中比较灵活,还可以仿制NCL风格的图片。minorticks_on这种召唤副刻度的方式比较简便,但是不能对刻度间隔进行修改。引入matplotlib.ticker的方式稍微复杂,但是能对刻度间隔进行灵活修改。

代码语言:javascript复制
import matplotlib.pyplot as plt                                          
import cartopy.crs as ccrs                                               
import cartopy.feature as cf                                             
from cartopy.mpl.ticker import LongitudeFormatter,LatitudeFormatter
plt.rcParams['font.sans-serif']=['SimHei']
fig=plt.figure(figsize=(2,2),dpi=400)
ax=fig.add_axes([0,0,1,1],projection=ccrs.PlateCarree(central_longitude=110))
ax.add_feature(cf.LAND.with_scale('110m'))
ax.add_feature(cf.OCEAN.with_scale('110m'))
ax.add_feature(cf.COASTLINE.with_scale('110m'),lw=0.4)
ax.add_feature(cf.RIVERS.with_scale('110m'),lw=0.4)
################################################################
ax.set_xticks([-180,-150,-120,-90,-60,-30,0,30,60,90,120,150,180])#指定要显示的经纬度                      
ax.set_yticks([-90,-60,-30,0,30,60,90])                  
ax.xaxis.set_major_formatter(LongitudeFormatter())#刻度格式转换为经纬度样式                       
ax.yaxis.set_major_formatter(LatitudeFormatter())                        
ax.tick_params(axis='both',which='major',labelsize=3,direction='out',length=5,width=0.3,pad=0.2,top=True,right=True)
ax.minorticks_on()
ax.tick_params(axis='both',which='minor',direction='out',width=0.3,top=True,right=True)
ax.spines['geo'].set_linewidth(0.5)#调节边框粗细
ax.set_title('Python仿制NCL风格地图',fontsize=5)
代码语言:javascript复制
import matplotlib.pyplot as plt                                          
import cartopy.crs as ccrs                                               
import cartopy.feature as cf                                             
from cartopy.mpl.ticker import LongitudeFormatter,LatitudeFormatter
import matplotlib.ticker as mticker
plt.rcParams['font.sans-serif']=['SimHei']
fig=plt.figure(figsize=(2,2),dpi=400)
ax=fig.add_axes([0,0,1,1],projection=ccrs.PlateCarree(central_longitude=110))
ax.add_feature(cf.LAND.with_scale('110m'))
ax.add_feature(cf.OCEAN.with_scale('110m'))
ax.add_feature(cf.COASTLINE.with_scale('110m'),lw=0.4)
ax.add_feature(cf.RIVERS.with_scale('110m'),lw=0.4)
################################################################
ax.set_xticks([-180,-150,-120,-90,-60,-30,0,30,60,90,120,150,180])#指定要显示的经纬度                      
ax.set_yticks([-90,-60,-30,0,30,60,90])                  
ax.xaxis.set_major_formatter(LongitudeFormatter())#刻度格式转换为经纬度样式                       
ax.yaxis.set_major_formatter(LatitudeFormatter())                        
ax.tick_params(axis='both',which='major',labelsize=3,direction='out',length=5,width=0.3,pad=0.2,top=True,right=True)
ax.xaxis.set_minor_locator(mticker.MultipleLocator(5))#刻度格式转换为经纬度样式                       
ax.yaxis.set_minor_locator(mticker.MultipleLocator(5))  
ax.tick_params(axis='both',which='minor',direction='out',width=0.3,top=True,right=True)
ax.spines['geo'].set_linewidth(0.5)#调节边框粗细
ax.set_title('Python仿制NCL风格地图',fontsize=5)

二、兰勃脱下经纬度的添加

最开始非常不容易在矩形以外的投影下添加经纬度,要添加就只能自己造轮子。但是随着版本更新,已经支持在兰勃脱下添加标签了。有一定的缺憾,比如经纬度标签不能强制对齐。

代码语言:javascript复制
import matplotlib.pyplot as plt 
import numpy as np
import cartopy.crs as ccrs                                               
import cartopy.feature as cf
import matplotlib.ticker as mticker
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
plt.rcParams['font.sans-serif']=['SimHei']
fig=plt.figure(figsize=(2,2),dpi=400)
ax=fig.add_axes([0,0,1,1],projection=ccrs.LambertConformal(central_longitude=110))
ax.add_feature(cf.LAND.with_scale('110m'))
ax.add_feature(cf.OCEAN.with_scale('110m'))
ax.add_feature(cf.COASTLINE.with_scale('110m'),lw=0.4)
ax.add_feature(cf.RIVERS.with_scale('110m'),lw=0.4)
################################################################
gl=ax.gridlines(draw_labels=True,linestyle=":",linewidth=0.3 ,x_inline=False, y_inline=False,color='k')
gl.top_labels=False #关闭上部经纬标签                                  
gl.right_labels=False
gl.xformatter = LONGITUDE_FORMATTER  #使横坐标转化为经纬度格式            
gl.yformatter = LATITUDE_FORMATTER                                        
gl.xlocator=mticker.FixedLocator(np.arange(80,140,10))                                   
gl.ylocator=mticker.FixedLocator(np.arange(10,60,10)) 
gl.xlabel_style={'size':4}#修改经纬度字体大小                             
gl.ylabel_style={'size':4}
ax.spines['geo'].set_linewidth(0.5)#调节边框粗细
ax.set_extent([80,140,10,60],crs=ccrs.PlateCarree())
ax.set_title('LambertConformal地图',fontsize=5)

三、墨卡托下经纬度的添加

在官网上提供了墨卡托下添加经纬度标签的样例。

代码语言:javascript复制
import matplotlib.pyplot as plt
import cartopy.feature as cf
import matplotlib.ticker as mticker
import cartopy.crs as ccrs
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter,LatitudeLocator
fig=plt.figure(figsize=(2,2),dpi=400)
ax =fig.add_axes([0,0,1,1],projection=ccrs.Mercator())
ax.add_feature(cf.LAND.with_scale('110m'))
ax.add_feature(cf.OCEAN.with_scale('110m'))
ax.add_feature(cf.COASTLINE.with_scale('110m'),lw=0.4)
ax.add_feature(cf.RIVERS.with_scale('110m'),lw=0.4)
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True,
                  linewidth=0.3,color='k',alpha=0.5,linestyle='--')
gl.top_labels = False
gl.right_labels = False
gl.xlocator = mticker.FixedLocator([-150,-120,-90,-60,-30,0,30,60,90,120,150,180])
gl.ylocator = LatitudeLocator()
gl.xformatter = LongitudeFormatter()
gl.yformatter = LatitudeFormatter()
gl.xlabel_style = {'size': 3}
gl.ylabel_style = {'size': 4}

cartopy的维护人员还要更新,目前看来还会支持更多投影下的经纬度添加。

0 人点赞