Skip to content
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

Add 'color' and 'size' to arguments #44856

Merged
merged 24 commits into from Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.0.rst
Expand Up @@ -422,6 +422,7 @@ Plotting
- Bug in :meth:`DataFrame.boxplot` that prevented passing in ``xlabel`` and ``ylabel`` (:issue:`45463`)
- Bug in :meth:`DataFrame.boxplot` that prevented specifying ``vert=False`` (:issue:`36918`)
- Bug in :meth:`DataFrame.plot.scatter` that prevented specifying ``norm`` (:issue:`45809`)
- 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`)

Groupby/resample/rolling
^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
24 changes: 20 additions & 4 deletions pandas/plotting/_core.py
Expand Up @@ -1603,7 +1603,8 @@ def scatter(self, x, y, s=None, c=None, **kwargs):
The column name or column position to be used as vertical
coordinates for each point.
s : str, scalar or array-like, optional
The size of each point. Possible values are:
The size of each point. ``size`` is also accepted as an alternate keyword
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we can list s, size : str.... here (i don't know if we do this for aliases elsewhere but should follow the same pattern), e.g. see read_csv where we have some aliases

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I follow the syntax of read_csv I have to write

size : str, scalar or array-like, optional
    Alias for s

I did not find a notation like s, size: str.

argument for ``s`` for consistency with other methods. Possible values are:

- A string with the name of the column to be used for marker's size.

Expand All @@ -1613,10 +1614,11 @@ def scatter(self, x, y, s=None, c=None, **kwargs):
recursively. For instance, when passing [2,14] all points size
will be either 2 or 14, alternatively.

.. versionchanged:: 1.1.0
.. versionchanged:: 1.5.0

c : str, int or array-like, optional
The color of each point. Possible values are:
The color of each point. ``color`` is also accepted as an alternate keyword
argument for ``c`` for consistency with other methods. Possible values are:

- A single color string referred to by name, RGB or RGBA code,
for instance 'red' or '#a98d19'.
Expand All @@ -1629,6 +1631,8 @@ def scatter(self, x, y, s=None, c=None, **kwargs):
- A column name or position whose values will be used to color the
marker points according to a colormap.

.. versionchanged:: 1.5.0

**kwargs
Keyword arguments to pass on to :meth:`DataFrame.plot`.

Expand Down Expand Up @@ -1666,7 +1670,19 @@ def scatter(self, x, y, s=None, c=None, **kwargs):
... c='species',
... colormap='viridis')
"""
return self(kind="scatter", x=x, y=y, s=s, c=c, **kwargs)
size = kwargs.pop("size", None)
if s is not None and size is not None:
raise TypeError("Specify exactly one of `s` and `size`")
MarcoGorelli marked this conversation as resolved.
Show resolved Hide resolved
elif s is not None or size is not None:
kwargs["s"] = s if s is not None else size

color = kwargs.pop("color", None)
if c is not None and color is not None:
raise TypeError("Specify exactly one of `c` and `color`")
elif c is not None or color is not None:
kwargs["c"] = c if c is not None else color

return self(kind="scatter", x=x, y=y, **kwargs)

def hexbin(self, x, y, C=None, reduce_C_function=None, gridsize=None, **kwargs):
"""
Expand Down
6 changes: 5 additions & 1 deletion pandas/tests/plotting/frame/test_frame.py
@@ -1,4 +1,3 @@
""" Test cases for DataFrame.plot """
from datetime import (
date,
datetime,
Expand Down Expand Up @@ -654,6 +653,11 @@ def test_plot_scatter(self):
with pytest.raises(TypeError, match=msg):
df.plot.scatter(y="y")

with pytest.raises(TypeError, match="Specify exactly one of `s` and `size`"):
df.plot.scatter(x="x", y="y", s=2, size=2)
with pytest.raises(TypeError, match="Specify exactly one of `c` and `color`"):
df.plot.scatter(x="a", y="b", c="red", color="green")

# GH 6951
axes = df.plot(x="x", y="y", kind="scatter", subplots=True)
self._check_axes_shape(axes, axes_num=1, layout=(1, 1))
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/plotting/frame/test_frame_color.py
Expand Up @@ -196,14 +196,15 @@ def test_if_scatterplot_colorbars_are_next_to_parent_axes(self):
assert np.isclose(parent_distance, colorbar_distance, atol=1e-7).all()

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

def test_scatter_colors(self):
Expand Down