-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
faceted plots #527
Changes from 37 commits
3030f86
d5ef818
30385f8
9e9d8ee
9f12a32
8516c5c
2f1fa0c
ef5e2e2
e8e2f1e
900ec93
058fda9
a82e019
e99d1d5
68c7b06
a5d37df
e7bce95
11e9f2b
6686988
7d9cdbb
fbfbad1
9cdfab7
a0ce7cf
870025a
43f4b4a
afed851
92d1189
c4abf41
e72e8cd
1546dc2
07e02bb
ab02f41
5dccfb0
78c584d
eab83a5
1043bd2
266b0bf
29db010
56761af
f6dc6a2
15babf1
99dbb5e
b48a50b
3247fdb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is this doing? I think nothing. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
||
|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
---- | ||
|
||
|
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 |
There was a problem hiding this comment.
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.