【Cartopy】1.库的安装和使用
安装cartopy库
首先上官方文档地址:cartopy
在线conda 安装:
conda install cartopy
(安装成功就不需要再看离线安装方法)离线安装: http://www.lfd.uci.edu/~gohlke/pythonlibs/#cartopy
注意:需要一些必须的软件包Shapely、pyshp,Cartopy所依赖的这两个库也都从上面的网址下载。
测试
import matplotlib.pyplot as plt
import cartopy.crs as ccrsplt.figure(figsize=(6, 3))
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180))
ax.coastlines(resolution='110m')
ax.gridlines()
plt.show()
测试失败 error/ warning: 没有地图文件。这里是因为cartopy运行程序默认从Natural Earth下载地图文件。可以优先下载地图文件放在本地文件夹下。
下载Natural Earth地图数据
- 下载10m、50m和110m分辨率的cultural 和 physical类型数据。
- 解压到本地路径:C:\Users\Administrator.local\share\cartopy\shapefiles\natural_earth\physical or C:\Users\Administrator.local\share\cartopy\shapefiles\natural_earth\cultural 中。
注:如果找不到,搜索 C:\Users\xxx.local\share\cartopy\shapefiles\natural_earth\physical
解压完成后,不能重新运行测试代码,要对代码进行修改
绘制地图测试代码
demo: 其中scale参数用于调整使用哪种分辨率的地图,全球地图建议用1:110的,小尺度地图可以用1:50的或1:10的。
1 Global map
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatterscale = '110m' # 补充1
fig = plt.figure(figsize=(8, 10))
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180))
ax.set_global()
# 补充部分
###### start #####
land = cfeature.NaturalEarthFeature('physical', 'land', scale, edgecolor='face',facecolor=cfeature.COLORS['land'])
ax.add_feature(land, facecolor='0.75')
ax.coastlines(scale)
###### end ######
# 标注坐标轴
ax.set_xticks([0, 60, 120, 180, 240, 300, 360], crs=ccrs.PlateCarree())
ax.set_yticks([-90, -60, -30, 0, 30, 60, 90], crs=ccrs.PlateCarree())
# zero_direction_label用来设置经度的0度加不加E和W
lon_formatter = LongitudeFormatter(zero_direction_label=False)
lat_formatter = LatitudeFormatter()
ax.xaxis.set_major_formatter(lon_formatter)
ax.yaxis.set_major_formatter(lat_formatter)
# 添加网格线
# gl = ax.gridlines()
ax.grid()
plt.show()
2. reginal map
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatterbox = [100, 150, 0, 50]
scale = '50m' # 补充1 此处50m不知道为什么还是不能加载
xstep, ystep = 10, 10
fig = plt.figure(figsize=(8, 10))
ax = plt.axes(projection=ccrs.PlateCarree())
# set_extent需要配置相应的crs,否则出来的地图范围不准确
ax.set_extent(box, crs=ccrs.PlateCarree())
# 补充部分
###### start #####
land = cfeature.NaturalEarthFeature('physical', 'land', scale, edgecolor='face',facecolor=cfeature.COLORS['land'])
ax.add_feature(land, facecolor='0.75')
ax.coastlines(scale)
###### end ######
# ===================================================
# 图像地址D:\Program\Anaconda\pkgs\cartopy-0.17.0-py37h5ae9855_1\Lib\site-packages\cartopy\data\raster\50-natural-earth-1-downsampled.png
# 如果有其它高精度图像文件,改名替换即可
ax.stock_img()
# ===================================================
#标注坐标轴
ax.set_xticks(np.arange(box[0], box[1]+xstep,xstep), crs=ccrs.PlateCarree())
ax.set_yticks(np.arange(box[2], box[3]+ystep,ystep), crs=ccrs.PlateCarree())
# zero_direction_label 用来设置经度的0度加不加E和W
lon_formatter = LongitudeFormatter(zero_direction_label=False)
lat_formatter = LatitudeFormatter()
ax.xaxis.set_major_formatter(lon_formatter)
ax.yaxis.set_major_formatter(lat_formatter)
# 添加网格线
ax.grid()
plt.show()
注:仔细观察可以发现上图scale依然是110m的分辨率,因为50m不能运行,经过测试需要把Natural Earth下载的地图文件修改名称,ne_50m_land.shp修改为50m_land.shp(去掉ne_)
因为懒,所以选择用程序修改文件名,在本文末,附上批量化修改文件名的代码
3. Polar projection map
import matplotlib.path as mpath
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker as mticker
import cartopy.crs as ccrs
import cartopy.feature as cfeaturefig = plt.figure(figsize=(6, 6))
ax = plt.axes(projection=ccrs.NorthPolarStereo())
box = [-180, 180, 55, 90]
xstep, ystep = 30, 15
# Limit the map to -60 degrees latitude and below.
ax.set_extent(box, crs=ccrs.PlateCarree())
# ===================================================
scale = '50m' # 补充1
land = cfeature.NaturalEarthFeature('physical', 'land', scale, edgecolor='face',facecolor=cfeature.COLORS['land'])
ocean = cfeature.NaturalEarthFeature('physical', 'ocean', scale, edgecolor='face',facecolor=cfeature.COLORS['water'])
ax.add_feature(land, facecolor='0.75')
ax.add_feature(ocean, facecolor='blue')
ax.coastlines(scale, linewidth=0.9)
# ===================================================
# 标注坐标轴
line = ax.gridlines(draw_labels=False)line.ylocator = mticker.FixedLocator(np.arange(40, 90, 20)) # 手动设置x轴刻度
line.xlocator = mticker.FixedLocator(np.arange(-180, 210, 30)) # 手动设置x轴刻度
# Compute a circle in axes coordinates, which we can use as a boundary
# for the map. We can pan/zoom as much as we like - the boundary will be
# permanently circular.
theta = np.linspace(0, 2 * np.pi, 100)
center, radius = [0.5, 0.5], 0.5
verts = np.vstack([np.sin(theta), np.cos(theta)]).T
circle = mpath.Path(verts * radius + center)
ax.set_boundary(circle, transform=ax.transAxes)# 创建要标注的labels字符串
ticks = np.arange(0, 210, 30)
etick = ['0'] + ['%d$^\circ$E' % tick for tick in ticks if (tick != 0) & (tick != 180)] + ['180']
wtick = ['%d$^\circ$W' % tick for tick in ticks if (tick != 0) & (tick != 180)]
labels = etick + wtick
# 创建与labels对应的经纬度标注位置
# xticks=[i for i in np.arange(0,210,30)]+[i for i in np.arange(-32,-180,-30)]
xticks = [-0.8, 28, 58, 89.1, 120, 151, 182.9, -36, -63, -89, -114, -140]
yticks = [53] + [53] + [54] + [55] * 2 + [54.5] + [54] + [50] + [49] * 3 + [50.6]# 标注经纬度
# ax.text(0.01,0.23,'60$^\circ$W',transform=ax.transAxes,rotation=25)
# ax.text(-63,50,'60$^\circ$W',transform=ccrs.Geodetic(),rotation=25)
for xtick, ytick, label in zip(xticks, yticks, labels):ax.text(xtick, ytick, label, transform=ccrs.Geodetic())
x = [180, 180, 0, 0]
y = [50, 90, 90, 50]
ax.plot([-180, 0], [80, 80], ':', transform=ccrs.Geodetic(), color='k', linewidth=0.4)
ax.plot([-90, 90], [80, 80], ':', transform=ccrs.Geodetic(), color='k', linewidth=0.5)
# ax.plot([90,0],[50,50],'-.',transform=ccrs.Geodetic(),color='r', linewidth=6)ax.text(11.9333, 78.9166, r'\bigstar', transform=ccrs.Geodetic(), size=15, color='r')
plt.show()
批量修改文件名
import os
path = r'E:\temp'
fileLists = os.listdir(path)
for f in fileLists:if f[:3] == 'ne_'oldname = path + os.sep + fnewname = path + os.sep + f[3:]os.rename(oldname, newname)
【Cartopy】1.库的安装和使用相关推荐
- IIS+PHP+MySQL+Zend Optimizer+GD库+phpMyAdmin安装配置[完整修正实用版]
IIS+PHP+MySQL+Zend Optimizer+GD库+phpMyAdmin安装配置[完整修正实用版] IIS+PHP+MySQL+Zend Optimizer+GD库+phpMyAdmin ...
- Python+Anaconda中库的安装
查看Anaconda中的Python的开发环境 (1)conda env list 不同环境中库的安装 方式一: Pycharm+Anacond安装完成后的Python文件创建以及No module ...
- libevent库的安装与使用
一.libevent库的安装 Libevent 使用源码安装的方式,源码下载地址:http://libevent.org/ 下载下来后,将 Libevent 的压缩包拷贝到 Linux 系统中,然后按 ...
- python3.7如何安装库_Python3.7版库的安装以及常用方法(十分简单)
首先你的python里面必须要有pip,有的话你就可以直接在cmd窗口的python所在主目录下输入pip install "你要安装的库". 下面以 matplotlib为例. ...
- 六丶青龙的依赖库+拉库+互助安装及使用教程
六丶青龙的依赖库+互助安装及使用教程 没有服务器的先自行购买,这里推荐腾讯云2H4G8M首年70–点击购买 青龙面板安装教程 QQ交流:1014549449 --------------点击跳转 青龙 ...
- python安装库-python 安装库
广告关闭 2017年12月,云+社区对外发布,从最开始的技术博客到现在拥有多个社区产品.未来,我们一起乘风破浪,创造无限可能. 安装插件使用命令 pip install opencv-python下载 ...
- python第三方库下载-3、python第三方库的安装方式
前言:在学习Python过程中,经常要用到很多第三方库,面对各种不同情况,Python为我们提供了多种安装方法,这里主要介绍三种方法:pycharm在线安装.pip在线安装(强烈推荐).离线安装. 方 ...
- 下面不属于python第三方库的安装方法的是-python第三方库的pip安装方法
安装python第三方库的三种方法 方法1:使用pip命令 方法2:集成安装方法 方法3:文件安装方法 一.pip命令安装方法(需要联网): pip安装方法简单讲就是使用python自带的pip安装工 ...
- 下面不属于python第三方库的安装方法的是-Python第三方库安装和卸载
系统:Windows 7 版本:Python 3.5 Python是一门简洁.优雅的语言,丰富的第三方库能让我们很多的编程任务变得更加简单.对于想要用Python进行数据分析,就需要强大的Python ...
- python基础代码库-python3.4第三方库的安装?python基础代码库
python怎么安装下载模块 python安装下载模块的:1.按盘上的[win r]快,打开运行窗口:2.输入cmd,点击定]:3.在打开的命令提示符中执行[pip install 模块名]命令即可. ...
最新文章
- 京东金融将发布重量级技术与数据产品 招募合作伙伴共拓蓝海市场
- SAP MM公司间STO里的交货单自动创建?
- (0003) iOS 开发之App 适配iOS 10
- QT教程4:基本窗口实现
- 网络投票轻松实现自动化 - 验证码识别的基本思路及方法
- Ubuntu桌面版网络设置
- PHP array_filter()函数与示例
- python字符串相加_谁说python字符串相加效率低
- Android:Android学习路线图
- Sci-Hub又又又被起诉了!这个论文免费下载网站也太难了...
- ubuntu软件包管理
- SAP固定资产业务场景及方案
- java word另存为_java实现页面另存为word
- js数组按中文拼音排序_这才是选择排序正确的打开方式!
- gitlab 503 错误的解决方案
- 李笑来《自学是门手艺》
- 智能优化算法:侏儒猫鼬优化算法-附代码
- 伪NMOS的基本特点
- PHP复习_封装万能的PDO类
- 4.3 协方差及相关系数、矩