matplotlibplotpython-xarraycartopy

Plot coastline over Xarray data for individual subplot


I'd like to overlay a coastline from cartopy onto one of my subplots but not the other, all of the solutions I've found so far lay the coastline over every subplot. Here is what I'm currently generating - enter image description here

Using the below code.

  fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(10,5))
  da.max(dim=["latitude", "longitude"]).plot(ax=axes[0])
  da.max(dim=["year"]).plot.pcolormesh(x="longitude",y="latitude", ax=axes[1])

When I tried to set individual axis with fig.add_gridspec there were issues with outdated engine compatibility presumably between xarray plotting and Matplotlib? It's unclear how I could add a projection to an axis not the figure.

The country I want to overlay is Australia btw.


Solution

  • Can you do

    fig = plt.figure(figsize=(10,5))
    
    ax1 = fig.add_subplot(1, 2, 1)
    da.max(dim=["latitude", "longitude"]).plot(ax=ax1)
    
    ax2 = fig.add_subplot(1, 2, 2, projection=my_chosen_projection)
    da.max(dim=["year"]).plot.pcolormesh(x="longitude",y="latitude", ax=ax2)
    ax2.coastlines()
    

    ?