0
votes

I have attempted to achieve this following the code provided at the end of this page. What I have tried is the following:

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()

But this only produces one the second of the two cartopy maps that I am trying to produce. Unfortunately, I am unable to provide the data I am using as it is very large data files. But can anyone see mistakes in the code that stops it from producing the first one?

1
To make it a subplot, try the following. plt.figure(figsize=(24,10));ax1 = plt.subplot(1,2,1, projection=ccrs.PlateCarree());ax2 = plt.subplot(1,2,2, projection=ccrs.PlateCarree())r-beginners

1 Answers

0
votes

You create ax1 and ax2 then destroy/redefine both ax1 and ax2 in your code. The solution is creating them correctly in the first place, and do not redefine them.

# 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