Thanks to visit codestin.com
Credit goes to github.com

Skip to content

FIX: pyplot.matshow figure handling #29546

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

Merged
merged 1 commit into from
Jan 31, 2025
Merged
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
18 changes: 11 additions & 7 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -997,8 +997,8 @@
root_fig = num.get_figure(root=True)
if root_fig.canvas.manager is None:
raise ValueError("The passed figure is not managed by pyplot")
elif any([figsize, dpi, facecolor, edgecolor, not frameon,
kwargs]) and root_fig.canvas.manager.num in allnums:
elif (any(param is not None for param in [figsize, dpi, facecolor, edgecolor])
or not frameon or kwargs) and root_fig.canvas.manager.num in allnums:
_api.warn_external(
"Ignoring specified arguments in this call because figure "
f"with num: {root_fig.canvas.manager.num} already exists")
Expand All @@ -1010,8 +1010,8 @@
if num is None:
num = next_num
else:
if any([figsize, dpi, facecolor, edgecolor, not frameon,
kwargs]) and num in allnums:
if (any(param is not None for param in [figsize, dpi, facecolor, edgecolor])
or not frameon or kwargs) and num in allnums:
_api.warn_external(
"Ignoring specified arguments in this call "
f"because figure with num: {num} already exists")
Expand Down Expand Up @@ -2663,9 +2663,13 @@
if fignum == 0:
ax = gca()
else:
# Extract actual aspect ratio of array and make appropriately sized
# figure.
fig = figure(fignum, figsize=figaspect(A))
if fignum is not None and fignum_exists(fignum):
# Do not try to set a figure size.
figsize = None
else:
# Extract actual aspect ratio of array and make appropriately sized figure.
figsize = figaspect(A)

Check warning on line 2671 in lib/matplotlib/pyplot.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/pyplot.py#L2671

Added line #L2671 was not covered by tests
fig = figure(fignum, figsize=figsize)
ax = fig.add_axes((0.15, 0.09, 0.775, 0.775))
im = ax.matshow(A, **kwargs)
sci(im)
Expand Down
16 changes: 12 additions & 4 deletions lib/matplotlib/tests/test_pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,13 +460,13 @@ def test_figure_hook():


def test_multiple_same_figure_calls():
fig = mpl.pyplot.figure(1, figsize=(1, 2))
fig = plt.figure(1, figsize=(1, 2))
with pytest.warns(UserWarning, match="Ignoring specified arguments in this call"):
fig2 = mpl.pyplot.figure(1, figsize=(3, 4))
fig2 = plt.figure(1, figsize=np.array([3, 4]))
with pytest.warns(UserWarning, match="Ignoring specified arguments in this call"):
mpl.pyplot.figure(fig, figsize=(5, 6))
plt.figure(fig, figsize=np.array([5, 6]))
assert fig is fig2
fig3 = mpl.pyplot.figure(1) # Checks for false warnings
fig3 = plt.figure(1) # Checks for false warnings
assert fig is fig3


Expand All @@ -476,3 +476,11 @@ def test_close_all_warning():
# Check that the warning is issued when 'all' is passed to plt.figure
with pytest.warns(UserWarning, match="closes all existing figures"):
fig2 = plt.figure("all")


def test_matshow():
fig = plt.figure()
arr = [[0, 1], [1, 2]]

# Smoke test that matshow does not ask for a new figsize on the existing figure
plt.matshow(arr, fignum=fig.number)
Loading