1 Star 0 Fork 0

fungis / python-cartopy-test

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Cartopy绘图入门指南

一、虚拟环境安装

(1)开发准备

Miniconda是一款小巧的python环境管理工具,安装包大约60M,其安装程序中包含conda软件包管理器和Python。一旦安装了Miniconda,就可以使用conda命令安装任何其他软件工具包并创建环境等(注意:需要配置环境变量)。 通过配置配置第三方镜像,可以解决conda下载速度慢的问题。

下载地址 https://docs.anaconda.com/free/miniconda/

  1. 通过命令配置,在Anaconda Promot中依次输入以下两条命令:
# 清华大学镜像
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/

conda config --set show_channel_urls yes   # 显示包的下载来源
  1. 通过文件的形式配置

除了使用conda的config命名,还可以通过直接修改配置文件.condarc的方式来完成下载源的修改。

Linux系统中.condarc文件的位置在/home/用户名/下。Windows系统中.condarc文件的位置在C:\users\用户名下。找到.condarc文件并将其内容修改为如下所示即可。

show_channel_urls: true
envs_dirs:
  - D:\ProgramData\Miniconda3\envs
channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - defaults
  • 查看是否修改成功

上面的两种修改方式均可将conda的下载源修改为国内镜像,可以通过如下命令查看一下是否修改成功。 ( 这里channels的显示结果是按照优先级进行排序的,最后添加的镜像源优先级会最高。 )

conda config --show channels

(2)环境准备

创建python3.9虚拟环境并使用,需要在Anaconda Promot中输入命令

创建conda环境 ,创建名为cartopy-test且python版本为3.9的虚拟环境

conda create -n cartopy-test python=3.9 -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/  -y

激活环境,进入名为cartopy-test的虚拟环境

conda activate cartopy-test

安装核心依赖库

# 安装Cartopy库
conda install cartopy -y
# 安装pandas库
conda install pandas -y
conda install xlrd -y
# 安装netCDF4库
conda install netCDF4 -y
# 安装PyKrige库
conda install PyKrige
# 安装scipy库
conda install scipy -y
# 安装geopandas
conda install geopandas=0.10.0 pygeos pyogrio -c conda-forge -y
# 安装jupyter notebook
conda install jupyter notebook
# 安装环境自动关联包
conda install nb_conda
# 安装代码补全包
conda install pyreadline
# 启动
jupyter notebook
# 删除该虚拟镜像
# conda remove -n cartopy-test --all

其他依赖库

conda install xarray -y # 读取气象数据 .nc文件
pykrige # 调用kriging插值算法
frykit # 大神写的依赖cartopy包画中国的包
openpyxl
cnmaps
basemap # 需要离线下载whl进行安装 目前已经不再更新 可以不安装

依赖库截图 image-20220728102055441

(3)conda常用命令

conda常用中一些常用命令总结如下。

# 查看conda版本,验证是否安装
conda --version
# 查看所有信息
conda info
# 查看所有已经创建的环境
conda info -e
# 创建环境
conda create -n test
# 激活环境
conda activate test
# 退出环境
conda deactivate
#  删除环境env_name
conda remove --name env_name --all 或者 conda env remove --name env_name
# 查看所有已经安装的包
conda list 
# 移除某个镜像源
conda config --remove-key channels
# 修改安装源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
# 查看安装源
conda config --show-sources

# 根据yml文件来创建conda环境
conda env create -n <your_env_name> -f environment.yml
# 导出conda环境为yml文件
conda env export >  xxx.yml
# 导出conda环境/python环境为txt文件
conda list -e > requirements.txt

二、实践绘图

(1)入门教程

首先需要导入一个cartopy.feature 常量,为了简化一些非常常见的情况,如大陆海洋国界等cartopy都已经进行了预定义,但需要注意的是直接导入的中国国界线并不是标准的,在使用时需要注意。

这些信息带有三种分辨率,分别为110m,50m和10m。

image-20220728104252552

  • 实现代码
# 引入库包
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt

# 创建画布,画布参数设置
fig = plt.figure(figsize=(4, 4), dpi=200)
# 全球图像的中央位于太平洋的 180 度经线处,默认投影
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=120))
# 使用墨卡托投影
# ax = plt.axes(projection=ccrs.Mercator(min_latitude=-80.0, max_latitude=84.0))

ax.add_feature(cfeature.LAND)  # 添加陆地
ax.add_feature(cfeature.COASTLINE, lw=0.3)  # 添加海岸线
ax.add_feature(cfeature.RIVERS, lw=0.25)  # 添加河流
# ax.add_feature(cfeat.RIVERS.with_scale('50m'),lw = 0.25)  # 加载分辨率为50的河流
ax.add_feature(cfeature.LAKES, color='r')  # 指定湖泊颜色为红色
ax.add_feature(cfeature.BORDERS, linestyle='-', lw=0.25)  # 不推荐,因为该默认参数会使得我国部分领土丢失
ax.add_feature(cfeature.OCEAN)  # 添加海洋

ax.coastlines(lw=0.3)
plt.savefig("./results/1.png")  # 生成图像保存本地
plt.show()

(2)相关教程

MIT License Copyright (c) 2023 fungis Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

cartopy学习案例分享 展开 收起
Python
MIT
取消

发行版 (1)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
Python
1
https://gitee.com/fungiser/python-cartopy-test.git
git@gitee.com:fungiser/python-cartopy-test.git
fungiser
python-cartopy-test
python-cartopy-test
master

搜索帮助