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

DataCharm 2021-02-22 13:02:28
Python 经纬度 空间 绘图 cartopy

（1）gridlines方式

```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)
################################################################
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的坐标修改方式

```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.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.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)```
```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.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.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)```

```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)
################################################################
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)```

```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)
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的维护人员还要更新，目前看来还会支持更多投影下的经纬度添加。

https://cloud.tencent.com/developer/article/1790266