Skip to content

Fix asymmetric error bars for series (closes #9536) #12046

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

Closed
wants to merge 1 commit into from
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
3 changes: 2 additions & 1 deletion doc/source/visualization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1366,9 +1366,10 @@ Horizontal and vertical errorbars can be supplied to the ``xerr`` and ``yerr`` k

- As a :class:`DataFrame` or ``dict`` of errors with column names matching the ``columns`` attribute of the plotting :class:`DataFrame` or matching the ``name`` attribute of the :class:`Series`
- As a ``str`` indicating which of the columns of plotting :class:`DataFrame` contain the error values
- As a single ``number`` which is used as the error for every value
- As raw values (``list``, ``tuple``, or ``np.ndarray``). Must be the same length as the plotting :class:`DataFrame`/:class:`Series`

Asymmetrical error bars are also supported, however raw error values must be provided in this case. For a ``M`` length :class:`Series`, a ``Mx2`` array should be provided indicating lower and upper (or left and right) errors. For a ``MxN`` :class:`DataFrame`, asymmetrical errors should be in a ``Mx2xN`` array.
Asymmetrical error bars are also supported, however raw error values must be provided in this case. For a ``N`` length :class:`Series`, a ``2xN`` array should be provided indicating lower and upper (or left and right) errors. For a ``MxN`` :class:`DataFrame`, asymmetrical errors should be in a ``Mx2xN`` array.

Here is an example of one way to easily plot group means with standard deviations from the raw data.

Expand Down
9 changes: 5 additions & 4 deletions doc/source/whatsnew/v0.18.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ Other Enhancements
- ``pd.read_msgpack()`` now always gives writeable ndarrays even when compression is used (:issue:`12359`).
- ``Index.take`` now handles ``allow_fill`` and ``fill_value`` consistently (:issue:`12631`)

.. ipython:: python
.. ipython:: python
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like these changes slipped in accidentally? Till line 67.


idx = pd.Index([1., 2., 3., 4.], dtype='float')
idx.take([2, -1]) # default, allow_fill=True, fill_value=None
idx.take([2, -1], fill_value=True)
idx = pd.Index([1., 2., 3., 4.], dtype='float')
idx.take([2, -1]) # default, allow_fill=True, fill_value=None
idx.take([2, -1], fill_value=True)

- ``Series.plot`` allows now asymmetric error bars in the shape of 2xN array (:issue:`9536`)

.. _whatsnew_0181.api:

Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/test_graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,20 @@ def test_errorbar_plot(self):
with tm.assertRaises((ValueError, TypeError)):
s.plot(yerr=s_err)

def test_errorbar_asymmetrical(self):
# github issue #9536
s = Series(np.random.randn(5))
err = np.random.rand(2, 5)

ax = _check_plot_works(s.plot, yerr=err, xerr=(err / 2))
self._check_has_errorbars(ax, yerr=1, xerr=1)

assert_allclose(ax.lines[2].get_ydata(), s.values - err[0])
assert_allclose(ax.lines[3].get_ydata(), s.values + err[1])

assert_allclose(ax.lines[0].get_xdata(), s.index - (err[0] / 2))
assert_allclose(ax.lines[1].get_xdata(), s.index + (err[1] / 2))

def test_table(self):
_check_plot_works(self.series.plot, table=True)
_check_plot_works(self.series.plot, table=self.series)
Expand Down
40 changes: 29 additions & 11 deletions pandas/tools/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -1419,10 +1419,17 @@ def _parse_errorbars(self, label, err):
Error bars can be specified in several ways:
Series: the user provides a pandas.Series object of the same
length as the data
ndarray: provides a np.ndarray of the same length as the data
list_like (list/tuple/ndarray/iterator): either a list like of the
Copy link
Contributor

Choose a reason for hiding this comment

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

if you are changing this, then pls provide tests for what you are adding.

same length N as the data has to be provided
or a list like of the shape Mx2xN for asymmetrical error
bars when plotting a DataFrame of shape MxN
or a list like of the shape 2xN for asymmetrical error bars
when plotting a Series.
DataFrame/dict: error values are paired with keys matching the
key in the plotted DataFrame
str: the name of the column within the plotted DataFrame
numeric scalar: the error provided as a number is used for every
data point
'''

if err is None:
Expand Down Expand Up @@ -1458,22 +1465,33 @@ def match_labels(data, e):

elif com.is_list_like(err):
if com.is_iterator(err):
err = np.atleast_2d(list(err))
err = np.asanyarray(list(err))
else:
# raw error values
err = np.atleast_2d(err)
err = np.asanyarray(err)

err_shape = err.shape
if self.nseries == 1 and err.ndim == 2 and len(err) == 2:
Copy link
Contributor

Choose a reason for hiding this comment

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

I would like to see a more general soln here, rather than specific ndim checking.

# asymmetrical errors bars for a series as a 2xN array
err = np.expand_dims(err, 0)
err_shape = err.shape

# asymmetrical error bars
if err.ndim == 3:
if (err_shape[0] != self.nseries) or \
(err_shape[1] != 2) or \
(err_shape[2] != len(self.data)):
if err_shape[2] != len(self.data):
msg = "Asymmetrical error bars should be provided " + \
"with the shape (%u, 2, %u)" % \
(self.nseries, len(self.data))
"with the shape (2, %u)" % (len(self.data))
raise ValueError(msg)
else:
err = np.atleast_2d(err)
err_shape = err.shape

# asymmetrical error bars
if err.ndim == 3:
if (err_shape[0] != self.nseries) or \
(err_shape[1] != 2) or \
(err_shape[2] != len(self.data)):
msg = "Asymmetrical error bars should be provided " + \
"with the shape (%u, 2, %u)" % \
(self.nseries, len(self.data))
raise ValueError(msg)

# broadcast errors to each data series
if len(err) == 1:
Expand Down