diff --git a/doc/source/getting_started/intro_tutorials/04_plotting.rst b/doc/source/getting_started/intro_tutorials/04_plotting.rst index ddc8a37911c98..1a805d3071552 100644 --- a/doc/source/getting_started/intro_tutorials/04_plotting.rst +++ b/doc/source/getting_started/intro_tutorials/04_plotting.rst @@ -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 @@ -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 + 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 diff --git a/doc/source/user_guide/dsintro.rst b/doc/source/user_guide/dsintro.rst index 4b0829e4a23b9..f359e9304ce24 100644 --- a/doc/source/user_guide/dsintro.rst +++ b/doc/source/user_guide/dsintro.rst @@ -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 diff --git a/doc/source/user_guide/visualization.rst b/doc/source/user_guide/visualization.rst index 9081d13ef2cf1..53e9314e80b46 100644 --- a/doc/source/user_guide/visualization.rst +++ b/doc/source/user_guide/visualization.rst @@ -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: diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 0fdec3175f635..3d7f154671f01 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -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`) Groupby/resample/rolling ^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index c62f73271577d..f3e9ec094168f 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -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']. + Specify `s` to suppress this warning""", + DeprecationWarning, + 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) diff --git a/pandas/tests/plotting/test_misc.py b/pandas/tests/plotting/test_misc.py index 53219e0d20b6d..428073f7a7a04 100644 --- a/pandas/tests/plotting/test_misc.py +++ b/pandas/tests/plotting/test_misc.py @@ -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") + 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] + )