From 2bcf9209af8aad69606879ab53983df08a7ca547 Mon Sep 17 00:00:00 2001 From: GiuPassarelli Date: Tue, 27 Aug 2019 09:46:20 -0300 Subject: [PATCH 1/4] Fixing issue #14119 --- doc/source/whatsnew/v0.25.2.rst | 2 +- pandas/plotting/_matplotlib/core.py | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v0.25.2.rst b/doc/source/whatsnew/v0.25.2.rst index 6974c7521a237..4f49f7d083520 100644 --- a/doc/source/whatsnew/v0.25.2.rst +++ b/doc/source/whatsnew/v0.25.2.rst @@ -69,7 +69,7 @@ I/O Plotting ^^^^^^^^ -- +- Fixed xticks in :core.py: `BarPlot._post_plot_logic` when setting a custom xticks - - diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 6ff3f28440303..81e9a488b6f15 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -1310,6 +1310,7 @@ def __init__(self, data, **kwargs): pos = kwargs.pop("position", 0.5) kwargs.setdefault("align", "center") self.tick_pos = np.arange(len(data)) + self.custom_tick_pos = kwargs.pop("xticks", None) self.bottom = kwargs.pop("bottom", 0) self.left = kwargs.pop("left", 0) @@ -1435,8 +1436,13 @@ def _post_plot_logic(self, ax, data): def _decorate_ticks(self, ax, name, ticklabels, start_edge, end_edge): ax.set_xlim((start_edge, end_edge)) - ax.set_xticks(self.tick_pos) - ax.set_xticklabels(ticklabels) + + if self.custom_tick_pos is not None: + ax.set_xticks(np.array(self.custom_tick_pos)) + else: + ax.set_xticks(self.tick_pos) + ax.set_xticklabels(ticklabels) + if name is not None and self.use_index: ax.set_xlabel(name) From a6d791cd12c64ae04a9b3d9dcda7705a1468dcc1 Mon Sep 17 00:00:00 2001 From: GiuPassarelli Date: Thu, 29 Aug 2019 10:23:39 -0300 Subject: [PATCH 2/4] Removing variable custom_tick_pos and adding test --- doc/source/whatsnew/v0.25.2.rst | 2 +- doc/source/whatsnew/v1.0.0.rst | 1 + pandas/plotting/_matplotlib/core.py | 5 ++--- pandas/tests/plotting/test_series.py | 7 +++++++ 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v0.25.2.rst b/doc/source/whatsnew/v0.25.2.rst index 4f49f7d083520..6974c7521a237 100644 --- a/doc/source/whatsnew/v0.25.2.rst +++ b/doc/source/whatsnew/v0.25.2.rst @@ -69,7 +69,7 @@ I/O Plotting ^^^^^^^^ -- Fixed xticks in :core.py: `BarPlot._post_plot_logic` when setting a custom xticks +- - - diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 7fe358d3820f2..08d756ce65552 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -171,6 +171,7 @@ Plotting - Bug in :meth:`DataFrame.plot` producing incorrect legend markers when plotting multiple series on the same axis (:issue:`18222`) - Bug in :meth:`DataFrame.plot` when ``kind='box'`` and data contains datetime or timedelta data. These types are now automatically dropped (:issue:`22799`) - Bug in :meth:`DataFrame.plot.line` and :meth:`DataFrame.plot.area` produce wrong xlim in x-axis (:issue:`27686`, :issue:`25160`, :issue:`24784`) +- Bug in :core.py:`BarPlot._decorate_ticks` xticks argument has no effect in bar plot (:issue:`14119`) Groupby/resample/rolling ^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 81e9a488b6f15..d03ff57811a49 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -1310,7 +1310,6 @@ def __init__(self, data, **kwargs): pos = kwargs.pop("position", 0.5) kwargs.setdefault("align", "center") self.tick_pos = np.arange(len(data)) - self.custom_tick_pos = kwargs.pop("xticks", None) self.bottom = kwargs.pop("bottom", 0) self.left = kwargs.pop("left", 0) @@ -1437,8 +1436,8 @@ def _post_plot_logic(self, ax, data): def _decorate_ticks(self, ax, name, ticklabels, start_edge, end_edge): ax.set_xlim((start_edge, end_edge)) - if self.custom_tick_pos is not None: - ax.set_xticks(np.array(self.custom_tick_pos)) + if self.xticks is not None: + ax.set_xticks(np.array(self.xticks)) else: ax.set_xticks(self.tick_pos) ax.set_xticklabels(ticklabels) diff --git a/pandas/tests/plotting/test_series.py b/pandas/tests/plotting/test_series.py index 2c4c8aa7461a3..950409d6715eb 100644 --- a/pandas/tests/plotting/test_series.py +++ b/pandas/tests/plotting/test_series.py @@ -871,6 +871,13 @@ def test_xticklabels(self): exp = ["P{i:02d}".format(i=i) for i in [0, 3, 5, 9]] self._check_text_labels(ax.get_xticklabels(), exp) + def test_xtick_barPlot(self): + # GH28172 + s = pd.Series(range(10), index=["P{i:02d}".format(i=i) for i in range(10)]) + ax = s.plot.bar(xticks=range(0, 11, 2)) + exp = np.array(list(range(0, 11, 2))) + assert (exp == ax.get_xticks()).all() + def test_custom_business_day_freq(self): # GH7222 from pandas.tseries.offsets import CustomBusinessDay From 7d001e6f24c734ab0c611646f74cef081f665643 Mon Sep 17 00:00:00 2001 From: GiuPassarelli Date: Tue, 3 Sep 2019 09:53:02 -0300 Subject: [PATCH 3/4] Update doc/source/whatsnew/v1.0.0.rst Co-Authored-By: Tom Augspurger --- doc/source/whatsnew/v1.0.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 08d756ce65552..9e996db0da9fb 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -171,7 +171,7 @@ Plotting - Bug in :meth:`DataFrame.plot` producing incorrect legend markers when plotting multiple series on the same axis (:issue:`18222`) - Bug in :meth:`DataFrame.plot` when ``kind='box'`` and data contains datetime or timedelta data. These types are now automatically dropped (:issue:`22799`) - Bug in :meth:`DataFrame.plot.line` and :meth:`DataFrame.plot.area` produce wrong xlim in x-axis (:issue:`27686`, :issue:`25160`, :issue:`24784`) -- Bug in :core.py:`BarPlot._decorate_ticks` xticks argument has no effect in bar plot (:issue:`14119`) +- Bug in the ``xticks`` argument being ignored for :meth:`DataFrame.plot.bar` (:issue:`14119`) Groupby/resample/rolling ^^^^^^^^^^^^^^^^^^^^^^^^ From ebc5c3ee1978a065ffaccd9886b873d7b5386d7d Mon Sep 17 00:00:00 2001 From: GiuPassarelli Date: Tue, 3 Sep 2019 09:56:33 -0300 Subject: [PATCH 4/4] Fixing test --- pandas/tests/plotting/test_series.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/plotting/test_series.py b/pandas/tests/plotting/test_series.py index 950409d6715eb..add4de4476ee3 100644 --- a/pandas/tests/plotting/test_series.py +++ b/pandas/tests/plotting/test_series.py @@ -876,7 +876,7 @@ def test_xtick_barPlot(self): s = pd.Series(range(10), index=["P{i:02d}".format(i=i) for i in range(10)]) ax = s.plot.bar(xticks=range(0, 11, 2)) exp = np.array(list(range(0, 11, 2))) - assert (exp == ax.get_xticks()).all() + tm.assert_numpy_array_equal(exp, ax.get_xticks()) def test_custom_business_day_freq(self): # GH7222