Skip to content

Commit 5305c97

Browse files
mosc9575yehoshuadimarsky
authored andcommitted
Add 'color' and 'size' to arguments (pandas-dev#44856)
1 parent 3f2796b commit 5305c97

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

doc/source/whatsnew/v1.5.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,7 @@ Plotting
580580
- Bug in :meth:`DataFrame.boxplot` that prevented passing in ``xlabel`` and ``ylabel`` (:issue:`45463`)
581581
- Bug in :meth:`DataFrame.boxplot` that prevented specifying ``vert=False`` (:issue:`36918`)
582582
- Bug in :meth:`DataFrame.plot.scatter` that prevented specifying ``norm`` (:issue:`45809`)
583+
- The function :meth:`DataFrame.plot.scatter` now accepts ``color`` as an alias for ``c`` and ``size`` as an alias for ``s`` for consistency to other plotting functions (:issue:`44670`)
583584

584585
Groupby/resample/rolling
585586
^^^^^^^^^^^^^^^^^^^^^^^^

pandas/plotting/_core.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -1615,6 +1615,11 @@ def scatter(self, x, y, s=None, c=None, **kwargs):
16151615
16161616
.. versionchanged:: 1.1.0
16171617
1618+
size : str, scalar or array-like, optional
1619+
Alias for s.
1620+
1621+
.. versionadded:: 1.5.0
1622+
16181623
c : str, int or array-like, optional
16191624
The color of each point. Possible values are:
16201625
@@ -1628,6 +1633,10 @@ def scatter(self, x, y, s=None, c=None, **kwargs):
16281633
16291634
- A column name or position whose values will be used to color the
16301635
marker points according to a colormap.
1636+
color : str, int or array-like, optional
1637+
Alias for c.
1638+
1639+
.. versionadded:: 1.5.0
16311640
16321641
**kwargs
16331642
Keyword arguments to pass on to :meth:`DataFrame.plot`.
@@ -1666,7 +1675,19 @@ def scatter(self, x, y, s=None, c=None, **kwargs):
16661675
... c='species',
16671676
... colormap='viridis')
16681677
"""
1669-
return self(kind="scatter", x=x, y=y, s=s, c=c, **kwargs)
1678+
size = kwargs.pop("size", None)
1679+
if s is not None and size is not None:
1680+
raise TypeError("Specify exactly one of `s` and `size`")
1681+
elif s is not None or size is not None:
1682+
kwargs["s"] = s if s is not None else size
1683+
1684+
color = kwargs.pop("color", None)
1685+
if c is not None and color is not None:
1686+
raise TypeError("Specify exactly one of `c` and `color`")
1687+
elif c is not None or color is not None:
1688+
kwargs["c"] = c if c is not None else color
1689+
1690+
return self(kind="scatter", x=x, y=y, **kwargs)
16701691

16711692
def hexbin(self, x, y, C=None, reduce_C_function=None, gridsize=None, **kwargs):
16721693
"""

pandas/tests/plotting/frame/test_frame.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
""" Test cases for DataFrame.plot """
21
from datetime import (
32
date,
43
datetime,
@@ -653,6 +652,11 @@ def test_plot_scatter(self):
653652
with pytest.raises(TypeError, match=msg):
654653
df.plot.scatter(y="y")
655654

655+
with pytest.raises(TypeError, match="Specify exactly one of `s` and `size`"):
656+
df.plot.scatter(x="x", y="y", s=2, size=2)
657+
with pytest.raises(TypeError, match="Specify exactly one of `c` and `color`"):
658+
df.plot.scatter(x="a", y="b", c="red", color="green")
659+
656660
# GH 6951
657661
axes = df.plot(x="x", y="y", kind="scatter", subplots=True)
658662
self._check_axes_shape(axes, axes_num=1, layout=(1, 1))

pandas/tests/plotting/frame/test_frame_color.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,15 @@ def test_if_scatterplot_colorbars_are_next_to_parent_axes(self):
196196
assert np.isclose(parent_distance, colorbar_distance, atol=1e-7).all()
197197

198198
@pytest.mark.parametrize("cmap", [None, "Greys"])
199-
def test_scatter_with_c_column_name_with_colors(self, cmap):
199+
@pytest.mark.parametrize("kw", ["c", "color"])
200+
def test_scatter_with_c_column_name_with_colors(self, cmap, kw):
200201
# https://github.com/pandas-dev/pandas/issues/34316
201202
df = DataFrame(
202203
[[5.1, 3.5], [4.9, 3.0], [7.0, 3.2], [6.4, 3.2], [5.9, 3.0]],
203204
columns=["length", "width"],
204205
)
205206
df["species"] = ["r", "r", "g", "g", "b"]
206-
ax = df.plot.scatter(x=0, y=1, c="species", cmap=cmap)
207+
ax = df.plot.scatter(x=0, y=1, cmap=cmap, **{kw: "species"})
207208
assert ax.collections[0].colorbar is None
208209

209210
def test_scatter_colors(self):

0 commit comments

Comments
 (0)