Skip to content

Commit e4e1b63

Browse files
authored
VIS: Accept xlabel and ylabel for scatter and hexbin plots (#37102)
* VIS: Accept xlabel and ylabel for scatter and hexbin plots * Add issue number note
1 parent c0c3516 commit e4e1b63

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ Other enhancements
193193
- Where possible :meth:`RangeIndex.difference` and :meth:`RangeIndex.symmetric_difference` will return :class:`RangeIndex` instead of :class:`Int64Index` (:issue:`36564`)
194194
- Added :meth:`Rolling.sem()` and :meth:`Expanding.sem()` to compute the standard error of mean (:issue:`26476`).
195195
- :meth:`Rolling.var()` and :meth:`Rolling.std()` use Kahan summation and Welfords Method to avoid numerical issues (:issue:`37051`)
196+
- :meth:`DataFrame.plot` now recognizes ``xlabel`` and ``ylabel`` arguments for plots of type ``scatter`` and ``hexbin`` (:issue:`37001`)
196197

197198
.. _whatsnew_120.api_breaking.python:
198199

pandas/plotting/_core.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -678,15 +678,25 @@ class PlotAccessor(PandasObject):
678678
ylim : 2-tuple/list
679679
Set the y limits of the current axes.
680680
xlabel : label, optional
681-
Name to use for the xlabel on x-axis. Default uses index name as xlabel.
681+
Name to use for the xlabel on x-axis. Default uses index name as xlabel, or the
682+
x-column name for planar plots.
682683
683684
.. versionadded:: 1.1.0
684685
686+
.. versionchanged:: 1.2.0
687+
688+
Now applicable to planar plots (`scatter`, `hexbin`).
689+
685690
ylabel : label, optional
686-
Name to use for the ylabel on y-axis. Default will show no ylabel.
691+
Name to use for the ylabel on y-axis. Default will show no ylabel, or the
692+
y-column name for planar plots.
687693
688694
.. versionadded:: 1.1.0
689695
696+
.. versionchanged:: 1.2.0
697+
698+
Now applicable to planar plots (`scatter`, `hexbin`).
699+
690700
rot : int, default None
691701
Rotation for ticks (xticks for vertical, yticks for horizontal
692702
plots).

pandas/plotting/_matplotlib/core.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -924,8 +924,10 @@ def nseries(self) -> int:
924924

925925
def _post_plot_logic(self, ax: "Axes", data):
926926
x, y = self.x, self.y
927-
ax.set_ylabel(pprint_thing(y))
928-
ax.set_xlabel(pprint_thing(x))
927+
xlabel = self.xlabel if self.xlabel is not None else pprint_thing(x)
928+
ylabel = self.ylabel if self.ylabel is not None else pprint_thing(y)
929+
ax.set_xlabel(xlabel)
930+
ax.set_ylabel(ylabel)
929931

930932
def _plot_colorbar(self, ax: "Axes", **kwds):
931933
# Addresses issues #10611 and #10678:

pandas/tests/plotting/test_frame.py

+21
Original file line numberDiff line numberDiff line change
@@ -3449,6 +3449,27 @@ def test_xlabel_ylabel_dataframe_single_plot(
34493449
assert ax.get_ylabel() == str(new_label)
34503450
assert ax.get_xlabel() == str(new_label)
34513451

3452+
@pytest.mark.parametrize(
3453+
"xlabel, ylabel",
3454+
[
3455+
(None, None),
3456+
("X Label", None),
3457+
(None, "Y Label"),
3458+
("X Label", "Y Label"),
3459+
],
3460+
)
3461+
@pytest.mark.parametrize("kind", ["scatter", "hexbin"])
3462+
def test_xlabel_ylabel_dataframe_plane_plot(self, kind, xlabel, ylabel):
3463+
# GH 37001
3464+
xcol = "Type A"
3465+
ycol = "Type B"
3466+
df = pd.DataFrame([[1, 2], [2, 5]], columns=[xcol, ycol])
3467+
3468+
# default is the labels are column names
3469+
ax = df.plot(kind=kind, x=xcol, y=ycol, xlabel=xlabel, ylabel=ylabel)
3470+
assert ax.get_xlabel() == (xcol if xlabel is None else xlabel)
3471+
assert ax.get_ylabel() == (ycol if ylabel is None else ylabel)
3472+
34523473
@pytest.mark.parametrize(
34533474
"index_name, old_label, new_label",
34543475
[

0 commit comments

Comments
 (0)