GMT 制图入门 — 一键部署 + 零基础教程
声明:本 skill 中的软件安装方法、绘图语法、示例数据均引用自 GMT 中文社区(https://docs.gmt-china.org/latest/)。本 skill 仅将这些知识封装为 Hermes Agent 可调用的自动化流程。
触发条件
用户提到:GMT、制图、地图绘制、Generic Mapping Tools、地形图、DEM、投影、GMT教程、安装GMT
第零步:自动检测系统并安装 GMT(Agent 必须首先执行)
加载本 skill 后,Agent 必须:
0.1 检测操作系统
# Agent 执行此命令判断 OS
uname -s
# Darwin → macOS
# Linux → Linux
# MINGW* / MSYS* / CYGWIN* → Windows (Git Bash)
0.2 检测是否已安装 GMT
gmt --version 2>/dev/null || echo "NOT_INSTALLED"
如果已安装,跳过安装步骤。
0.3 一键安装(按 OS 自动选择方案)
▸ Linux 安装
# 方案 A:Conda(推荐,跨平台统一)
# 1. 安装 Miniconda(如果没有)
if ! command -v conda &>/dev/null; then
curl -sL "https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh" -o /tmp/miniconda.sh
bash /tmp/miniconda.sh -b -p ~/miniconda3
export PATH="$HOME/miniconda3/bin:$PATH"
fi
# 2. 配置 conda-forge
conda config --add channels conda-forge 2>/dev/null
conda config --set channel_priority strict 2>/dev/null
# 3. 接受 TOS(conda 26+)
conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/main 2>/dev/null
conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/r 2>/dev/null
# 4. 安装 GMT 到独立环境
conda config --set solver classic 2>/dev/null
CONDA_NO_PLUGINS=true conda create -y -n gmt -c conda-forge gmt
conda config --set solver libmamba 2>/dev/null
# 5. 持久化 PATH
SHELL_RC="$HOME/.bashrc"
grep -q "conda3/envs/gmt/bin" "$SHELL_RC" 2>/dev/null || \
echo 'export PATH="$HOME/miniconda3/envs/gmt/bin:$PATH"' >> "$SHELL_RC"
# 6. 验证
export PATH="$HOME/miniconda3/envs/gmt/bin:$PATH"
gmt --version
# 方案 B:系统包管理器(备选)
# sudo apt install gmt gmt-dcw gmt-gshhg -y # Ubuntu/Debian
▸ macOS 安装
# 方案 A:Homebrew(推荐)
if ! command -v brew &>/dev/null; then
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
brew install gmt
# 方案 B:Conda(同 Linux 方案,跨平台一致)
▸ Windows 安装
# 推荐方案:Conda(与 Linux/macOS 统一环境,避免路径问题)
# 前提:需要安装 Git Bash 或 Miniconda Prompt
# 1. 安装 Miniconda for Windows(如果没有)
# 手动下载:https://docs.conda.io/en/latest/miniconda.html
# 或用 curl(在 Git Bash 中):
if ! command -v conda &>/dev/null; then
curl -sL "https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe" -o /tmp/miniconda.exe
start /wait "" /tmp/miniconda.exe /InstallationType=JustMe /RegisterPython=0 /S /D=%UserProfile%\miniconda3
export PATH="$HOME/miniconda3/bin:$HOME/miniconda3/Scripts:$PATH"
fi
# 2. 安装 GMT(同 Linux 流程)
conda config --add channels conda-forge
conda create -y -n gmt -c conda-forge gmt
# 3. 持久化 PATH
# 在 Git Bash 的 ~/.bashrc 中添加:
echo 'export PATH="$HOME/miniconda3/envs/gmt/bin:$PATH"' >> ~/.bashrc
# 4. 验证
export PATH="$HOME/miniconda3/envs/gmt/bin:$PATH"
gmt --version
Windows 特别说明:如果用户没有 Git Bash,Agent 应引导用户先安装 Git for Windows(https://git-scm.com/download/win),然后用 Git Bash 执行上述命令。Conda 方案保证与 Linux/macOS 完全一致的 GMT 6.x 环境。
0.4 安装后验证
gmt begin first_map pdf
gmt basemap -R0/10/0/10 -JX10c -Baf -B+t"My First GMT Map"
gmt end
# 检查是否生成 first_map.pdf
ls -lh first_map.pdf
第一步:GMT 核心概念
1.1 现代模式(Modern Mode)
gmt begin 输出文件名 格式 # 如 map pdf
# 所有绘图命令...
gmt end
1.2 三个核心参数:-R -J -B
| 参数 | 含义 | 示例 |
|------|------|------|
| -R | 绘图区域 | -R114/123/34/38 (lon1/lon2/lat1/lat2) |
| -J | 投影方式 | -JM15c (Mercator, 宽15cm) |
| -B | 边框标注 | -B2 -BWSen |
每个 GMT 绘图命令都必须包含 -R 和 -J。
1.3 图层模型
第 6 层 gmt text 文字标注 ← 最上面
第 5 层 gmt plot 散点符号
第 4 层 gmt legend 图例
第 3 层 gmt coast 海岸线
第 2 层 gmt grdimage DEM 底图
第 1 层 gmt basemap 底图框架 ← 最下面
第二步:常用投影速查
| 投影 | -J 写法 | 场景 |
|------|-----------|------|
| Mercator | -JM15c | 区域地图 |
| Lambert | -JL120/36/33/42/15c | 中纬度 |
| Hammer | -JH120/15c | 全球概览 |
| Orthographic | -JG120/36/15c | 地球视角 |
| 线性 | -JX10c/8c | XY散点图 |
第三步:颜色选用(CPT)
| CPT | 效果 |
|-----|------|
| -Cterra | ★ 地形首选 |
| -Cglobe | 卫星图风格 |
| -Crainbow | 彩虹色 |
| -Chot | 热力色 |
| -Cpolar | 两极色 |
| -Cgray | 灰度 |
地形加光照:-I+d(自动光照)
第四步:要素绘制
4.1 海岸线
gmt coast -W0.3p,gray30 -Slightblue -Df
# 关键坑:DEM 上图叠加 coast 不要用 -G,会盖住地形
4.2 散点符号
gmt plot data.txt -Sc0.3c -Gred -W0.5p,black
# -Sc=圆 -Ss=方 -St=三角 -Sd=菱 -Sa=星
4.3 文字标注
gmt text labels.txt -F+f12p,4,black -DJ0.3c/0.2c
# 字体编号: 4=Times-Roman, 5=Times-Bold, 0=Helvetica
4.4 色标
gmt colorbar -Cterra -DJTC+w8c/0.5c+h -Baf+l"Elevation (m)"
4.5 图例
gmt legend -DJTR+o0.3c -F+gwhite+p0.5p << EOF
S 0.3c c 0.3c red 0.5p,black 0.7c Group A
EOF
第五步:DEM 地形数据获取
方案概览
| 数据源 | 分辨率 | 覆盖 | 是否需要 API Key | 国内可用 | |--------|--------|------|-----------------|----------| | 中科大 rsync | 30弧分~15弧秒 | 全球 | 否 | ✅ | | Copernicus S3 | 30m | 全球 | 否 | ✅ | | OpenTopography | 30m/90m | 全球 | 是(免费注册) | ✅ |
5.1 中科大 rsync(低分辨率全球数据)
rsync -av mirrors.ustc.edu.cn::gmtdata/server/earth/earth_relief/earth_relief_30m_g.grd ~/.gmt/cache/
5.2 Copernicus S3(高分辨率区域数据)
wget -O N36E117.tif \
"https://copernicus-dem-30m.s3.amazonaws.com/Copernicus_DSM_COG_10_N36_00_E117_00_DEM/Copernicus_DSM_COG_10_N36_00_E117_00_DEM.tif"
5.3 OpenTopography API(★ 推荐,最灵活的高分辨率 DEM)
OpenTopography 是全球高分辨率地形数据的开放平台,提供 30m/90m SRTM、ALOS、Copernicus 等数据集的 API 访问。GMT 6 内置了 OpenTopography 数据源支持,配置后可直接用 @earth_relief_01s 语法下载。
第 1 步:注册获取 API Key
- 打开 https://portal.opentopography.org/register
- 填写邮箱、姓名、机构,注册免费账号
- 登录后进入 MyOpenTopo → https://portal.opentopography.org/dashboard
- 在 dashboard 页面可以看到你的 API Key
第 2 步:把 API Key 告诉 Agent
用户将 API Key 发给 Agent 后,Agent 执行:
# 方式一:设置环境变量(推荐,不写入配置文件)
export OPENTOPO_API_KEY="你的API_KEY"
# 持久化到 shell 配置
echo 'export OPENTOPO_API_KEY="你的API_KEY"' >> ~/.bashrc
source ~/.bashrc
# 方式二:写入 GMT 配置文件(仅当前会话)
gmt set GMT_DATA_SERVER_OPENTOPO_API_KEY "你的API_KEY"
第 3 步:Agent 自动下载 DEM
配置好 API Key 后,Agent 可以直接用 GMT 内置语法:
# 下载研究区域的 1弧秒 SRTM DEM(自动通过 OpenTopography API)
gmt grdcut @earth_relief_01s -R114/123/34/38 -Gstudy_area.grd
# 直接绘图
gmt begin dem_map pdf
gmt grdimage study_area.grd -Cterra -I+d -JM15c -Baf
gmt coast -W0.3p -Swhite
gmt end
支持的 OpenTopography 数据集:
| GMT 语法 | 分辨率 | 数据集 |
|----------|--------|--------|
| @earth_relief_01s | 1弧秒 (~30m) | SRTM GL1 / ALOS World 3D |
| @earth_relief_03s | 3弧秒 (~90m) | SRTM GL3 |
| @earth_relief_15s | 15弧秒 (~450m) | SRTM15+ |
| @srtm_relief_01s | 1弧秒 | SRTM only |
| @srtm_relief_03s | 3弧秒 | SRTM only |
Agent 与用户交互流程
Agent: "要使用 OpenTopography 高分辨率 DEM,需要 API Key(免费)。"
Agent: "请访问 https://portal.opentopography.org/register 注册账号,"
Agent: "登录后在 https://portal.opentopography.org/dashboard 查看你的 API Key,"
Agent: "把 API Key 发给我,我会配置好。"
用户: "我的 API Key 是 xxxxxxxxxxxxxxxx"
Agent: 执行 export OPENTOPO_API_KEY="xxx" + echo 到 ~/.bashrc
Agent: 验证: gmt grdcut @earth_relief_01s -R测试区域 -Gtest.grd
Agent: "配置成功!以后画图我自动用 OpenTopography 获取高精度 DEM。"
5.4 DEM 分辨率选择速查
| 地图范围 | 推荐分辨率 | 数据源 | |----------|-----------|--------| | 全球/半球 | 30弧分 (~55km) | 中科大 rsync | | 大陆级 | 15弧秒 (~450m) | 中科大 rsync | | 区域级(省) | 30m (1弧秒) | OpenTopography / Copernicus | | 局部(矿区) | 30m | OpenTopography |
第六步:完整示例 — 区域地质样品分布图
将以下脚本保存为 sample_map.sh,修改上方的 REGION 和 PROJ 即可直接使用:
#!/bin/bash
# GMT 区域样品分布图模板
# 使用前改三个地方:
# 1. REGION 改为你的经纬度范围
# 2. PROJ 选择投影和尺寸
# 3. 准备数据文件 samples.txt (格式: lon lat group)
set -e
REGION="115/122/30/36" # ← 改成你的研究区域
PROJ="M15c" # ← 投影:M=Mercator, J=Orthographic, H=Hammer
OUT="sample_map"
# 检查 GMT
if ! command -v gmt &> /dev/null; then
echo "GMT not found. Run: conda create -n gmt -c conda-forge gmt"
exit 1
fi
gmt begin ${OUT} pdf
# 1. 底框
gmt basemap -R${REGION} -J${PROJ} -Ba2f1 -BWSen -B+t"Sample Distribution"
# 2. 海岸线
gmt coast -W0.3p,gray40 -Slightblue -Df
# 3. 样品点(按组别着色)
# 数据格式: lon lat group
if [ -f samples.txt ]; then
awk '$3=="andesite"' samples.txt | gmt plot -Sc0.3c -Gred -W0.5p,black -l"Andesite"
awk '$3=="granite"' samples.txt | gmt plot -Sc0.3c -Gblue -W0.5p,black -l"Granite"
awk '$3=="basalt"' samples.txt | gmt plot -St0.3c -Ggreen -W0.5p,black -l"Basalt"
else
echo "No samples.txt — create it with format: lon lat group"
fi
# 4. 文字标注(可选)
# echo "116.4 39.9 Beijing" | gmt text -F+f10p,5,black -DJ0.3c/0.2c
# 5. 图例
gmt legend -DJTR+o0.3c -F+gwhite+p0.5p << EOF
S 0.3c c 0.3c red 0.5p,black 0.7c Andesite
S 0.3c c 0.3c blue 0.5p,black 0.7c Granite
S 0.3c t 0.3c green 0.5p,black 0.7c Basalt
EOF
gmt end
echo "Done → ${OUT}.pdf"
运行:
chmod +x sample_map.sh
# 先准备你的 samples.txt,然后:
./sample_map.sh
常见错误排查
| 问题 | 原因 | 解决 |
|------|------|------|
| gmt: command not found | PATH 未设置 | export PATH=~/miniconda3/envs/gmt/bin:$PATH |
| Region ... not correctly specified | -R 格式错 | 必须是 lon1/lon2/lat1/lat2 |
| 海岸线盖住 DEM | 用了 -G 填色 | 去掉 -G,仅保留 -S |
| DEM 下载失败 | 网络屏蔽 | 中科大 rsync / Copernicus S3 / OpenTopography |
| Bad record counter | 缓存损坏 | rm -f ~/.gmt/gmt_data_server.txt |
| OpenTopography 403 | API Key 未配置 | 检查 echo $OPENTOPO_API_KEY |
| 中文乱码 | 字体不支持中文 | 用英文,或安装中文字体 |
学习路径
0. 加载本 skill → Agent 自动检测 OS → 一键安装 GMT → 验证 first_map
1. GMT 中文社区入门教程 → https://docs.gmt-china.org/latest/tutorial/started/
2. 跑通上面的完整示例 → 改成自己的数据和区域
3. 配置 OpenTopography API Key → 自动获取高精度 DEM
4. 添加 DEM 地形底图 → gmt grdimage + 光照
5. 进阶:自定义 CPT、等值线、多子图
→ https://docs.gmt-china.org/latest/tutorial/advanced/
6. 看绘图实例找灵感 → https://docs.gmt-china.org/latest/gallery/
参考资源
- GMT 中文社区: https://docs.gmt-china.org/latest/
- 官方文档: https://docs.generic-mapping-tools.org/
- GMT 中文论坛: https://community.gmt-china.org/
- OpenTopography 注册: https://portal.opentopography.org/register
- OpenTopography Dashboard: https://portal.opentopography.org/dashboard
- 本 skill 参考文件:
references/gmt-cheatsheet.md— 命令速查表references/gmt-dem-sources.md— DEM 数据源详解
Scan to join WeChat group