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

Skip to content

Commit 52ce211

Browse files
authored
Merge pull request #29546 from rcomer/matshow
FIX: pyplot.matshow figure handling
2 parents 7285029 + c281070 commit 52ce211

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
@@ -997,8 +997,8 @@ def figure(
997997
root_fig = num.get_figure(root=True)
998998
if root_fig.canvas.manager is None:
999999
raise ValueError("The passed figure is not managed by pyplot")
1000-
elif any([figsize, dpi, facecolor, edgecolor, not frameon,
1001-
kwargs]) and root_fig.canvas.manager.num in allnums:
1000+
elif (any(param is not None for param in [figsize, dpi, facecolor, edgecolor])
1001+
or not frameon or kwargs) and root_fig.canvas.manager.num in allnums:
10021002
_api.warn_external(
10031003
"Ignoring specified arguments in this call because figure "
10041004
f"with num: {root_fig.canvas.manager.num} already exists")
@@ -1010,8 +1010,8 @@ def figure(
10101010
if num is None:
10111011
num = next_num
10121012
else:
1013-
if any([figsize, dpi, facecolor, edgecolor, not frameon,
1014-
kwargs]) and num in allnums:
1013+
if (any(param is not None for param in [figsize, dpi, facecolor, edgecolor])
1014+
or not frameon or kwargs) and num in allnums:
10151015
_api.warn_external(
10161016
"Ignoring specified arguments in this call "
10171017
f"because figure with num: {num} already exists")
@@ -2663,9 +2663,13 @@ def matshow(A: ArrayLike, fignum: None | int = None, **kwargs) -> AxesImage:
26632663
if fignum == 0:
26642664
ax = gca()
26652665
else:
2666-
# Extract actual aspect ratio of array and make appropriately sized
2667-
# figure.
2668-
fig = figure(fignum, figsize=figaspect(A))
2666+
if fignum is not None and fignum_exists(fignum):
2667+
# Do not try to set a figure size.
2668+
figsize = None
2669+
else:
2670+
# Extract actual aspect ratio of array and make appropriately sized figure.
2671+
figsize = figaspect(A)
2672+
fig = figure(fignum, figsize=figsize)
26692673
ax = fig.add_axes((0.15, 0.09, 0.775, 0.775))
26702674
im = ax.matshow(A, **kwargs)
26712675
sci(im)

lib/matplotlib/tests/test_pyplot.py

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

461461

462462
def test_multiple_same_figure_calls():
463-
fig = mpl.pyplot.figure(1, figsize=(1, 2))
463+
fig = plt.figure(1, figsize=(1, 2))
464464
with pytest.warns(UserWarning, match="Ignoring specified arguments in this call"):
465-
fig2 = mpl.pyplot.figure(1, figsize=(3, 4))
465+
fig2 = plt.figure(1, figsize=np.array([3, 4]))
466466
with pytest.warns(UserWarning, match="Ignoring specified arguments in this call"):
467-
mpl.pyplot.figure(fig, figsize=(5, 6))
467+
plt.figure(fig, figsize=np.array([5, 6]))
468468
assert fig is fig2
469-
fig3 = mpl.pyplot.figure(1) # Checks for false warnings
469+
fig3 = plt.figure(1) # Checks for false warnings
470470
assert fig is fig3
471471

472472

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

0 commit comments

Comments
 (0)