The following code produces the image I want, but the colourbar is blank/white, and doesn't match the data:
def plot_array(da, ax=None, shift=True):
"""plots an array of lat by lon on a coastline map"""
m = basemap.Basemap()
m.drawcoastlines()
m.pcolormesh(da.lon, y=da.lat, data=da.T, latlon=True)
return m
# 'monthly mean' is an xarray DataArray
fig = plt.figure(0, (14, 8))
grid = ImageGrid(fig, 111, nrows_ncols=(3, 4), axes_pad=0.3,
cbar_mode='single', cbar_location="bottom",)
for i, m in enumerate(months):
plt.sca(grid[i])
plot_array(monthly_mean.sel(month=i + 1))
plt.title(m)
plt.suptitle("{b} - {y} monthly means".format(b=benchmark, y=year))
# plt.colorbar()
plt.tight_layout()
os.makedirs("plots/monthly_means/{y}".format(y=year), exist_ok=True)
plt.savefig("plots/monthly_means/{y}/{b}_{y}.png".format(b=benchmark, y=year))
plt.close()
Is there something else I need to do to get the colorbar working with ImageGrid, or do ImageGrid and Basemap just not play well together?
plt.colorbar(cax=grid[0].cax)
, I get an error:RuntimeError: No mappable was found to use for colorbar creation. First define a mappable such as an image (with imshow) or a contour set (with contourf).
and if I tryplt.colorbar(plt.gca(), cax=grid[0].cax)
I get the errorAttributeError: 'CbarAxes' object has no attribute 'autoscale_None'
Finally, if I tryplt.colorbar(grid[0],cax=grid[0].cax)
I get the errorAttributeError: 'LocatableAxes' object has no attribute 'autoscale_None
Any idea how to fix this? – Semiyearly