Xarray: New Feature: Add FacetGrid functionality to plot.line()

Created on 26 Apr 2018  路  5Comments  路  Source: pydata/xarray

When data is more than 2 dimensional, plot() creates a FacetGrid where each facet gets plots two dimensional data. I would find it very useful to have similar functionality for line plots.

Let's define three DataArrays, of 2,3 and 4 dimensions, named d2, d3 and d4.

d4=xr.DataArray(np.random.randn(3,4,5,6),
                dims=['x','y','z','w'],
                coords=[range(3),range(4),range(5),range(6)])
d3=d4[...,0].drop('w')
d2=d3[...,0].drop('z')

d4.plot(row='x', col='y')   #creates a 2d facet grid, each facet is a pcolormesh
d3.plot(col='x')           #creates a 1d facet grid, each facet is a pcolormesh
plt.show()

It would be very useful to have a similar interface for line plots too. That is, I would expect

d2.plot.line(row='y', x='x')

to create a row of panels, each having one line. Similarly, I would expect

d3.plot.line(col='y', x='x')
#or alternatively
d3.plot.line(col='y', hue='z')

to create a row of panels, each having a series of lines, with hue corresponding to z and so forth. Finally,

d4.plot.line(row='z',col='y', x='x')
#or alternatively
d4.plot.line(row='z',col='y', hue='w')

should create a 2d matrix of panels, each having a series of lines, with hue corresponding to w.

This seems to me like a natural generalization of the behaviour of plot(), and I found myself coding manually loops to do that a few times.

enhancement help wanted plotting

Most helpful comment

@shoyer I can try to work on it and already tried to dig through the code to see how it can be done. If I commit to doing that then (a) it'll take time and (b) I'll probably bug you to help me...

All 5 comments

CC @dcherian

I agree, this would be a useful feature. Any interest in working on a pull request? :)

I think this could be built on top of the existing FacetGrid functionality in xarray.plot (like xarray's other faceted plots), e.g., building on your example
xr.plot.FacetGrid(d3, col='y', row='z', size=1.5).map(plt.plot, 'x', Ellipsis) produces:
image

I'll note that usually I do this sort of visualization with seaborn's FacetGrid, but there could indeed be some value in supporting it directly in xarray:
http://seaborn.pydata.org/generated/seaborn.FacetGrid.html#seaborn.FacetGrid

@shoyer I can try to work on it and already tried to dig through the code to see how it can be done. If I commit to doing that then (a) it'll take time and (b) I'll probably bug you to help me...

Created a pull request: https://github.com/pydata/xarray/pull/2107
This is my first time contributing to an open-source project so be gentle. Any feedback will be appreciated.

can be closed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andrewpauling picture andrewpauling  路  3Comments

TomNicholas picture TomNicholas  路  4Comments

phausamann picture phausamann  路  3Comments

duncanwp picture duncanwp  路  4Comments

zxdawn picture zxdawn  路  3Comments