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

Skip to content

[Bug]: matshow(..., fignum=...) broken #29540

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

Closed
DerWeh opened this issue Jan 28, 2025 · 1 comment · Fixed by #29545
Closed

[Bug]: matshow(..., fignum=...) broken #29540

DerWeh opened this issue Jan 28, 2025 · 1 comment · Fixed by #29545
Labels
Milestone

Comments

@DerWeh
Copy link
Contributor

DerWeh commented Jan 28, 2025

Bug summary

From Version 3.10.0, plt.matshow(..., fignum=...) is broken.
I tested against the last nightly release matplotlib-3.11.0.dev377+g6c5e3f3348, where it is still broken.

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
plt.matshow(np.random.random([3, 3]), fignum=fig.number)

Actual outcome

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/wehandre/mambaforge/envs/temptest/lib/python3.12/site-packages/matplotlib/pyplot.py", line 2668, in matshow
    fig = figure(fignum, figsize=figaspect(A))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/wehandre/mambaforge/envs/temptest/lib/python3.12/site-packages/matplotlib/pyplot.py", line 1013, in figure
    if any([figsize, dpi, facecolor, edgecolor, not frameon,
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Expected outcome

No error

Additional information

The issue is, that figsize is a NumPy array, and thus cannot be used within any. figsize is the result of the function matplotlib.figure.figaspect. To my knowledge, previously, this returned a tuple, which can be evaluated as a boolean. The documentation still suggests that the result is a tuple. However, I don't see any changes to matshow or figaspect inspecting their blames.

Operating system

No response

Matplotlib Version

3.10.0

Matplotlib Backend

No response

Python version

3.12.8

Jupyter version

No response

Installation

pip

@rcomer
Copy link
Member

rcomer commented Jan 28, 2025

Thanks @DerWeh. It looks like figaspect has not changed for a long time, so we should probably just change the documentation and type hint on that to reflect that it returns an array.

The warning that trips over this was added at #27992. The check there should be adjusted to account for the fact that some of the parameters could be arrays (colours could also potentially be 3- or 4-element arrays).

We do not want the warning to be thrown for this example, so matshow should not pass a figsize if the figure already exists.

So I think this is three issues in one!

@rcomer rcomer linked a pull request Feb 13, 2025 that will close this issue
2 tasks
@QuLogic QuLogic added this to the v3.10.2 milestone Apr 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants