diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py old mode 100644 new mode 100755 index 1035150302d2c..38a8b4620f6e3 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -565,6 +565,14 @@ def axes(self) -> Sequence[Axes]: @cache_readonly def _axes_and_fig(self) -> tuple[Sequence[Axes], Figure]: import matplotlib.pyplot as plt + import warnings + + # Warn users that 'sharex' and 'sharey' are ignored when 'subplots' is False + if not self.subplots and (self.sharex or self.sharey): + warnings.warn( + "'sharex' and 'sharey' parameters are ignored when 'subplots' is set to False.", + UserWarning + ) if self.subplots: naxes = ( @@ -603,6 +611,7 @@ def _axes_and_fig(self) -> tuple[Sequence[Axes], Figure]: axes_seq = cast(Sequence["Axes"], axes) return axes_seq, fig + @property def result(self): """ diff --git a/pandas/tests/plotting/test_sharex_sharey_warning.py b/pandas/tests/plotting/test_sharex_sharey_warning.py new file mode 100644 index 0000000000000..a54086e14ed64 --- /dev/null +++ b/pandas/tests/plotting/test_sharex_sharey_warning.py @@ -0,0 +1,11 @@ +import pytest +import pandas as pd + +def test_plot_sharex_sharey_warning(): + # Create a simple DataFrame + df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) + + # Check for UserWarning when sharex or sharey is set with subplots=False + with pytest.warns(UserWarning, match="parameters are ignored when 'subplots' is set to False") as record: + df.plot(sharex=True, sharey=True, subplots=False) + print(record[0].message)