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 warning for sharex/sharey when subplots=False in plot method #60323

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions pandas/plotting/_matplotlib/core.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down Expand Up @@ -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):
"""
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/plotting/test_sharex_sharey_warning.py
Original file line number Diff line number Diff line change
@@ -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)