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

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


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



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

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

(1)gridlines方式

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

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的方式稍微复杂,但是能对刻度间隔进行灵活修改。

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

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

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

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)

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

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

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

本文分享自微信公众号 - DataCharm(shujumeili)

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间: 2020-11-18

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

版权声明
本文为[DataCharm]所创,转载请带上原文链接,感谢
https://cloud.tencent.com/developer/article/1790266

  1. Python 3 entry, see this is enough
  2. 华为大佬打造的400集Python视频学起来,学完万物皆可爬
  3. 400 episodes of Python video created by Huawei boss
  4. django之csrf_exempt解决跨域请求的问题
  5. CSRF of Django_ Exempt solves the problem of cross domain requests
  6. 1.7 万 Star!一个简单实用的 Python 进度条库
  7. 17000 stars! A simple and practical Python progress bar library
  8. Python爬虫:设置Cookie解决网站拦截并爬取蚂蚁短租
  9. Python crawler: setting cookie to solve website interception and crawling ant short rent
  10. Python-Net编程
  11. Python net programming
  12. 学习Python数学英语基础重要吗?Python教程!
  13. Is it important to learn the basics of math and English in Python!
  14. Python数据分析常用库有哪些?Python学习!
  15. What are the common libraries for Python data analysis? Learn Python!
  16. win 创建python虚拟环境
  17. Creating Python virtual environment with win
  18. In order to automatically collect B station barrage, I developed a tool in Python
  19. 用Python编程语言来实现阿姆斯特朗数的检查
  20. Using python programming language to check Armstrong number
  21. Python中的解决中文字符编码的问题
  22. Solving the problem of Chinese character coding in Python
  23. Translation: practical Python Programming 02_ 01_ Datatypes
  24. Installation and use of Python and tensorflow in win10 environment (Python version 3.6, tensorflow version 1.6)
  25. Python series 46
  26. Linux安装Python3
  27. 【python接口自动化】- 正则用例参数化
  28. Python RestFul Api 设计
  29. filecmp --- 文件及目录的比较│Python标准库
  30. Installing python3 on Linux
  31. [Python] Matplotlib 圖表的繪製和美化技巧
  32. (資料科學學習手札108)Python+Dash快速web應用開發——靜態部件篇(上)
  33. 翻譯:《實用的Python程式設計》02_01_Datatypes
  34. 【python接口自动化】- 正则用例参数化
  35. 翻译:《实用的Python编程》02_02_Containers
  36. 两年Java,去字节跳动写Python和Go
  37. [Python interface automation] - regular use case parameterization
  38. Python restful API design
  39. 翻译:《实用的Python编程》02_02_Containers
  40. 两年Java,去字节跳动写Python和Go
  41. 翻译:《实用的Python编程》02_02_Containers
  42. Python基于粒子群优化的投资组合优化研究
  43. ubuntu部署django项目
  44. 兩年Java,去位元組跳動寫Python和Go
  45. 翻譯:《實用的Python程式設計》02_02_Containers
  46. 这样学习Python,爷爷都学会了!超简单Python入门
  47. [Python] 基于 jieba 的中文分词总结
  48. 【python】递归听了N次也没印象,读完这篇你就懂了
  49. [Python] 基于 jieba 的中文分词总结
  50. 人理解迭代,神则体会递归,从电影艺术到Python代码实现神的逆向思维模式
  51. [Python] 基於 jieba 的中文分詞總結
  52. Python属于后端开发还是前端开发?Python入门!
  53. 【python】递归听了N次也没印象,读完这篇你就懂了
  54. 一天快速入门python
  55. 学习Python对年龄有没有要求?30岁可以吗?
  56. 清华教授!12小时整理的最全Python教程(文末无偿分享)
  57. Filecmp -- comparison of files and directories
  58. Drawing and beautifying skills of [Python] Matplotlib chart
  59. Python + dash rapid web application development static components
  60. Translation: practical Python Programming 02_ 01_ Datatypes