python - 同一图中的正常和 cartopy 子图的组合

标签 python matplotlib geospatial cartopy

我想要一个带有两个子图的图,一个较大的带有 map ,第二个较小的带有散点图。我正在使用 cartopy 绘制 map 。我使用 gridspec_kw 确定高度的分数。但是,由于投影限制,它也会影响宽度。 This is what i get .

这就是我得到的。

import matplotlib.pyplot as plt
import cartopy as ccrs
fig, ax = plt.subplots(2,1,subplot_kw=dict(projection=ccrs.crs.PlateCarree()),gridspec_kw={'height_ratios': [4, 1]})

一种可能的解决方案是仅对上面板使用 subplot_kw=dict(projection=ccrs.crs.PlateCarree()。但我无法弄清楚如何做到这一点。有推荐 add_subplot 的方法,但这是非常手动的我不喜欢这个。可以用 plt.subplots() 做吗?

This is what I want
这就是我要的。

最佳答案

我的建议是使用 gridspec控制子图的大小和 fig.add_subplot而不是 plt.subplots .这样你就可以只对第一个子图指定 Cartopy 投影。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import cartopy.crs as ccrs
import cartopy.feature as cfeature

fig = plt.figure()
gs = fig.add_gridspec(3, 3)

ax1 = fig.add_subplot(gs[0:2, :], projection=ccrs.PlateCarree())
ax1.set_extent([-180, 180, -90, 90], crs=ccrs.PlateCarree())
ax1.coastlines(resolution='auto', color='k')
ax1.gridlines(color='lightgrey', linestyle='-', draw_labels=True)

ax2 = fig.add_subplot(gs[2, :])
ax2.plot([1, 2], [3, 4])
enter image description here

关于python - 同一图中的正常和 cartopy 子图的组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61433850/

相关文章:

python - cv2.VideoCapture卡住了我的整个计算机

python - 使用类型提示时如何向函数添加默认参数?

python - 如何在python中显示RGB图像

python - 使用 Matplotlib 和 PyQt 进行动态绘图 - 卡住窗口

R_从 SpatialPolygonsDataFrame 中提取坐标

javascript - 如何将这段 Python 翻译成地道的 Javascript

Python尝试使用opener打开页面的时间

python - 将文本添加到 pandas dataframe plot

mysql - 你如何使用 MySQL 空间查询来查找 X 半径内的所有记录?

geospatial - 我可以在 postgis 的同一个空间索引列中存储圆和多边形吗?