# python-cartopy-test **Repository Path**: fungiser/python-cartopy-test ## Basic Information - **Project Name**: python-cartopy-test - **Description**: cartopy学习案例分享 - **Primary Language**: Python - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-07-13 - **Last Updated**: 2024-05-03 ## Categories & Tags **Categories**: Uncategorized **Tags**: Python ## README # Cartopy绘图入门指南 ## 一、虚拟环境安装 ### (1)开发准备 `Miniconda`是一款小巧的python环境管理工具,安装包大约60M,其安装程序中包含conda软件包管理器和Python。一旦安装了Miniconda,就可以使用conda命令安装任何其他软件工具包并创建环境等(注意:需要配置环境变量)。 通过配置配置第三方镜像,可以解决conda下载速度慢的问题。 > 下载地址 https://docs.anaconda.com/free/miniconda/ 1. 通过命令配置,在Anaconda Promot中依次输入以下两条命令: ```bash # 清华大学镜像 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 # 显示包的下载来源 ``` 2. 通过文件的形式配置 除了使用conda的config命名,还可以通过直接修改配置文件.condarc的方式来完成下载源的修改。 Linux系统中.condarc文件的位置在/home/用户名/下。Windows系统中.condarc文件的位置在C:\users\用户名下。找到.condarc文件并将其内容修改为如下所示即可。 ```yaml 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的显示结果是按照优先级进行排序的,最后添加的镜像源优先级会最高。 ) ```python conda config --show channels ``` ### (2)环境准备 创建python3.9虚拟环境并使用,需要在Anaconda Promot中输入命令 创建conda环境 ,创建名为cartopy-test且python版本为3.9的虚拟环境 ```bash conda create -n cartopy-test python=3.9 -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ -y ``` 激活环境,进入名为cartopy-test的虚拟环境 ```bash conda activate cartopy-test ``` 安装核心依赖库 ```bash # 安装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 ``` 其他依赖库 ```bash conda install xarray -y # 读取气象数据 .nc文件 pykrige # 调用kriging插值算法 frykit # 大神写的依赖cartopy包画中国的包 openpyxl cnmaps basemap # 需要离线下载whl进行安装 目前已经不再更新 可以不安装 ``` 依赖库截图 ![image-20220728102055441](README.assets/image-20220728102055441.png) ### (3)conda常用命令 conda常用中一些常用命令总结如下。 ```bash # 查看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 -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](README.assets/image-20220728104252552.png) - 实现代码 ```python # 引入库包 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)相关教程 - [Cartopy安装](https://mp.weixin.qq.com/s/GW6odDBGLPVRZTKx0YZLVg) - [这份Cartopy绘图入门指南,请查收!](https://mp.weixin.qq.com/s/ZByHsAeu80_Pe6T5P3rEzw) - [The cartopy Feature interface !](https://scitools.org.uk/cartopy/docs/v0.14/matplotlib/feature_interface.html)