我想要一个带有两个子图的图,一个较大的带有 map ,第二个较小的带有散点图。我正在使用 cartopy 绘制 map 。我使用 gridspec_kw 确定高度的分数。但是,由于投影限制,它也会影响宽度。 .
这就是我得到的。
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() 做吗?
这就是我要的。
最佳答案
我的建议是使用 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])
关于python - 同一图中的正常和 cartopy 子图的组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61433850/