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`) - .. --------------------------------------------------------------------------- diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index 529849e740169..749845f222834 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -720,6 +720,10 @@ class PlotAccessor(PandasObject): Now applicable to planar plots (`scatter`, `hexbin`). + .. versionchanged:: 2.0.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. @@ -730,6 +734,10 @@ class PlotAccessor(PandasObject): 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). 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: 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