Skip to content

Commit 95dfb3d

Browse files
committed
VIS: Accept xlabel and ylabel for scatter and hexbin plots
1 parent 9cb3723 commit 95dfb3d

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-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
@@ -922,8 +922,10 @@ def nseries(self) -> int:
922922

923923
def _post_plot_logic(self, ax: "Axes", data):
924924
x, y = self.x, self.y
925-
ax.set_ylabel(pprint_thing(y))
926-
ax.set_xlabel(pprint_thing(x))
925+
xlabel = self.xlabel if self.xlabel is not None else pprint_thing(x)
926+
ylabel = self.ylabel if self.ylabel is not None else pprint_thing(y)
927+
ax.set_xlabel(xlabel)
928+
ax.set_ylabel(ylabel)
927929

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

pandas/tests/plotting/test_frame.py

+20
Original file line numberDiff line numberDiff line change
@@ -3449,6 +3449,26 @@ 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+
xcol = "Type A"
3464+
ycol = "Type B"
3465+
df = pd.DataFrame([[1, 2], [2, 5]], columns=[xcol, ycol])
3466+
3467+
# default is the labels are column names
3468+
ax = df.plot(kind=kind, x=xcol, y=ycol, xlabel=xlabel, ylabel=ylabel)
3469+
assert ax.get_xlabel() == (xcol if xlabel is None else xlabel)
3470+
assert ax.get_ylabel() == (ycol if ylabel is None else ylabel)
3471+
34523472
@pytest.mark.parametrize(
34533473
"index_name, old_label, new_label",
34543474
[

0 commit comments

Comments
 (0)