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

Skip to content

Commit b17774c

Browse files
authored
Merge pull request #29555 from meeseeksmachine/auto-backport-of-pr-29546-on-v3.10.x
Backport PR #29546 on branch v3.10.x (FIX: pyplot.matshow figure handling)
2 parents ab1e0a5 + edf8076 commit b17774c

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

lib/matplotlib/pyplot.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -994,8 +994,8 @@ def figure(
994994
root_fig = num.get_figure(root=True)
995995
if root_fig.canvas.manager is None:
996996
raise ValueError("The passed figure is not managed by pyplot")
997-
elif any([figsize, dpi, facecolor, edgecolor, not frameon,
998-
kwargs]) and root_fig.canvas.manager.num in allnums:
997+
elif (any(param is not None for param in [figsize, dpi, facecolor, edgecolor])
998+
or not frameon or kwargs) and root_fig.canvas.manager.num in allnums:
999999
_api.warn_external(
10001000
"Ignoring specified arguments in this call because figure "
10011001
f"with num: {root_fig.canvas.manager.num} already exists")
@@ -1007,8 +1007,8 @@ def figure(
10071007
if num is None:
10081008
num = next_num
10091009
else:
1010-
if any([figsize, dpi, facecolor, edgecolor, not frameon,
1011-
kwargs]) and num in allnums:
1010+
if (any(param is not None for param in [figsize, dpi, facecolor, edgecolor])
1011+
or not frameon or kwargs) and num in allnums:
10121012
_api.warn_external(
10131013
"Ignoring specified arguments in this call "
10141014
f"because figure with num: {num} already exists")
@@ -2662,9 +2662,13 @@ def matshow(A: ArrayLike, fignum: None | int = None, **kwargs) -> AxesImage:
26622662
if fignum == 0:
26632663
ax = gca()
26642664
else:
2665-
# Extract actual aspect ratio of array and make appropriately sized
2666-
# figure.
2667-
fig = figure(fignum, figsize=figaspect(A))
2665+
if fignum is not None and fignum_exists(fignum):
2666+
# Do not try to set a figure size.
2667+
figsize = None
2668+
else:
2669+
# Extract actual aspect ratio of array and make appropriately sized figure.
2670+
figsize = figaspect(A)
2671+
fig = figure(fignum, figsize=figsize)
26682672
ax = fig.add_axes((0.15, 0.09, 0.775, 0.775))
26692673
im = ax.matshow(A, **kwargs)
26702674
sci(im)

lib/matplotlib/tests/test_pyplot.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -459,13 +459,13 @@ def test_figure_hook():
459459

460460

461461
def test_multiple_same_figure_calls():
462-
fig = mpl.pyplot.figure(1, figsize=(1, 2))
462+
fig = plt.figure(1, figsize=(1, 2))
463463
with pytest.warns(UserWarning, match="Ignoring specified arguments in this call"):
464-
fig2 = mpl.pyplot.figure(1, figsize=(3, 4))
464+
fig2 = plt.figure(1, figsize=np.array([3, 4]))
465465
with pytest.warns(UserWarning, match="Ignoring specified arguments in this call"):
466-
mpl.pyplot.figure(fig, figsize=(5, 6))
466+
plt.figure(fig, figsize=np.array([5, 6]))
467467
assert fig is fig2
468-
fig3 = mpl.pyplot.figure(1) # Checks for false warnings
468+
fig3 = plt.figure(1) # Checks for false warnings
469469
assert fig is fig3
470470

471471

@@ -475,3 +475,11 @@ def test_close_all_warning():
475475
# Check that the warning is issued when 'all' is passed to plt.figure
476476
with pytest.warns(UserWarning, match="closes all existing figures"):
477477
fig2 = plt.figure("all")
478+
479+
480+
def test_matshow():
481+
fig = plt.figure()
482+
arr = [[0, 1], [1, 2]]
483+
484+
# Smoke test that matshow does not ask for a new figsize on the existing figure
485+
plt.matshow(arr, fignum=fig.number)

0 commit comments

Comments
 (0)