Labeled data enables expressive computations. These same
labels can also be used to easily create informative plots.

xarray’s plotting capabilities are centered around
xarray.DataArray objects.
To plot xarray.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.

xarray plotting functionality is a thin wrapper around the popular
matplotlib library.
Matplotlib syntax and function names were copied as much as possible, which
makes for an easy transition between the two.
Matplotlib must be installed before xarray can plot.

For more extensive plotting applications consider the following projects:

Additional arguments are passed directly to the matplotlib function which
does the work.
For example, xarray.plot.line() calls
matplotlib.pyplot.plot passing in the index and the array values as x and y, respectively.
So to make a line plot with blue triangles a matplotlib format string
can be used:

In [10]: air1d[:200].plot.line('b-^')Out[10]: [<matplotlib.lines.Line2D at 0x7f7e163b1ed0>]

Note

Not all xarray plotting methods support passing positional arguments
to the wrapped matplotlib functions, but they do all
support keyword arguments.

Keyword arguments work the same way, and are more explicit.

In [11]: air1d[:200].plot.line(color='purple',marker='o')Out[11]: [<matplotlib.lines.Line2D at 0x7f7e0cc85f90>]

To add the plot to an existing axis pass in the axis as a keyword argument
ax. This works for all xarray plotting methods.
In this example axes is an array consisting of the left and right
axes created by plt.subplots.

All 2d plots in xarray allow the use of the keyword arguments yincrease
and xincrease.

In [20]: air2d.plot(yincrease=False)Out[20]: <matplotlib.collections.QuadMesh at 0x7f7e16134510>

Note

We use xarray.plot.pcolormesh() as the default two-dimensional plot
method because it is more flexible than xarray.plot.imshow().
However, for large arrays, imshow can be much faster than pcolormesh.
If speed is important to you and you are plotting a regular mesh, consider
using imshow.

In [24]: b=air2d.copy()# Apply a nonlinear transformation to one of the coordsIn [25]: b.coords['lat']=np.log(b.coords['lat'])In [26]: b.plot()Out[26]: <matplotlib.collections.QuadMesh at 0x7f7e0c45f250>

xarray methods update label information and generally play around with the
axes. So any kind of updates to the plot
should be done after the call to the xarray’s plot.
In the example below, plt.xlabel effectively does nothing, since
d_ylog.plot() updates the xlabel.

This plot shows that we have outliers. The easy way to visualize
the data without the outliers is to pass the parameter
robust=True.
This will use the 2nd and 98th
percentiles of the data to compute the color limits.

In [41]: air_outliers.plot(robust=True)Out[41]: <matplotlib.collections.QuadMesh at 0x7f7e0c288050>

Observe that the ranges of the color bar have changed. The arrows on the
color bar indicate
that the colors include data points outside the bounds.

It is often useful, when visualizing 2d data, to use a discrete colormap,
rather than the default continuous colormaps that matplotlib uses. The
levels keyword argument can be used to generate plots with discrete
colormaps. For example, to make a plot with 8 discrete color intervals:

In [42]: air2d.plot(levels=8)Out[42]: <matplotlib.collections.QuadMesh at 0x7f7e0bec8b90>

It is also possible to use a list of levels to specify the boundaries of the
discrete colormap:

In [43]: air2d.plot(levels=[0,12,18,30])Out[43]: <matplotlib.collections.QuadMesh at 0x7f7e0bddd390>

You can also specify a list of discrete colors through the colors argument:

Finally, if you have Seaborn
installed, you can also specify a seaborn color palette to the cmap
argument. Note that levelsmust be specified with seaborn color palettes
if using imshow or pcolormesh (but not with contour or contourf,
since levels are chosen automatically).

In [46]: air2d.plot(levels=10,cmap='husl')Out[46]: <matplotlib.collections.QuadMesh at 0x7f7e08058710>

Faceting here refers to splitting an array along one or two dimensions and
plotting each group.
xarray’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.

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.

xarray.plot.FacetGrid is used to control the behavior of the
multiple plots.
It borrows an API and code from Seaborn.
The structure is contained within the axes and name_dicts
attributes, both 2d Numpy object arrays.

Here the output is the same. Since the data is 1 dimensional the line plot
was used.

The convenience method xarray.DataArray.plot() dispatches to an appropriate
plotting function based on the dimensions of the DataArray and whether
the coordinates are sorted and uniformly spaced. This table
describes what gets plotted:

The plot will produce an image corresponding to the values of the array.
Hence the top left pixel will be a different color than the others.
Before reading on, you may want to look at the coordinates and
think carefully about what the limits, labels, and orientation for
each of the axes should be.

In [79]: a.plot()Out[79]: <matplotlib.collections.QuadMesh at 0x7f7e0725a450>

It may seem strange that
the values on the y axis are decreasing with -0.5 on the top. This is because
the pixels are centered over their coordinates, and the
axis labels and ranges correspond to the values of the
coordinates.