From bfa923e491162176046a923712c5a73de489779a Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sun, 17 Jan 2021 18:36:51 +0000 Subject: [PATCH 1/9] Revert "Inconsistent indexes for tick label plotting (#28733)" This reverts commit fb379d8266492f917ed880f7619f3d0d9bc7c8db. --- doc/source/whatsnew/v1.2.0.rst | 1 - pandas/plotting/_matplotlib/core.py | 25 ++------ pandas/tests/plotting/frame/test_frame.py | 74 ----------------------- 3 files changed, 4 insertions(+), 96 deletions(-) diff --git a/doc/source/whatsnew/v1.2.0.rst b/doc/source/whatsnew/v1.2.0.rst index 8e9361125513b..bf92c0c97c854 100644 --- a/doc/source/whatsnew/v1.2.0.rst +++ b/doc/source/whatsnew/v1.2.0.rst @@ -751,7 +751,6 @@ Plotting - Bug in :meth:`DataFrame.plot` was rotating xticklabels when ``subplots=True``, even if the x-axis wasn't an irregular time series (:issue:`29460`) - Bug in :meth:`DataFrame.plot` where a marker letter in the ``style`` keyword sometimes caused a ``ValueError`` (:issue:`21003`) -- Bug in :meth:`DataFrame.plot.bar` and :meth:`Series.plot.bar` where ticks positions were assigned by value order instead of using the actual value for numeric or a smart ordering for string (:issue:`26186`, :issue:`11465`) - Twinned axes were losing their tick labels which should only happen to all but the last row or column of 'externally' shared axes (:issue:`33819`) - Bug in :meth:`Series.plot` and :meth:`DataFrame.plot` was throwing a :exc:`ValueError` when the Series or DataFrame was indexed by a :class:`.TimedeltaIndex` with a fixed frequency and the x-axis lower limit was greater than the upper limit (:issue:`37454`) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index aa8e0665a7310..15e30bd7fc257 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -1369,6 +1369,7 @@ def __init__(self, data, **kwargs): self.bar_width = kwargs.pop("width", 0.5) pos = kwargs.pop("position", 0.5) kwargs.setdefault("align", "center") + self.tick_pos = np.arange(len(data)) self.bottom = kwargs.pop("bottom", 0) self.left = kwargs.pop("left", 0) @@ -1391,16 +1392,7 @@ def __init__(self, data, **kwargs): self.tickoffset = self.bar_width * pos self.lim_offset = 0 - if isinstance(self.data.index, ABCMultiIndex): - if kwargs["ax"] is not None and kwargs["ax"].has_data(): - warnings.warn( - "Redrawing a bar plot with a MultiIndex is not supported " - + "and may lead to inconsistent label positions.", - UserWarning, - ) - self.ax_index = np.arange(len(data)) - else: - self.ax_index = self.data.index + self.ax_pos = self.tick_pos - self.tickoffset def _args_adjust(self): if is_list_like(self.bottom): @@ -1427,15 +1419,6 @@ def _make_plot(self): for i, (label, y) in enumerate(self._iter_data(fillna=0)): ax = self._get_ax(i) - - if self.orientation == "vertical": - ax.xaxis.update_units(self.ax_index) - self.tick_pos = ax.convert_xunits(self.ax_index).astype(np.int) - elif self.orientation == "horizontal": - ax.yaxis.update_units(self.ax_index) - self.tick_pos = ax.convert_yunits(self.ax_index).astype(np.int) - self.ax_pos = self.tick_pos - self.tickoffset - kwds = self.kwds.copy() if self._is_series: kwds["color"] = colors @@ -1507,8 +1490,8 @@ def _post_plot_logic(self, ax: "Axes", data): str_index = [pprint_thing(key) for key in range(data.shape[0])] name = self._get_index_name() - s_edge = self.ax_pos.min() - 0.25 + self.lim_offset - e_edge = self.ax_pos.max() + 0.25 + self.bar_width + self.lim_offset + s_edge = self.ax_pos[0] - 0.25 + self.lim_offset + e_edge = self.ax_pos[-1] + 0.25 + self.bar_width + self.lim_offset self._decorate_ticks(ax, name, str_index, s_edge, e_edge) diff --git a/pandas/tests/plotting/frame/test_frame.py b/pandas/tests/plotting/frame/test_frame.py index a6aa6c02d1a79..d25741a0a9fae 100644 --- a/pandas/tests/plotting/frame/test_frame.py +++ b/pandas/tests/plotting/frame/test_frame.py @@ -2191,80 +2191,6 @@ def test_xlabel_ylabel_dataframe_plane_plot(self, kind, xlabel, ylabel): assert ax.get_xlabel() == (xcol if xlabel is None else xlabel) assert ax.get_ylabel() == (ycol if ylabel is None else ylabel) - @pytest.mark.parametrize("method", ["bar", "barh"]) - def test_bar_ticklabel_consistence(self, method): - # Draw two consecutiv bar plot with consistent ticklabels - # The labels positions should not move between two drawing on the same axis - # GH: 26186 - def get_main_axis(ax): - if method == "barh": - return ax.yaxis - elif method == "bar": - return ax.xaxis - - # Plot the first bar plot - data = {"A": 0, "B": 3, "C": -4} - df = DataFrame.from_dict(data, orient="index", columns=["Value"]) - ax = getattr(df.plot, method)() - ax.get_figure().canvas.draw() - - # Retrieve the label positions for the first drawing - xticklabels = [t.get_text() for t in get_main_axis(ax).get_ticklabels()] - label_positions_1 = dict(zip(xticklabels, get_main_axis(ax).get_ticklocs())) - - # Modify the dataframe order and values and plot on same axis - df = df.sort_values("Value") * -2 - ax = getattr(df.plot, method)(ax=ax, color="red") - ax.get_figure().canvas.draw() - - # Retrieve the label positions for the second drawing - xticklabels = [t.get_text() for t in get_main_axis(ax).get_ticklabels()] - label_positions_2 = dict(zip(xticklabels, get_main_axis(ax).get_ticklocs())) - - # Assert that the label positions did not change between the plotting - assert label_positions_1 == label_positions_2 - - def test_bar_numeric(self): - # Bar plot with numeric index have tick location values equal to index - # values - # GH: 11465 - df = DataFrame(np.random.rand(10), index=np.arange(10, 20)) - ax = df.plot.bar() - ticklocs = ax.xaxis.get_ticklocs() - expected = np.arange(10, 20, dtype=np.int64) - tm.assert_numpy_array_equal(ticklocs, expected) - - def test_bar_multiindex(self): - # Test from pandas/doc/source/user_guide/visualization.rst - # at section Plotting With Error Bars - # Related to issue GH: 26186 - - ix3 = pd.MultiIndex.from_arrays( - [ - ["a", "a", "a", "a", "b", "b", "b", "b"], - ["foo", "foo", "bar", "bar", "foo", "foo", "bar", "bar"], - ], - names=["letter", "word"], - ) - - df3 = DataFrame( - {"data1": [3, 2, 4, 3, 2, 4, 3, 2], "data2": [6, 5, 7, 5, 4, 5, 6, 5]}, - index=ix3, - ) - - # Group by index labels and take the means and standard deviations - # for each group - gp3 = df3.groupby(level=("letter", "word")) - means = gp3.mean() - errors = gp3.std() - - # No assertion we just ensure that we can plot a MultiIndex bar plot - # and are getting a UserWarning if redrawing - with tm.assert_produces_warning(None): - ax = means.plot.bar(yerr=errors, capsize=4) - with tm.assert_produces_warning(UserWarning): - means.plot.bar(yerr=errors, capsize=4, ax=ax) - def _generate_4_axes_via_gridspec(): import matplotlib as mpl From f408acf1f9d238b09d84d2e29c6bbd2f343196ed Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sun, 17 Jan 2021 19:29:24 +0000 Subject: [PATCH 2/9] reinstate 1.2.0 release note --- doc/source/whatsnew/v1.2.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.2.0.rst b/doc/source/whatsnew/v1.2.0.rst index bf92c0c97c854..8e9361125513b 100644 --- a/doc/source/whatsnew/v1.2.0.rst +++ b/doc/source/whatsnew/v1.2.0.rst @@ -751,6 +751,7 @@ Plotting - Bug in :meth:`DataFrame.plot` was rotating xticklabels when ``subplots=True``, even if the x-axis wasn't an irregular time series (:issue:`29460`) - Bug in :meth:`DataFrame.plot` where a marker letter in the ``style`` keyword sometimes caused a ``ValueError`` (:issue:`21003`) +- Bug in :meth:`DataFrame.plot.bar` and :meth:`Series.plot.bar` where ticks positions were assigned by value order instead of using the actual value for numeric or a smart ordering for string (:issue:`26186`, :issue:`11465`) - Twinned axes were losing their tick labels which should only happen to all but the last row or column of 'externally' shared axes (:issue:`33819`) - Bug in :meth:`Series.plot` and :meth:`DataFrame.plot` was throwing a :exc:`ValueError` when the Series or DataFrame was indexed by a :class:`.TimedeltaIndex` with a fixed frequency and the x-axis lower limit was greater than the upper limit (:issue:`37454`) From ddfd48ffe7b45e42cf88a8b975253348cf863c8f Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sun, 17 Jan 2021 19:38:21 +0000 Subject: [PATCH 3/9] add 1.2.1 release note --- doc/source/whatsnew/v1.2.1.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/doc/source/whatsnew/v1.2.1.rst b/doc/source/whatsnew/v1.2.1.rst index 411eeed1714f1..7effa3f9dd0ae 100644 --- a/doc/source/whatsnew/v1.2.1.rst +++ b/doc/source/whatsnew/v1.2.1.rst @@ -12,6 +12,10 @@ including other versions of pandas. .. _whatsnew_121.regressions: +We have reverted a commit that resulted in several plotting related regressions in pandas 1.2 (:issue:`38969`, :issue:`38736`, :issue:`38865`, :issue:`38947` and :issue:`39126`) + +As a result, bugs reported as fixed in pandas 1.2 are again present (:issue:`26186` and :issue:`11465`) + Fixed regressions ~~~~~~~~~~~~~~~~~ - Fixed regression in :meth:`to_csv` that created corrupted zip files when there were more rows than ``chunksize`` (:issue:`38714`) From e9637b35c2df5c1164ec8ce7a42ee7a6afd1192a Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Mon, 18 Jan 2021 10:11:28 +0000 Subject: [PATCH 4/9] Update doc/source/whatsnew/v1.2.1.rst Co-authored-by: Joris Van den Bossche --- doc/source/whatsnew/v1.2.1.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.2.1.rst b/doc/source/whatsnew/v1.2.1.rst index 7effa3f9dd0ae..60febc783d554 100644 --- a/doc/source/whatsnew/v1.2.1.rst +++ b/doc/source/whatsnew/v1.2.1.rst @@ -14,7 +14,7 @@ including other versions of pandas. We have reverted a commit that resulted in several plotting related regressions in pandas 1.2 (:issue:`38969`, :issue:`38736`, :issue:`38865`, :issue:`38947` and :issue:`39126`) -As a result, bugs reported as fixed in pandas 1.2 are again present (:issue:`26186` and :issue:`11465`) +As a result, bugs reported as fixed in pandas 1.2 related to inconsistent tick labeling in bar plots are again present (:issue:`26186` and :issue:`11465`) Fixed regressions ~~~~~~~~~~~~~~~~~ From 3ab87aabe712cb3eb5fd68dac11305d6484d5e0b Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Mon, 18 Jan 2021 10:14:56 +0000 Subject: [PATCH 5/9] move note below the list of regressions --- doc/source/whatsnew/v1.2.1.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v1.2.1.rst b/doc/source/whatsnew/v1.2.1.rst index 60febc783d554..9308bae3dd5b5 100644 --- a/doc/source/whatsnew/v1.2.1.rst +++ b/doc/source/whatsnew/v1.2.1.rst @@ -12,10 +12,6 @@ including other versions of pandas. .. _whatsnew_121.regressions: -We have reverted a commit that resulted in several plotting related regressions in pandas 1.2 (:issue:`38969`, :issue:`38736`, :issue:`38865`, :issue:`38947` and :issue:`39126`) - -As a result, bugs reported as fixed in pandas 1.2 related to inconsistent tick labeling in bar plots are again present (:issue:`26186` and :issue:`11465`) - Fixed regressions ~~~~~~~~~~~~~~~~~ - Fixed regression in :meth:`to_csv` that created corrupted zip files when there were more rows than ``chunksize`` (:issue:`38714`) @@ -39,6 +35,10 @@ Fixed regressions - Fixed regression in comparisons between ``NaT`` and ``datetime.date`` objects incorrectly returning ``True`` (:issue:`39151`) - Fixed regression in :func:`pandas.testing.assert_index_equal` raising ``TypeError`` with ``check_order=False`` when :class:`Index` has mixed dtype (:issue:`39168`) +We have reverted a commit that resulted in several plotting related regressions in pandas 1.2 (:issue:`38969`, :issue:`38736`, :issue:`38865`, :issue:`38947` and :issue:`39126`) + +As a result, bugs reported as fixed in pandas 1.2 related to inconsistent tick labeling in bar plots are again present (:issue:`26186` and :issue:`11465`) + .. --------------------------------------------------------------------------- .. _whatsnew_121.bug_fixes: From ca5749d8093ca32adaf6d3df3ddfaa56cd3b6c1b Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Mon, 18 Jan 2021 10:17:01 +0000 Subject: [PATCH 6/9] update 1.2.0 whatsnew --- doc/source/whatsnew/v1.2.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.2.0.rst b/doc/source/whatsnew/v1.2.0.rst index 8e9361125513b..a6af07293aee6 100644 --- a/doc/source/whatsnew/v1.2.0.rst +++ b/doc/source/whatsnew/v1.2.0.rst @@ -750,7 +750,7 @@ Plotting ^^^^^^^^ - Bug in :meth:`DataFrame.plot` was rotating xticklabels when ``subplots=True``, even if the x-axis wasn't an irregular time series (:issue:`29460`) -- Bug in :meth:`DataFrame.plot` where a marker letter in the ``style`` keyword sometimes caused a ``ValueError`` (:issue:`21003`) +- Bug in :meth:`DataFrame.plot` where a marker letter in the ``style`` keyword sometimes caused a ``ValueError`` (:issue:`21003`). This fix has been reverted in pandas 1.2.1, see :doc:`v1.2.1` - Bug in :meth:`DataFrame.plot.bar` and :meth:`Series.plot.bar` where ticks positions were assigned by value order instead of using the actual value for numeric or a smart ordering for string (:issue:`26186`, :issue:`11465`) - Twinned axes were losing their tick labels which should only happen to all but the last row or column of 'externally' shared axes (:issue:`33819`) - Bug in :meth:`Series.plot` and :meth:`DataFrame.plot` was throwing a :exc:`ValueError` when the Series or DataFrame was From 182d6878e53cdcfb3aa43bafcb505e52e21ecb9b Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Mon, 18 Jan 2021 10:20:19 +0000 Subject: [PATCH 7/9] wrong entry --- doc/source/whatsnew/v1.2.0.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.2.0.rst b/doc/source/whatsnew/v1.2.0.rst index a6af07293aee6..95757448a7978 100644 --- a/doc/source/whatsnew/v1.2.0.rst +++ b/doc/source/whatsnew/v1.2.0.rst @@ -750,8 +750,8 @@ Plotting ^^^^^^^^ - Bug in :meth:`DataFrame.plot` was rotating xticklabels when ``subplots=True``, even if the x-axis wasn't an irregular time series (:issue:`29460`) -- Bug in :meth:`DataFrame.plot` where a marker letter in the ``style`` keyword sometimes caused a ``ValueError`` (:issue:`21003`). This fix has been reverted in pandas 1.2.1, see :doc:`v1.2.1` -- Bug in :meth:`DataFrame.plot.bar` and :meth:`Series.plot.bar` where ticks positions were assigned by value order instead of using the actual value for numeric or a smart ordering for string (:issue:`26186`, :issue:`11465`) +- Bug in :meth:`DataFrame.plot` where a marker letter in the ``style`` keyword sometimes caused a ``ValueError`` (:issue:`21003`) +- Bug in :meth:`DataFrame.plot.bar` and :meth:`Series.plot.bar` where ticks positions were assigned by value order instead of using the actual value for numeric or a smart ordering for string (:issue:`26186`, :issue:`11465`). This fix has been reverted in pandas 1.2.1, see :doc:`v1.2.1` - Twinned axes were losing their tick labels which should only happen to all but the last row or column of 'externally' shared axes (:issue:`33819`) - Bug in :meth:`Series.plot` and :meth:`DataFrame.plot` was throwing a :exc:`ValueError` when the Series or DataFrame was indexed by a :class:`.TimedeltaIndex` with a fixed frequency and the x-axis lower limit was greater than the upper limit (:issue:`37454`) From 45be5a4d1568036aa5346392934a9725711e3abd Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Mon, 18 Jan 2021 10:27:18 +0000 Subject: [PATCH 8/9] Update doc/source/whatsnew/v1.2.1.rst Co-authored-by: Joris Van den Bossche --- doc/source/whatsnew/v1.2.1.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.2.1.rst b/doc/source/whatsnew/v1.2.1.rst index 9308bae3dd5b5..ba3433fc2cd6d 100644 --- a/doc/source/whatsnew/v1.2.1.rst +++ b/doc/source/whatsnew/v1.2.1.rst @@ -35,8 +35,7 @@ Fixed regressions - Fixed regression in comparisons between ``NaT`` and ``datetime.date`` objects incorrectly returning ``True`` (:issue:`39151`) - Fixed regression in :func:`pandas.testing.assert_index_equal` raising ``TypeError`` with ``check_order=False`` when :class:`Index` has mixed dtype (:issue:`39168`) -We have reverted a commit that resulted in several plotting related regressions in pandas 1.2 (:issue:`38969`, :issue:`38736`, :issue:`38865`, :issue:`38947` and :issue:`39126`) - +We have reverted a commit that resulted in several plotting related regressions in pandas 1.2 (:issue:`38969`, :issue:`38736`, :issue:`38865`, :issue:`38947` and :issue:`39126`). As a result, bugs reported as fixed in pandas 1.2 related to inconsistent tick labeling in bar plots are again present (:issue:`26186` and :issue:`11465`) .. --------------------------------------------------------------------------- From 03f41b8eafb415bc189ec430b1c22f6ba41e6cae Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Mon, 18 Jan 2021 11:09:20 +0000 Subject: [PATCH 9/9] Update doc/source/whatsnew/v1.2.1.rst Co-authored-by: Joris Van den Bossche --- doc/source/whatsnew/v1.2.1.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.2.1.rst b/doc/source/whatsnew/v1.2.1.rst index ba3433fc2cd6d..20358a5767526 100644 --- a/doc/source/whatsnew/v1.2.1.rst +++ b/doc/source/whatsnew/v1.2.1.rst @@ -35,8 +35,8 @@ Fixed regressions - Fixed regression in comparisons between ``NaT`` and ``datetime.date`` objects incorrectly returning ``True`` (:issue:`39151`) - Fixed regression in :func:`pandas.testing.assert_index_equal` raising ``TypeError`` with ``check_order=False`` when :class:`Index` has mixed dtype (:issue:`39168`) -We have reverted a commit that resulted in several plotting related regressions in pandas 1.2 (:issue:`38969`, :issue:`38736`, :issue:`38865`, :issue:`38947` and :issue:`39126`). -As a result, bugs reported as fixed in pandas 1.2 related to inconsistent tick labeling in bar plots are again present (:issue:`26186` and :issue:`11465`) +We have reverted a commit that resulted in several plotting related regressions in pandas 1.2.0 (:issue:`38969`, :issue:`38736`, :issue:`38865`, :issue:`38947` and :issue:`39126`). +As a result, bugs reported as fixed in pandas 1.2.0 related to inconsistent tick labeling in bar plots are again present (:issue:`26186` and :issue:`11465`) .. ---------------------------------------------------------------------------