[英]Is there a way to plot two cartopy maps as subplots in the same figure?
[英]combination of normal and cartopy subplots within the same figure
我想要一個帶有兩個子圖的 plot,一個較大的帶有 map,第二個較小的帶有散點圖 plot。 我正在使用 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])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.