同图中法线图和卡托图的组合。

问题描述 投票:0回答:1

我想要一个有两个子图的图,一个较大的地图和第二个较小的散点图。我使用cartopy来绘制地图,我使用gridspec_kw来确定高度的分数。我通过使用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_kw=dict(projection=ccrs.crs.PlateCarree())。有一些方法推荐使用add_subplot,但那是非常手动的,我不喜欢这样。是否可以用plt.subplots()来做?

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

python matplotlib geospatial cartopy
1个回答
0
投票

我的建议是使用 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())
ax.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

© www.soinside.com 2019 - 2024. All rights reserved.