有没有办法在同一个图形中将两个卡通地图绘制为子图?

2024-05-14 23:32:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图按照this page结尾提供的代码实现这一点。 我尝试了以下几点:

fig, (ax1, ax2) = plt.subplots(1,2, figsize=(24, 10))
fig.suptitle('Comparison of 1970-2000 and 2070-2100 Soil Moisture Content')
ax1 = plt.axes(projection=ccrs.PlateCarree())
ax1.set_title('Under SSP245 Scenario')
cf1 = ax1.contourf(lon_hist, lat_hist, mrsos_change45, transform=ccrs.PlateCarree(), cmap='BrBG')
fig.colorbar(cf1, ax=ax1)
ax1.set_aspect('equal', adjustable=None)
ax1.add_feature(ctp.feature.BORDERS, linestyle='-', alpha=1)
ax1.coastlines(resolution='10m')
ax1.add_feature(ctp.feature.OCEAN, zorder=100, edgecolor='k')
ax1.coastlines()
ax1.gridlines(draw_labels=True)
    


ax2 = plt.axes(projection=ccrs.PlateCarree())
ax2.set_title('Under SSP585 Scenario')
cf2 = ax2.contourf(lon_hist, lat_hist, mrsos_change85, cf1.levels, transform=ccrs.PlateCarree(), cmap='BrBG')
fig.colorbar(cf2, ax=ax2)
ax2.set_aspect('equal')
ax2.add_feature(ctp.feature.BORDERS, linestyle='-', alpha=1)
ax2.coastlines(resolution='10m')
ax2.add_feature(ctp.feature.OCEAN, zorder=100, edgecolor='k')
ax2.coastlines()
ax2.gridlines(draw_labels=True)

plt.show()

但这只会生成我正在尝试生成的两张卡通地图中的第二张。 不幸的是,我无法提供我正在使用的数据,因为它是非常大的数据文件。但是,有人能看到阻止它生成第一个代码的代码中的错误吗


Tags: 代码addfigpltctphistfeatureset
1条回答
网友
1楼 · 发布于 2024-05-14 23:32:23

创建ax1和ax2,然后在代码中销毁/重新定义ax1ax2。解决方案首先是正确地创建它们,而不是重新定义它们

# modified code (relevant parts only)

import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import cartopy

# Use subplot_kw to declare the projection
fig, (ax1, ax2) = plt.subplots(1,2, figsize=(24, 7), subplot_kw={"projection": ccrs.PlateCarree()})
fig.suptitle('Comparison of 1970-2000 and 2070-2100 Soil Moisture Content')

#ax1 = plt.axes(projection=ccrs.PlateCarree())  #this destroys/redefines ax1
ax1.set_title('Under SSP245 Scenario')
#cf1 = ax1.contourf(lon_hist, lat_hist, mrsos_change45, transform=ccrs.PlateCarree(), cmap='BrBG')
#fig.colorbar(cf1, ax=ax1)
ax1.set_aspect('equal', adjustable=None)
ax1.add_feature(cartopy.feature.BORDERS, linestyle='-', alpha=1)
#ax1.coastlines(resolution='10m')
ax1.add_feature(cartopy.feature.OCEAN, zorder=100, edgecolor='k')
ax1.coastlines()
ax1.gridlines(draw_labels=True)

#ax2 = plt.axes(projection=ccrs.PlateCarree())  #this destroys/redefines ax2
ax2.set_title('Under SSP585 Scenario', fontsize=14, va='bottom', backgroundcolor="white")
#cf2 = ax2.contourf(lon_hist, lat_hist, mrsos_change85, cf1.levels, transform=ccrs.PlateCarree(), cmap='BrBG')
#fig.colorbar(cf2, ax=ax2)
#ax2.set_aspect('equal')
ax2.add_feature(cartopy.feature.BORDERS, linestyle='-', alpha=1)
#ax2.coastlines(resolution='10m')
ax2.add_feature(cartopy.feature.OCEAN, zorder=100, edgecolor='k')
ax2.coastlines()
ax2.gridlines(draw_labels=True)

plt.show()

2subplots

相关问题 更多 >

    热门问题