Closed andreas-h closed 2 years ago
I agree, this is unfortunate. I suspect the source of the discrepancy is that we use a totally different codepath if either col
or row
is supplied, creating our own subplot grid.
I think this is fixed in the latest master.
import xarray as xr
import numpy as np
import cartopy.crs as ccrs
da = xr.tutorial.open_dataset('air_temperature')['air']
p = da.isel(time=[0, 1]).plot(
transform=ccrs.PlateCarree(), col='time',
subplot_kws={'projection': ccrs.Orthographic(-80, 35)}
)
for ax in p.axes.flat:
ax.coastlines()
ax.gridlines()
p = da.isel(time=0).plot(
transform=ccrs.PlateCarree(),
subplot_kws={'projection': ccrs.Orthographic(-80, 35)}
)
However if you try iterating over the axes, it will crash because p
is quadmesh type not a facetgrid type so I was wondering whether it'd be good to have an keyword like force_facetgrid_type=True
which uses the FacetGrid class to plot a single map so that the user can keep the same code for single axes and facet axes?
p = da.isel(time=0).plot(
transform=ccrs.PlateCarree(),
subplot_kws={'projection': ccrs.Orthographic(-80, 35)}
)
for ax in p.axes.flat:
ax.coastlines()
ax.gridlines()
As a workaround you can use:
p = da.isel(time=[0]).plot.pcolormesh(
col="time",
transform=ccrs.PlateCarree(),
subplot_kws={'projection': ccrs.Orthographic(-80, 35)}
)
Note the time=[0]
- using time=0
squezes the time dimension. Also note the .plot.pcolormesh
. Using just .plot
also calls daarray.squeeze()
.
In order to maintain a list of currently relevant issues, we mark issues as stale after a period of inactivity
If this issue remains relevant, please comment here or remove the stale
label; otherwise it will be marked as closed automatically
I'm on xarray 0.11.3, and currently do not have the possibility to test on master. However, the commit history suggests that this issue still exists. Please accept my apologies if I missed something.
I'm following the plotting documentation at https://xarray.pydata.org/en/stable/plotting.html#maps
When plotting two maps in the same call (exactly as written in the documentation),
everything works fine.
However, when I only want to plot one map,
I get the following error:
It is not clear (from the user perspective) why one should work and the other should not.
Of course I can create the axes instance before using xarray's
plot
function, but it would be nicer if I wouldn't have to.