Skip to content

VIS: Add xlabel/ylabel to plot.hist() #49794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
-

.. ---------------------------------------------------------------------------
Expand Down
8 changes: 8 additions & 0 deletions pandas/plotting/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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).
Expand Down
8 changes: 6 additions & 2 deletions pandas/plotting/_matplotlib/hist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/plotting/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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
Expand Down