Skip to content

Commit f7fad77

Browse files
authored
VIS: Add xlabel/ylabel to plot.hist() (#49794)
* VIS: Apply xlabel and ylabel if passed to plot.hist(). * DOC: Indicate versionchanged for xlabel/ylabel docs. * DOC: Add whatsnew entry. * TST: Include histogram in xlabel/ylabel test. * Undo accidental change. * DOC: Fix another typo.
1 parent 22d9ffc commit f7fad77

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

doc/source/whatsnew/v2.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Other enhancements
8383
- :func:`timedelta_range` now supports a ``unit`` keyword ("s", "ms", "us", or "ns") to specify the desired resolution of the output index (:issue:`49824`)
8484
- :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`)
8585
- Added ``name`` parameter to :meth:`IntervalIndex.from_breaks`, :meth:`IntervalIndex.from_arrays` and :meth:`IntervalIndex.from_tuples` (:issue:`48911`)
86+
- :meth:`DataFrame.plot.hist` now recognizes ``xlabel`` and ``ylabel`` arguments (:issue:`49793`)
8687
-
8788

8889
.. ---------------------------------------------------------------------------

pandas/plotting/_core.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,10 @@ class PlotAccessor(PandasObject):
714714
715715
Now applicable to planar plots (`scatter`, `hexbin`).
716716
717+
.. versionchanged:: 2.0.0
718+
719+
Now applicable to histograms.
720+
717721
ylabel : label, optional
718722
Name to use for the ylabel on y-axis. Default will show no ylabel, or the
719723
y-column name for planar plots.
@@ -724,6 +728,10 @@ class PlotAccessor(PandasObject):
724728
725729
Now applicable to planar plots (`scatter`, `hexbin`).
726730
731+
.. versionchanged:: 2.0.0
732+
733+
Now applicable to histograms.
734+
727735
rot : float, default None
728736
Rotation for ticks (xticks for vertical, yticks for horizontal
729737
plots).

pandas/plotting/_matplotlib/hist.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ def __init__(
5959
) -> None:
6060
self.bins = bins # use mpl default
6161
self.bottom = bottom
62+
self.xlabel = kwargs.get("xlabel")
63+
self.ylabel = kwargs.get("ylabel")
6264
# Do not call LinePlot.__init__ which may fill nan
6365
MPLPlot.__init__(self, data, **kwargs) # pylint: disable=non-parent-init-called
6466

@@ -170,9 +172,11 @@ def _make_plot_keywords(self, kwds, y):
170172

171173
def _post_plot_logic(self, ax: Axes, data) -> None:
172174
if self.orientation == "horizontal":
173-
ax.set_xlabel("Frequency")
175+
ax.set_xlabel("Frequency" if self.xlabel is None else self.xlabel)
176+
ax.set_ylabel(self.ylabel)
174177
else:
175-
ax.set_ylabel("Frequency")
178+
ax.set_xlabel(self.xlabel)
179+
ax.set_ylabel("Frequency" if self.ylabel is None else self.ylabel)
176180

177181
@property
178182
def orientation(self) -> PlottingOrientation:

pandas/tests/plotting/test_series.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ def test_style_single_ok(self):
807807
"index_name, old_label, new_label",
808808
[(None, "", "new"), ("old", "old", "new"), (None, "", "")],
809809
)
810-
@pytest.mark.parametrize("kind", ["line", "area", "bar", "barh"])
810+
@pytest.mark.parametrize("kind", ["line", "area", "bar", "barh", "hist"])
811811
def test_xlabel_ylabel_series(self, kind, index_name, old_label, new_label):
812812
# GH 9093
813813
ser = Series([1, 2, 3, 4])
@@ -818,6 +818,9 @@ def test_xlabel_ylabel_series(self, kind, index_name, old_label, new_label):
818818
if kind == "barh":
819819
assert ax.get_xlabel() == ""
820820
assert ax.get_ylabel() == old_label
821+
elif kind == "hist":
822+
assert ax.get_xlabel() == ""
823+
assert ax.get_ylabel() == "Frequency"
821824
else:
822825
assert ax.get_ylabel() == ""
823826
assert ax.get_xlabel() == old_label

0 commit comments

Comments
 (0)