Skip to content

Commit 3743dbc

Browse files
authored
BUG: Fix for xlabel/ylabel in barh plot (#45145)
1 parent 1d57c89 commit 3743dbc

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

doc/source/whatsnew/v1.5.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ Period
230230

231231
Plotting
232232
^^^^^^^^
233-
-
233+
- Bug in :meth:`DataFrame.plot.barh` that prevented labeling the x-axis and ``xlabel`` updating the y-axis label (:issue:`45144`)
234234
-
235235

236236
Groupby/resample/rolling

pandas/plotting/_matplotlib/core.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,10 @@ def _plot(cls, ax: Axes, x, y, style=None, is_errorbar: bool = False, **kwds):
754754
args = (x, y, style) if style is not None else (x, y)
755755
return ax.plot(*args, **kwds)
756756

757+
def _get_custom_index_name(self):
758+
"""Specify whether xlabel/ylabel should be used to override index name"""
759+
return self.xlabel
760+
757761
def _get_index_name(self) -> str | None:
758762
if isinstance(self.data.index, ABCMultiIndex):
759763
name = self.data.index.names
@@ -766,9 +770,10 @@ def _get_index_name(self) -> str | None:
766770
if name is not None:
767771
name = pprint_thing(name)
768772

769-
# GH 9093, override the default xlabel if xlabel is provided.
770-
if self.xlabel is not None:
771-
name = pprint_thing(self.xlabel)
773+
# GH 45145, override the default axis label if one is provided.
774+
index_name = self._get_custom_index_name()
775+
if index_name is not None:
776+
name = pprint_thing(index_name)
772777

773778
return name
774779

@@ -1572,12 +1577,11 @@ def _post_plot_logic(self, ax: Axes, data):
15721577
str_index = [pprint_thing(key) for key in data.index]
15731578
else:
15741579
str_index = [pprint_thing(key) for key in range(data.shape[0])]
1575-
name = self._get_index_name()
15761580

15771581
s_edge = self.ax_pos[0] - 0.25 + self.lim_offset
15781582
e_edge = self.ax_pos[-1] + 0.25 + self.bar_width + self.lim_offset
15791583

1580-
self._decorate_ticks(ax, name, str_index, s_edge, e_edge)
1584+
self._decorate_ticks(ax, self._get_index_name(), str_index, s_edge, e_edge)
15811585

15821586
def _decorate_ticks(self, ax: Axes, name, ticklabels, start_edge, end_edge):
15831587
ax.set_xlim((start_edge, end_edge))
@@ -1608,13 +1612,17 @@ def _plot( # type: ignore[override]
16081612
):
16091613
return ax.barh(x, y, w, left=start, log=log, **kwds)
16101614

1615+
def _get_custom_index_name(self):
1616+
return self.ylabel
1617+
16111618
def _decorate_ticks(self, ax: Axes, name, ticklabels, start_edge, end_edge):
16121619
# horizontal bars
16131620
ax.set_ylim((start_edge, end_edge))
16141621
ax.set_yticks(self.tick_pos)
16151622
ax.set_yticklabels(ticklabels)
16161623
if name is not None and self.use_index:
16171624
ax.set_ylabel(name)
1625+
ax.set_xlabel(self.xlabel)
16181626

16191627

16201628
class PiePlot(MPLPlot):

pandas/tests/plotting/test_series.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -787,16 +787,20 @@ def test_style_single_ok(self):
787787
"index_name, old_label, new_label",
788788
[(None, "", "new"), ("old", "old", "new"), (None, "", "")],
789789
)
790-
@pytest.mark.parametrize("kind", ["line", "area", "bar"])
790+
@pytest.mark.parametrize("kind", ["line", "area", "bar", "barh"])
791791
def test_xlabel_ylabel_series(self, kind, index_name, old_label, new_label):
792792
# GH 9093
793793
ser = Series([1, 2, 3, 4])
794794
ser.index.name = index_name
795795

796-
# default is the ylabel is not shown and xlabel is index name
796+
# default is the ylabel is not shown and xlabel is index name (reverse for barh)
797797
ax = ser.plot(kind=kind)
798-
assert ax.get_ylabel() == ""
799-
assert ax.get_xlabel() == old_label
798+
if kind == "barh":
799+
assert ax.get_xlabel() == ""
800+
assert ax.get_ylabel() == old_label
801+
else:
802+
assert ax.get_ylabel() == ""
803+
assert ax.get_xlabel() == old_label
800804

801805
# old xlabel will be overridden and assigned ylabel will be used as ylabel
802806
ax = ser.plot(kind=kind, ylabel=new_label, xlabel=new_label)

0 commit comments

Comments
 (0)