Skip to content

faceted plots #527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 43 commits into from
Sep 4, 2015
Merged
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
3030f86
add parameter add_labels=True to turn off labeling
clarkfitzg Aug 3, 2015
d5ef818
Merge branch 'master' into facet2
clarkfitzg Aug 12, 2015
30385f8
facets share common colorbar
clarkfitzg Aug 13, 2015
9e9d8ee
display colorbar on facetgrid
clarkfitzg Aug 13, 2015
9f12a32
beginning logic for row/col facets
clarkfitzg Aug 13, 2015
8516c5c
about to bring in set_label from seaborn
clarkfitzg Aug 13, 2015
2f1fa0c
labeled grid using seaborn
clarkfitzg Aug 13, 2015
ef5e2e2
rotate text to match
clarkfitzg Aug 14, 2015
e8e2f1e
properly place colorbar label
clarkfitzg Aug 14, 2015
900ec93
add failing test for figure labels
clarkfitzg Aug 14, 2015
058fda9
Merge branch 'master' into facet2
clarkfitzg Aug 24, 2015
a82e019
Merge branch 'master' into facet2
clarkfitzg Aug 24, 2015
e99d1d5
data structures for facetgrid
clarkfitzg Aug 25, 2015
68c7b06
failing test for floating index
clarkfitzg Aug 25, 2015
a5d37df
failing test for nonunique index
clarkfitzg Aug 25, 2015
e7bce95
handle nonunique coordinates
clarkfitzg Aug 25, 2015
11e9f2b
refactor set_titles, add failing test for long titles
clarkfitzg Aug 25, 2015
6686988
use core.formatting for titles
clarkfitzg Aug 25, 2015
7d9cdbb
failing test for robust kwarg
clarkfitzg Aug 26, 2015
fbfbad1
refactoring _determine_cmap_parms
clarkfitzg Aug 28, 2015
9cdfab7
predictable colormaps for facetgrids
clarkfitzg Aug 28, 2015
a0ce7cf
better tests for colorbars
clarkfitzg Aug 28, 2015
870025a
col wrapping works now
clarkfitzg Aug 28, 2015
43f4b4a
failing test for font size
clarkfitzg Aug 31, 2015
afed851
refactoring init
clarkfitzg Aug 31, 2015
92d1189
reorganizing docs
clarkfitzg Aug 31, 2015
c4abf41
remove all random tests from plotting
clarkfitzg Sep 1, 2015
e72e8cd
Merge branch 'master' into facet2
clarkfitzg Sep 1, 2015
1546dc2
helper function for x, y label inference
clarkfitzg Sep 1, 2015
07e02bb
make axes invisible for ragged case
clarkfitzg Sep 1, 2015
ab02f41
use ticker.maxNLocator to control ticks
clarkfitzg Sep 2, 2015
5dccfb0
take out global imports of maptplotlib
clarkfitzg Sep 3, 2015
78c584d
add docs from seaborn, generate docs, set_font
clarkfitzg Sep 3, 2015
eab83a5
Colorbar positioning tweak and use tight_layout()
shoyer Sep 3, 2015
1043bd2
Merge pull request #1 from shoyer/facet2
clarkfitzg Sep 3, 2015
266b0bf
address Stephans comments and pep8 cleanup
clarkfitzg Sep 4, 2015
29db010
Use default axis labels and sizes
clarkfitzg Sep 4, 2015
56761af
change from contourf to imshow for faceted plotting examples
clarkfitzg Sep 4, 2015
f6dc6a2
darray now called data, remove iterable behavior on FacetGrid
clarkfitzg Sep 4, 2015
15babf1
attributes all set in bottom of __init__
clarkfitzg Sep 4, 2015
99dbb5e
don't show FacetGrid methods directly in API docs
shoyer Sep 4, 2015
b48a50b
rename plotfunc arg to func
clarkfitzg Sep 4, 2015
3247fdb
Merge pull request #2 from shoyer/facet2
clarkfitzg Sep 4, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,15 @@ Plotting
plot.imshow
plot.line
plot.pcolormesh
plot.FacetGrid

FacetGrid methods
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one more suggestion: clarkfitzg#2

we can remove this methods and let them just show up on the doc page for FacetGrid.

-----------------

.. autosummary::
:toctree: generated/

plot.FacetGrid.map_dataarray
plot.FacetGrid.set_titles
plot.FacetGrid.set_ticks
plot.FacetGrid.map
112 changes: 111 additions & 1 deletion doc/plotting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ Xray's plotting capabilities are centered around
:py:class:`xray.DataArray` objects.
To plot :py:class:`xray.Dataset` objects
simply access the relevant DataArrays, ie ``dset['var1']``.
Here we focus mostly on arrays 2d or larger. If your data fits
nicely into a pandas DataFrame then you're better off using one of the more
developed tools there.

Xray plotting functionality is a thin wrapper around the popular
`matplotlib <http://matplotlib.org/>`_ library.
Expand Down Expand Up @@ -45,16 +48,18 @@ Imports
# Use defaults so we don't get gridlines in generated docs
import matplotlib as mpl
mpl.rcdefaults()
mpl.rcParams.update({'figure.autolayout': True})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this doing? I think nothing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this is undoing the seaborn import? If it doesn't seem to be changing the doc build, take it out.


The following imports are necessary for all of the examples.

.. ipython:: python

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import xray

We'll use the North American air temperature dataset.
For these examples we'll use the North American air temperature dataset.

.. ipython:: python

Expand Down Expand Up @@ -306,6 +311,111 @@ since levels are chosen automatically).
air2d.plot(levels=10, cmap='husl')


Faceting
--------

Faceting here refers to splitting an array along one or two dimensions and
plotting each group.
Xray's basic plotting is useful for plotting two dimensional arrays. What
about three or four dimensional arrays? That's where facets become helpful.

Consider the temperature data set. There are 4 observations per day for two
years which makes for 2920 values along the time dimension.
One way to visualize this data is to make a
seperate plot for each time period.

The faceted dimension should not have too many values;
faceting on the time dimension will produce 2920 plots. That's
too much to be helpful. To handle this situation try performing
an operation that reduces the size of the data in some way. For example, we
could compute the average air temperature for each month and reduce the
size of this dimension from 2920 -> 12. A simpler way is
to just take a slice on that dimension.
So let's use a slice to pick 6 times throughout the first year.

.. ipython:: python

t = air.isel(time=slice(0, 365 * 4, 250))
t.coords

Simple Example
~~~~~~~~~~~~~~

TODO - replace with the convenience method from plot

We can use :py:meth:`xray.FacetGrid.map_dataarray` on a DataArray:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

link


.. ipython:: python

g = xray.plot.FacetGrid(t, col='time', col_wrap=3)

@savefig plot_facet_dataarray.png height=12in
g.map_dataarray(xray.plot.contourf, 'lon', 'lat')

FacetGrid Objects
~~~~~~~~~~~~~~~~~

:py:class:`xray.plot.FacetGrid` is used to control the behavior of the
multiple plots.
It borrows an API and code from `Seaborn
<http://stanford.edu/~mwaskom/software/seaborn/tutorial/axis_grids.html>`_.

Iterating over the FacetGrid iterates over the individual axes.
Pick out individual axes using the ``.axes`` attribute.

.. ipython:: python

g = (xray.plot
.FacetGrid(t, col='time', col_wrap=3)
.map_dataarray(xray.plot.contourf, 'lon', 'lat')
)

for i, ax in enumerate(g):
ax.set_title('Air Temperature %d' % i)

bottomright = g.axes[-1, -1]
bottomright.annotate('bottom right', (240, 40))

@savefig plot_facet_iterator.png height=12in
plt.show()

4 dimensional
~~~~~~~~~~~~~~

For 4 dimensional arrays we can use the rows and columns of the grids.
Here we create a 4 dimensional array by taking the original data and adding
a fixed amount. Now we can see how the temperature maps would compare if
one were much hotter.

.. ipython:: python

t2 = t.isel(time=slice(0, 2))
t4d = xray.concat([t2, t2 + 40], pd.Index(['normal', 'hot'], name='fourth_dim'))
# This is a 4d array
t4d.coords

g = xray.plot.FacetGrid(t4d, col='time', row='fourth_dim')

@savefig plot_facet_4d.png height=12in
g.map_dataarray(xray.plot.imshow, 'lon', 'lat')

Other features
~~~~~~~~~~~~~~

Faceted plotting supports other arguments common to xray 2d plots.

.. ipython:: python

hasoutliers = t.isel(time=slice(0, 5)).copy()
hasoutliers[0, 0, 0] = -100
hasoutliers[-1, -1, -1] = 400

g = xray.plot.FacetGrid(hasoutliers, col='time', col_wrap=3)

@savefig plot_facet_robust.png height=12in
g.map_dataarray(xray.plot.contourf, 'lon', 'lat', robust=True, cmap='viridis')


Maps
----

Expand Down
2 changes: 2 additions & 0 deletions xray/plot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
from .plot import (plot, line, contourf, contour,
hist, imshow, pcolormesh)

from .facetgrid import FacetGrid
Loading