Skip to content

BUG: asymmetric error bars for series (GH9536) #34514

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

Merged
merged 30 commits into from
Jul 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
a9f9dc4
BUG: asymmetric error bars for series (GH9536)
Steffen911 Jun 1, 2020
0ea50d3
feedback: verify error message
Steffen911 Jun 1, 2020
4a2eb9c
feedback: explain previous and current behavior
Steffen911 Jun 1, 2020
99b51fc
chore: update escape sequence
Steffen911 Jun 1, 2020
4a277e5
chore: pep8
Steffen911 Jun 1, 2020
bbf9b0c
Merge branch 'master' of https://github.com/pandas-dev/pandas into fi…
Steffen911 Jun 1, 2020
1bb51a7
Merge branch 'master' of https://github.com/pandas-dev/pandas into fi…
Steffen911 Jun 4, 2020
2975406
feedback: verify error bars completely
Steffen911 Jun 7, 2020
98b808b
Merge branch 'master' of https://github.com/pandas-dev/pandas into fi…
Steffen911 Jun 7, 2020
fa4915b
feedback: update tenses and inline changelog
Steffen911 Jun 7, 2020
bca0ffa
Merge branch 'master' of https://github.com/pandas-dev/pandas into fi…
Steffen911 Jun 11, 2020
08d800c
Merge branch 'master' of https://github.com/pandas-dev/pandas into fi…
Steffen911 Jun 12, 2020
4f430b3
feedback: use assert_numpy_array_equal for comparison
Steffen911 Jun 12, 2020
67b534d
feedback: use numpy syntax for expectation construction
Steffen911 Jun 12, 2020
4a02db0
Merge branch 'master' of https://github.com/pandas-dev/pandas into fi…
Steffen911 Jun 22, 2020
76b99fa
Merge branch 'master' of https://github.com/pandas-dev/pandas into fi…
Steffen911 Jun 25, 2020
01bfa7e
feedback: use elif
Steffen911 Jun 26, 2020
fe5d047
Merge branch 'master' of https://github.com/pandas-dev/pandas into fi…
Steffen911 Jun 29, 2020
59520c1
Merge branch 'fix/9536-asymmetric-error-series' of https://github.com…
Steffen911 Jun 29, 2020
5920812
Merge branch 'master' of https://github.com/pandas-dev/pandas into fi…
Steffen911 Jun 30, 2020
7f0c463
Merge branch 'master' of https://github.com/pandas-dev/pandas into fi…
Steffen911 Jul 3, 2020
2222fd2
Merge branch 'master' of https://github.com/pandas-dev/pandas into fi…
Steffen911 Jul 10, 2020
0efd216
Merge branch 'master' of https://github.com/pandas-dev/pandas into fi…
Steffen911 Jul 13, 2020
a6460f7
Merge branch 'master' of https://github.com/pandas-dev/pandas into fi…
Steffen911 Jul 13, 2020
58eea14
Merge branch 'master' of https://github.com/pandas-dev/pandas into fi…
Steffen911 Jul 14, 2020
e5e0ff4
Merge branch 'master' of https://github.com/pandas-dev/pandas into fi…
Steffen911 Jul 15, 2020
b80d363
Merge branch 'master' of https://github.com/pandas-dev/pandas into fi…
Steffen911 Jul 17, 2020
945db8c
Merge branch 'master' of https://github.com/pandas-dev/pandas into fi…
Steffen911 Jul 17, 2020
26ed805
chore: update whatsnew
Steffen911 Jul 17, 2020
e00b9c4
Merge branch 'master' of https://github.com/pandas-dev/pandas into fi…
Steffen911 Jul 17, 2020
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
2 changes: 1 addition & 1 deletion doc/source/user_guide/visualization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1425,7 +1425,7 @@ Horizontal and vertical error bars can be supplied to the ``xerr`` and ``yerr``
* As a ``str`` indicating which of the columns of plotting :class:`DataFrame` contain the error values.
* 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ Other enhancements
- :class:`pandas.core.window.ExponentialMovingWindow` now supports a ``times`` argument that allows ``mean`` to be calculated with observations spaced by the timestamps in ``times`` (:issue:`34839`)
- :meth:`DataFrame.agg` and :meth:`Series.agg` now accept named aggregation for renaming the output columns/indexes. (:issue:`26513`)
- ``compute.use_numba`` now exists as a configuration option that utilizes the numba engine when available (:issue:`33966`)
- :meth:`Series.plot` now supports asymmetric error bars. Previously, if :meth:`Series.plot` received a "2xN" array with error values for `yerr` and/or `xerr`, the left/lower values (first row) were mirrored, while the right/upper values (second row) were ignored. Now, the first row represents the left/lower error values and the second row the right/upper error values. (:issue:`9536`)

.. ---------------------------------------------------------------------------

Expand Down
16 changes: 15 additions & 1 deletion pandas/plotting/_matplotlib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,12 @@ def _parse_errorbars(self, label, err):
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

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.
"""
if err is None:
return None
Expand Down Expand Up @@ -810,7 +816,15 @@ def match_labels(data, e):
err_shape = err.shape

# asymmetrical error bars
if err.ndim == 3:
if isinstance(self.data, ABCSeries) and err_shape[0] == 2:
err = np.expand_dims(err, 0)
err_shape = err.shape
if err_shape[2] != len(self.data):
raise ValueError(
"Asymmetrical error bars should be provided "
f"with the shape (2, {len(self.data)})"
)
elif isinstance(self.data, ABCDataFrame) and err.ndim == 3:
if (
(err_shape[0] != self.nseries)
or (err_shape[1] != 2)
Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/plotting/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,26 @@ def test_dup_datetime_index_plot(self):
s = Series(values, index=index)
_check_plot_works(s.plot)

def test_errorbar_asymmetrical(self):
# GH9536
s = Series(np.arange(10), name="x")
err = np.random.rand(2, 10)

ax = s.plot(yerr=err, xerr=err)

result = np.vstack([i.vertices[:, 1] for i in ax.collections[1].get_paths()])
expected = (err.T * np.array([-1, 1])) + s.to_numpy().reshape(-1, 1)
tm.assert_numpy_array_equal(result, expected)

msg = (
"Asymmetrical error bars should be provided "
f"with the shape \\(2, {len(s)}\\)"
)
with pytest.raises(ValueError, match=msg):
s.plot(yerr=np.random.rand(2, 11))

tm.close()

@pytest.mark.slow
def test_errorbar_plot(self):

Expand Down