Vin*_*mar 4 python matplotlib geospatial cartopy
我想要一个包含两个子图的图,一个较大的带有地图,第二个较小的带有散点图。我正在使用 cartopy 来绘制地图。我使用 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]})
Run Code Online (Sandbox Code Playgroud)
一种可能的解决方案是仅对上面板使用 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])
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2050 次 |
最近记录: |