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 DataArray objects. To plot Dataset objects simply access the relevant DataArrays, i.e. dset['var1']. Dataset specific plotting routines are also available (see Dataset Plotting). 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.

To use xarray’s plotting capabilities with time coordinates containing cftime.datetime objects nc-time-axis v1.3.0 or later needs to be installed.

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.

  • hvplot: hvplot makes it very easy to produce dynamic plots (backed by Holoviews or Geoviews) by adding a hvplot accessor to DataArrays.

  • Cartopy: Provides cartographic tools.

Getting Started#

The plotting functionality in xarray is organized into several focused sections:

  • Line plots: For 1-dimensional data and time series

  • 2D plots: For images, maps, and spatial data

  • Faceting: For creating multi-panel plots (small multiples)

  • Dataset plotting: For scatter plots, quiver plots, and vector data visualization

Each section provides detailed examples and best practices for that type of visualization.

The following topics are covered in the subsections below: