Plotting¶
Introduction¶
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:
- Seaborn: “provides a high-level interface for drawing attractive statistical graphics.” Integrates well with pandas.
- HoloViews and GeoViews: “Composable, declarative data structures for building even complex visualizations easily.” Includes native support for xarray objects.
- Cartopy: Provides cartographic tools.
Imports¶
The following imports are necessary for all of the examples.
In [1]: import numpy as np
In [2]: import pandas as pd
In [3]: import matplotlib.pyplot as plt
In [4]: import xarray as xr
For these examples we’ll use the North American air temperature dataset.
In [5]: airtemps = xr.tutorial.open_dataset('air_temperature')
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-5-892a7b38525b> in <module>()
----> 1 airtemps = xr.tutorial.open_dataset('air_temperature')
/build/python-xarray-DevH5s/python-xarray-0.11.3/xarray/tutorial.py in open_dataset(name, cache, cache_dir, github_url, branch, **kws)
66 # May want to add an option to remove it.
67 if not _os.path.isdir(longdir):
---> 68 _os.mkdir(longdir)
69
70 url = '/'.join((github_url, 'raw', branch, fullname))
FileNotFoundError: [Errno 2] No such file or directory: '/sbuild-nonexistent/.xarray_tutorial_data'
In [6]: airtemps