How do I add a coastline to an xarray.plot.facetgrid.FacetGrid?

Question:

I am attempting to make a figure with 12 plots showing the monthly average of a parameter from the 1800’s to the 2100’s for each month. I have the plots pretty much finished with the average monthly data for each month and 12 plots, I just want to add coastlines. I have attempted to add "coastlines = True" to the end of my contour() plotting line, where I got a warning message stating, "The following kwargs were not used by contour: ‘coastline’". I have also tried using ax.coastlines() after my plotting command and I get an error message saying, "’FacetGrid’ object has no attribute ‘coastlines’". The final thing I tried to do is below:

fg = cape_climo.plot.contourf(x = 'lon', y = 'lat', col =    'month', col_wrap =3, ,
                        subplot_kws={
        "projection": ccrs.LambertConformal()}, cbar_kwargs={"orientation": "vertical", "shrink": 0.8, "aspect": 40},
    robust=True) 

fg.map(lambda: plt.gca().coastlines())

I get no error or warning message with this…but I also get no coastlines.

Asked By: franke11

||

Answers:

fg contains a axes property containing all created axes, so you can do the following:

[ax.coastlines() for ax in fg.axes.flatten()]

Note in newer versions of xarray this is axs instead of axes:

[ax.coastlines() for ax in fg.axs.flatten()]
Answered By: mathause