Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 11 additions & 3 deletions doc/plotting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,6 @@ If required, the automatic legend can be turned off using ``add_legend=False``.
``hue`` can be passed directly to :py:func:`xarray.plot` as `air.isel(lon=10, lat=[19,21,22]).plot(hue='lat')`.




Dimension along y-axis
~~~~~~~~~~~~~~~~~~~~~~

Expand All @@ -225,7 +223,7 @@ It is also possible to make line plots such that the data are on the x-axis and
air.isel(time=10, lon=[10, 11]).plot(y='lat', hue='lon')

Changing Axes Direction
-----------------------
~~~~~~~~~~~~~~~~~~~~~~~

The keyword arguments ``xincrease`` and ``yincrease`` let you control the axes direction.

Expand All @@ -234,6 +232,16 @@ The keyword arguments ``xincrease`` and ``yincrease`` let you control the axes d
@savefig plotting_example_xincrease_yincrease_kwarg.png
air.isel(time=10, lon=[10, 11]).plot.line(y='lat', hue='lon', xincrease=False, yincrease=False)

Errorbars
~~~~~~~~~

If provided with the keyword arguments ``xerr`` and ``yerr``, errorbars will be added. Additional ``kwargs`` will be passed to matplotlib's ``errorbar()``. Currently this functionality is only supported for 1D DataArrays; i.e. the following would not work with ``air.isel(time=10, lon=[10, 11])``.

.. ipython:: python

@savefig plotting_example_yerr_kwarg.png
air.isel(time=10, lon=10).plot.line(y='lat', xerr=3, yerr=1.5, ecolor='r')

Two Dimensions
--------------

Expand Down
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ Enhancements
(:issue:`2218`)
By `Keisuke Fujii <https://github.com/fujiisoup>`_.

- :py:meth:`~xarray.DataArray.plot()` will now plot errorbars if provided with kwargs ``xerr, yerr``.
By `Deepak Cherian <https://github.com/dcherian>`_.


Bug fixes
~~~~~~~~~
Expand Down
11 changes: 10 additions & 1 deletion xarray/plot/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ def line(darray, *args, **kwargs):
Coordinates for x, y axis. Only one of these may be specified.
The other coordinate plots values from the DataArray on which this
plot method is called.
xerr, yerr : Errorbar sizes (optional)
Copy link
Member

Choose a reason for hiding this comment

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

Can we expand this description?

xincrease : None, True, or False, optional
Should the values on the x axes be increasing from left to right?
if None, use the default for the matplotlib function.
Expand Down Expand Up @@ -308,6 +309,8 @@ def line(darray, *args, **kwargs):
y = kwargs.pop('y', None)
xincrease = kwargs.pop('xincrease', True)
yincrease = kwargs.pop('yincrease', True)
xerr = kwargs.pop('xerr', None)
yerr = kwargs.pop('yerr', None)
add_legend = kwargs.pop('add_legend', True)
_labels = kwargs.pop('_labels', True)

Expand All @@ -317,7 +320,13 @@ def line(darray, *args, **kwargs):

_ensure_plottable(xplt)

primitive = ax.plot(xplt, yplt, *args, **kwargs)
if xerr is None and yerr is None:
primitive = ax.plot(xplt, yplt, *args, **kwargs)
else:
if xplt.ndim > 1 or yplt.ndim > 1:
raise ValueError('xerr, yerr can only be used with 1D data.')

primitive = ax.errorbar(xplt, yplt, xerr=xerr, yerr=yerr, **kwargs)

if _labels:
if xlabel is not None:
Expand Down
9 changes: 9 additions & 0 deletions xarray/tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ def test_2d_line(self):
self.darray[:, :, 0].plot.line(x='dim_0', hue='dim_1')
self.darray[:, :, 0].plot.line(y='dim_0', hue='dim_1')

with raises_regex(ValueError, 'xerr, yerr'):
self.darray[:, :, 0].plot.line(y='dim_0', hue='dim_1', xerr=1)

with raises_regex(ValueError, 'cannot'):
self.darray[:, :, 0].plot.line(x='dim_1', y='dim_0', hue='dim_1')

Expand Down Expand Up @@ -391,6 +394,12 @@ def test_slice_in_title(self):
title = plt.gca().get_title()
assert 'd = 10' == title

def test_errorbars(self):
primitive = self.darray.plot(xerr=self.darray / 2,
yerr=self.darray / 2,
ecolor='r')
assert isinstance(primitive, mpl.container.ErrorbarContainer)


class TestPlotHistogram(PlotTestCase):
def setUp(self):
Expand Down