From d003019b64a0e3dbef97c036298268b919ecff16 Mon Sep 17 00:00:00 2001 From: Stefanie Molin <24376333+stefmolin@users.noreply.github.com> Date: Sat, 19 Nov 2022 16:51:04 -0500 Subject: [PATCH 1/6] VIS: Apply xlabel and ylabel if passed to plot.hist(). --- pandas/plotting/_matplotlib/hist.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pandas/plotting/_matplotlib/hist.py b/pandas/plotting/_matplotlib/hist.py index 9aad516d308c3..c20495dac5863 100644 --- a/pandas/plotting/_matplotlib/hist.py +++ b/pandas/plotting/_matplotlib/hist.py @@ -59,6 +59,8 @@ def __init__( ) -> None: self.bins = bins # use mpl default self.bottom = bottom + self.xlabel = kwargs.get("xlabel") + self.ylabel = kwargs.get("ylabel") # Do not call LinePlot.__init__ which may fill nan MPLPlot.__init__(self, data, **kwargs) @@ -170,9 +172,11 @@ def _make_plot_keywords(self, kwds, y): def _post_plot_logic(self, ax: Axes, data) -> None: if self.orientation == "horizontal": - ax.set_xlabel("Frequency") + ax.set_xlabel("Frequency" if self.xlabel is None else self.xlabel) + ax.set_ylabel(self.ylabel) else: - ax.set_ylabel("Frequency") + ax.set_xlabel(self.xlabel) + ax.set_ylabel("Frequency" if self.ylabel is None else self.ylabel) @property def orientation(self) -> PlottingOrientation: From 89ebc073d5acf9106475525389b9df9e2d9127ac Mon Sep 17 00:00:00 2001 From: Stefanie Molin <24376333+stefmolin@users.noreply.github.com> Date: Sat, 19 Nov 2022 16:55:06 -0500 Subject: [PATCH 2/6] DOC: Indicate versionchanged for xlabel/ylabel docs. --- pandas/plotting/_core.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index 529849e740169..f123732d82251 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -720,16 +720,24 @@ class PlotAccessor(PandasObject): Now applicable to planar plots (`scatter`, `hexbin`). + .. versionchanged:: 1.6.0 + + Now applicable to histograms. + ylabel : label, optional Name to use for the ylabel on y-axis. Default will show no ylabel, or the y-column name for planar plots. .. versionadded:: 1.1.0 - .. versionchanged:: 1.2.0 + .. versionchanged:: 2.0.0 Now applicable to planar plots (`scatter`, `hexbin`). + .. versionchanged:: 2.0.0 + + Now applicable to histograms. + rot : float, default None Rotation for ticks (xticks for vertical, yticks for horizontal plots). From 8527b1e65125385b84f7baaab0252f026c6ba456 Mon Sep 17 00:00:00 2001 From: Stefanie Molin <24376333+stefmolin@users.noreply.github.com> Date: Sat, 19 Nov 2022 17:05:06 -0500 Subject: [PATCH 3/6] DOC: Add whatsnew entry. --- doc/source/whatsnew/v2.0.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 0e6d1029d352b..41a141ae65981 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -64,6 +64,7 @@ Other enhancements - :func:`date_range` now supports a ``unit`` keyword ("s", "ms", "us", or "ns") to specify the desired resolution of the output index (:issue:`49106`) - :meth:`DataFrame.to_json` now supports a ``mode`` keyword with supported inputs 'w' and 'a'. Defaulting to 'w', 'a' can be used when lines=True and orient='records' to append record oriented json lines to an existing json file. (:issue:`35849`) - Added ``name`` parameter to :meth:`IntervalIndex.from_breaks`, :meth:`IntervalIndex.from_arrays` and :meth:`IntervalIndex.from_tuples` (:issue:`48911`) +- :meth:`DataFrame.plot.hist` now recognizes ``xlabel`` and ``ylabel`` arguments (:issue:`49793`) - .. --------------------------------------------------------------------------- From 896457d7b0b925cafc8dbba707d789625c0e7514 Mon Sep 17 00:00:00 2001 From: Stefanie Molin <24376333+stefmolin@users.noreply.github.com> Date: Sat, 19 Nov 2022 17:07:27 -0500 Subject: [PATCH 4/6] TST: Include histogram in xlabel/ylabel test. --- pandas/tests/plotting/test_series.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pandas/tests/plotting/test_series.py b/pandas/tests/plotting/test_series.py index d9505b4d593e6..3a9aa91002730 100644 --- a/pandas/tests/plotting/test_series.py +++ b/pandas/tests/plotting/test_series.py @@ -807,7 +807,7 @@ def test_style_single_ok(self): "index_name, old_label, new_label", [(None, "", "new"), ("old", "old", "new"), (None, "", "")], ) - @pytest.mark.parametrize("kind", ["line", "area", "bar", "barh"]) + @pytest.mark.parametrize("kind", ["line", "area", "bar", "barh", "hist"]) def test_xlabel_ylabel_series(self, kind, index_name, old_label, new_label): # GH 9093 ser = Series([1, 2, 3, 4]) @@ -818,6 +818,9 @@ def test_xlabel_ylabel_series(self, kind, index_name, old_label, new_label): if kind == "barh": assert ax.get_xlabel() == "" assert ax.get_ylabel() == old_label + elif kind == "hist": + assert ax.get_xlabel() == "" + assert ax.get_ylabel() == "Frequency" else: assert ax.get_ylabel() == "" assert ax.get_xlabel() == old_label From c8a81abeebe32fd2f7b5ae8518cbaab9e057f0f4 Mon Sep 17 00:00:00 2001 From: Stefanie Molin <24376333+stefmolin@users.noreply.github.com> Date: Tue, 22 Nov 2022 20:50:32 -0500 Subject: [PATCH 5/6] Undo accidental change. --- pandas/plotting/_core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index f123732d82251..22cf2a6d7c2ca 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -730,7 +730,7 @@ class PlotAccessor(PandasObject): .. versionadded:: 1.1.0 - .. versionchanged:: 2.0.0 + .. versionchanged:: 1.2.0 Now applicable to planar plots (`scatter`, `hexbin`). From 4fc38405b76ea49a710f76832bdb31b8f7c838fe Mon Sep 17 00:00:00 2001 From: Stefanie Molin <24376333+stefmolin@users.noreply.github.com> Date: Tue, 22 Nov 2022 20:52:07 -0500 Subject: [PATCH 6/6] DOC: Fix another typo. --- pandas/plotting/_core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index 22cf2a6d7c2ca..749845f222834 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -720,7 +720,7 @@ class PlotAccessor(PandasObject): Now applicable to planar plots (`scatter`, `hexbin`). - .. versionchanged:: 1.6.0 + .. versionchanged:: 2.0.0 Now applicable to histograms.