之前的资料都有些过时,反正对我是不起作用。
请参考官方 matplotlib/basemap 的Github仓库的Readme:GitHub - matplotlib/basemap: Plot on map projections (with coastlines and political boundaries) using matplotlib
以下是翻译:
Basemap
在地图投影上绘制(带有海岸线和政治边界)使用matplotlib。
要求
基本要求如下:
-
Python 2.6(或更高版本)
可选要求包括:(如果你用到了下列method,请安装以下包)
-
OWSLib。它需要用于方法
Basemap.wmsimage
。 -
Pillow。它需要用于方法
Basemap.bluemarble
,Basemap.etopo
,Basemap.shadedrelief
和Basemap.warpimage
。
安装
basemap-data
和 basemap-data-hires
包在 PyPI 中可用,并且可以使用 pip 安装:
pip install basemap-data
pip install basemap-data-hires
为 Windows 和 GNU/Linux 预编译的 basemap
二进制轮子 (体系结构 x86 和 x64,Python 2.7 和 3.5+) 以及 MacOS (体系结构 x64 和 arm64,Python 3.9+) 也可在 PyPI 中找到:
pip install basemap
否则,您将需要从 GitHub 所托管的源代码安装 basemap
如下所示:
-
安装先决条件 Python 模块:
-
下载
basemap
源代码并移动到packages/basemap
文件夹:git clone --depth 1 https://github.com/matplotlib/basemap.git cd basemap/packages/basemap
-
构建 GEOS 库。您可以使用
utils
中提供的帮助程序 (请注意,您需要提前安装 CMake 和工作的 C 编译器):export GEOS_DIR=<your desired location> python -c "import utils; utils.GeosLibrary('3.6.5').build(installdir='${GEOS_DIR}')"
或者,如果已经安装,则可以直接链接到系统库。
GEOS_DIR
必须指向 GEOS 安装前缀; 例如,如果libgeos_c.so
位于/usr/lib
,geos_c.h
位于/usr/include
, 则必须将GEOS_DIR
设置为/usr
。 -
构建并安装
basemap
二进制轮子:pip install .
在 Linux 上,如果您的 Python 是通过包管理系统安装的, 请确保您具有构建 Cython 扩展所需的 Python 头文件
Python.h
(例如,在类似于 Debian 的系统上,您应该安装python-dev
软件包)。 -
通过执行以下命令检查包是否正确安装:
python -c "from mpl_toolkits.basemap import Basemap"
文档
请参阅 https://matplotlib.org/basemap/
在 examples
目录中查看脚本以获取示例用法。
阅读 FAQ 和/或向 matplotlib-users 邮件列表发送电子邮件,如果您遇到问题或有疑问。
联系
Ben Root ben.v.root@gmail.com
Víctor Molina García (@molinav)
感谢
特别感谢 John Hunter、Andrew Straw、Eric Firing、Rob Hetland、Scott Sinclair、Ivan Lima、Erik Andersen、Michael Hearne、Jesper Larsen、Ryan May、David Huard、Mauro Cavalcanti、Jonas Bluethgen、Chris Murphy、Pierre Gerard-Marchant、Christoph Gohlke、Eric Bruning、Stephane Raynaud、Tom Loredo、Patrick Marsh、Phil Elson 以及 Henry Hammond对宝贵的贡献。
已知问题
Basemap.fillcontinents
方法并不总是表现正确。 Matplotlib 总是尝试填充多边形的内部。在某些情况下,海岸线多边形的内部可能是 模糊的,可能会填充外部而不是内部。解决方法是稍微更改地图投影区域或者 使用 Basemap.drawlsmask
方法屏蔽陆地区域而不是填充海岸线 多边形(在 ortho_demo.py
示例中有说明)。