Xarray: FacetGrid with independent colorbars

Created on 27 Apr 2017  路  6Comments  路  Source: pydata/xarray

Sometimes the magnitude of a variable can vary dramatically across a given coordinate, which makes 2d plots generated by xr.FacetGrid difficult to interpret. It would be useful if an option to xr.FacetGrid could be specified which allows each subplot to have its own colorbar.

plotting

Most helpful comment

I tend to agree with you @shoyer. In that case it would be nice to have a Dataset.plot convenience method for plotting all the variables in a Dataset. This method could handle all the annoying boiler plate associated with subplots/AxesGrid.

All 6 comments

Seems reasonable, although this would imply quite a lot of change in the current logic. For example, facetgrids share x and/or y axis:

index

If there is a colorbar in between, would you like to share axes?

The current way to do what you need is to do it manually:

ds = xr.tutorial.load_dataset('air_temperature')
t = ds.air.groupby('time.season').mean(dim='time')
f, (ax1, ax2) = plt.subplots(1, 2, figsize=(7, 3))
t.isel(season=0).plot(ax=ax1)
t.isel(season=1).plot(ax=ax2)

index2

As you see, this messes up with the layout so a call to tight_layout() is necessary.

whoops. accidentally hit the close button.

@fmaussion I was worried that would be the case. I think matplotlib's AxesGrid module is the appropriate tool to handle this use case. Unfortunately, I find the AxesGrid API pretty unpleasant for interactive use.

The AxesGrid API isn't actually that bad. See this

from mpl_toolkits.axes_grid1 import ImageGrid

import xarray as xr

fig = plt.figure(figsize=(6,2.5))
grid = ImageGrid(fig, 111, (1,2),
                 axes_pad=.1,
                 cbar_location='bottom',
                 cbar_mode='each',
                 cbar_pad=0.3,
                 aspect=False,
                 cbar_size='3%')

for (lab,data), ax, cax in zip(t.groupby('season'), grid, grid.cbar_axes):
    im = ax.contourf(data)
    cax.colorbar(im)

This does kind of go against the point of FacetGrid, which is to display facets of the same variables in a grid. If your plots should have different colorbars, then arguably they should be different data variables in a Dataset.

That said, this does seem similar in spirit to the sharex and sharey arguments. Maybe sharez or share_vlim would be appropriate arguments for changing this?

Squeezing multiple colorbars in will be a little trickier than just allowing colorbar limits to vary, but if it can be done in a sane fashion that doesn't make the existing code more complex I would be in support.

I tend to agree with you @shoyer. In that case it would be nice to have a Dataset.plot convenience method for plotting all the variables in a Dataset. This method could handle all the annoying boiler plate associated with subplots/AxesGrid.

I like @shoyer 's suggestion as well. This would be very useful if the data being plotted are equal-sized images, in which case the colorbar could be dropped altogether as it no longer makes sense for them to share the same range.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

duncanwp picture duncanwp  路  4Comments

blaylockbk picture blaylockbk  路  4Comments

ray306 picture ray306  路  4Comments

byersiiasa picture byersiiasa  路  5Comments

tomchor picture tomchor  路  4Comments