Skip to content

BUG: fix hardcoded scatter marker size issue #54204 #54304

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions doc/source/getting_started/intro_tutorials/04_plotting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ I want to visually compare the :math:`NO_2` values measured in London versus Par
.. ipython:: python

@savefig 04_airqual_scatter.png
air_quality.plot.scatter(x="station_london", y="station_paris", alpha=0.5)
air_quality.plot.scatter(x="station_london", y="station_paris", s = 20, alpha=0.5)
plt.show()

.. raw:: html
Expand All @@ -117,11 +117,14 @@ standard Python to get an overview of the available plot methods:

.. ipython:: python

[
method_name
for method_name in dir(air_quality.plot)
if not method_name.startswith("_")
]
import warnings
Copy link
Member

Choose a reason for hiding this comment

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

You can add an :okwarning: below the ipython directive instead of this

with warnings.catch_warnings():
warnings.simplefilter("ignore")
[
method_name
for method_name in dir(air_quality.plot)
if not method_name.startswith("_")
]

.. note::
In many development environments as well as IPython and
Expand Down
2 changes: 1 addition & 1 deletion doc/source/user_guide/dsintro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ greater than 5, calculate the ratio, and plot:
SepalRatio=lambda x: x.SepalWidth / x.SepalLength,
PetalRatio=lambda x: x.PetalWidth / x.PetalLength,
)
.plot(kind="scatter", x="SepalRatio", y="PetalRatio")
.plot(kind="scatter", x="SepalRatio", y="PetalRatio", s = 20)
)

Since a function is passed in, the function is computed on the DataFrame
Expand Down
2 changes: 1 addition & 1 deletion doc/source/user_guide/visualization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ It is recommended to specify ``color`` and ``label`` keywords to distinguish eac

ax = df.plot.scatter(x="a", y="b", color="DarkBlue", label="Group 1")
@savefig scatter_plot_repeated.png
df.plot.scatter(x="c", y="d", color="DarkGreen", label="Group 2", ax=ax);
df.plot.scatter(x="c", y="d", color="DarkGreen", label="Group 2", ax=ax, s = 20);

.. ipython:: python
:suppress:
Expand Down
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ Plotting
^^^^^^^^
- Bug in :meth:`Series.plot` when invoked with ``color=None`` (:issue:`51953`)
- Fixed UserWarning in :meth:`DataFrame.plot.scatter` when invoked with ``c="b"`` (:issue:`53908`)
-
- Fixed bug in :meth:`DataFrame.plot.scatter` wherein marker size was previously hardcoded to a default value (:issue:`54204`)
Copy link
Member

Choose a reason for hiding this comment

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

Should be a note in the deprecation section now


Groupby/resample/rolling
^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
15 changes: 12 additions & 3 deletions pandas/plotting/_matplotlib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1199,9 +1199,18 @@ def _kind(self) -> Literal["scatter"]:

def __init__(self, data, x, y, s=None, c=None, **kwargs) -> None:
if s is None:
# hide the matplotlib default for size, in case we want to change
# the handling of this argument later
s = 20
# The default size of the elements in a scatter plot
# is now based on the rcParam ``lines.markersize``.
# This means that if rcParams are temporarily changed,
# the marker size changes as well according to mpl.rc_context().
warnings.warn(
"""The default of s=20 is deprecated and
has changed to mpl.rcParams['lines.markersize'].
Copy link
Member

Choose a reason for hiding this comment

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

  1. Single quotes
  2. has changed -> will be changed

Specify `s` to suppress this warning""",
DeprecationWarning,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
DeprecationWarning,
FutureWarning,

stacklevel=find_stack_level(),
)
s = mpl.rcParams["lines.markersize"] ** 2.0
elif is_hashable(s) and s in data.columns:
s = data[s]
super().__init__(data, x, y, s=s, **kwargs)
Expand Down
32 changes: 32 additions & 0 deletions pandas/tests/plotting/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,3 +662,35 @@ def test_bar_plt_xaxis_intervalrange(self):
(a.get_text() == b.get_text())
for a, b in zip(s.plot.bar().get_xticklabels(), expected)
)

@pytest.mark.filterwarnings("default")
Copy link
Member

Choose a reason for hiding this comment

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

def test_change_scatter_markersize_rcparams(self):
# GH 54204
# Ensure proper use of lines.markersize to style pandas scatter
# plots like matplotlib does.
# Will raise deprecation warnings.
df = DataFrame(data={"x": [1, 2, 3], "y": [1, 2, 3]})

pandas_default = df.plot.scatter(
x="x", y="y", title="pandas scatter, default rc marker size"
)

mpl_default = mpl.pyplot.scatter(df["x"], df["y"])

# verify that pandas and matplotlib scatter
# default marker size are the same (s = 6^2 = 36)
assert (
pandas_default.collections[0].get_sizes()[0] == mpl_default.get_sizes()[0]
)

with mpl.rc_context({"lines.markersize": 10}):
pandas_changed = df.plot.scatter(
x="x", y="y", title="pandas scatter, changed rc marker size"
)
mpl_changed = mpl.pyplot.scatter(df["x"], df["y"])

# verify that pandas and matplotlib scatter
# changed marker size are the same (s = 10^2 = 100)
assert (
pandas_changed.collections[0].get_sizes()[0] == mpl_changed.get_sizes()[0]
)