From 0a3c1d733e53be3d4c0e002f558c2fe0c3d647cf Mon Sep 17 00:00:00 2001 From: Souvik Mandal Date: Fri, 27 Dec 2019 21:59:53 +0530 Subject: [PATCH] BUG: The setting xrot=0 in DataFrame.hist() doesn't work with by and subplots #30288 (#30491) --- doc/source/whatsnew/v1.0.0.rst | 1 + pandas/plotting/_matplotlib/hist.py | 3 ++- pandas/tests/plotting/test_hist_method.py | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 284ce8bbecb67..0d0e9d9a54fff 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -828,6 +828,7 @@ Plotting - :func:`set_option` now validates that the plot backend provided to ``'plotting.backend'`` implements the backend when the option is set, rather than when a plot is created (:issue:`28163`) - :meth:`DataFrame.plot` now allow a ``backend`` keyword argument to allow changing between backends in one session (:issue:`28619`). - Bug in color validation incorrectly raising for non-color styles (:issue:`29122`). +- Bug in :meth:`DataFrame.hist`, ``xrot=0`` does not work with ``by`` and subplots (:issue:`30288`). Groupby/resample/rolling ^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/pandas/plotting/_matplotlib/hist.py b/pandas/plotting/_matplotlib/hist.py index 8957389ac2b13..f8b2c7ab123d0 100644 --- a/pandas/plotting/_matplotlib/hist.py +++ b/pandas/plotting/_matplotlib/hist.py @@ -250,7 +250,8 @@ def _grouped_hist( def plot_group(group, ax): ax.hist(group.dropna().values, bins=bins, **kwargs) - xrot = xrot or rot + if xrot is None: + xrot = rot fig, axes = _grouped_plot( plot_group, diff --git a/pandas/tests/plotting/test_hist_method.py b/pandas/tests/plotting/test_hist_method.py index 6c1c7dfd1a4a4..74d48c10ad9a0 100644 --- a/pandas/tests/plotting/test_hist_method.py +++ b/pandas/tests/plotting/test_hist_method.py @@ -253,6 +253,24 @@ def test_tight_layout(self): tm.close() + def test_hist_subplot_xrot(self): + # GH 30288 + df = DataFrame( + { + "length": [1.5, 0.5, 1.2, 0.9, 3], + "animal": ["pig", "rabbit", "pig", "pig", "rabbit"], + } + ) + axes = _check_plot_works( + df.hist, + filterwarnings="always", + column="length", + by="animal", + bins=5, + xrot=0, + ) + self._check_ticks_props(axes, xrot=0) + @td.skip_if_no_mpl class TestDataFrameGroupByPlots(TestPlotBase):