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

[Bug]: set_aspect with negative argument leads to infinite loop #23847

Closed
jvanhoefer opened this issue Sep 9, 2022 · 14 comments · Fixed by #23931
Closed

[Bug]: set_aspect with negative argument leads to infinite loop #23847

jvanhoefer opened this issue Sep 9, 2022 · 14 comments · Fixed by #23931
Labels
Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues Good first issue Open a pull request against these issues if there are no active ones!
Milestone

Comments

@jvanhoefer
Copy link

Bug summary

When calling plt.gca().set_aspect(-1) it leads to a infinite loop, where ValueError: 'box_aspect' and 'fig_aspect' must be positive repeatedly, until I terminate python.

Code for reproduction

import matplotlib.pyplot as plt
plt.plot()
plt.gca().set_aspect(-1)

Actual outcome

ValueError: 'box_aspect' and 'fig_aspect' must be positive is thrown repeatedly

Expected outcome

either a single Value Error, or that you get the same as if you would invert the y-axis, but with a 1:1 aspect ratio.

Additional information

No response

Operating system

iOS

Matplotlib Version

3.5.3

Matplotlib Backend

MacOSX

Python version

3.10

Jupyter version

No response

Installation

pip

@timhoffm
Copy link
Member

timhoffm commented Sep 9, 2022

Please provide more info: a full minimal example (the code snippet above does not raise) and the full stack trace.


Side note: Possibly someting in the backend/event loop. With Qt backend and an added plt.show() I get

Traceback (most recent call last):
  File "/home/tim/anaconda3/envs/mpl35/lib/python3.10/site-packages/matplotlib/backends/backend_qt.py", line 477, in _draw_idle
    self.draw()
  File "/home/tim/anaconda3/envs/mpl35/lib/python3.10/site-packages/matplotlib/backends/backend_agg.py", line 436, in draw
    self.figure.draw(self.renderer)
  File "/home/tim/anaconda3/envs/mpl35/lib/python3.10/site-packages/matplotlib/artist.py", line 73, in draw_wrapper
    result = draw(artist, renderer, *args, **kwargs)
  File "/home/tim/anaconda3/envs/mpl35/lib/python3.10/site-packages/matplotlib/artist.py", line 50, in draw_wrapper
    return draw(artist, renderer)
  File "/home/tim/anaconda3/envs/mpl35/lib/python3.10/site-packages/matplotlib/figure.py", line 2823, in draw
    artists = self._get_draw_artists(renderer)
  File "/home/tim/anaconda3/envs/mpl35/lib/python3.10/site-packages/matplotlib/figure.py", line 238, in _get_draw_artists
    ax.apply_aspect()
  File "/home/tim/anaconda3/envs/mpl35/lib/python3.10/site-packages/matplotlib/axes/_base.py", line 1895, in apply_aspect
    pb1 = pb.shrunk_to_aspect(box_aspect, pb, fig_aspect)
  File "/home/tim/anaconda3/envs/mpl35/lib/python3.10/site-packages/matplotlib/transforms.py", line 558, in shrunk_to_aspect
    raise ValueError("'box_aspect' and 'fig_aspect' must be positive")
ValueError: 'box_aspect' and 'fig_aspect' must be positive

and an empty but responsive figure window. As expected the interpreter is blocked while the window is open. When closing the window, the prompt returns.

Anyway, we should do input validation already on set_aspect().

@tacaswell tacaswell added this to the v3.7.0 milestone Sep 9, 2022
@tacaswell tacaswell added Good first issue Open a pull request against these issues if there are no active ones! Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues labels Sep 9, 2022
@tacaswell
Copy link
Member

Steps needed:

  • add validation in set_aspect that will raise for negative values
  • add a test that it raises

@ZzN1NJ4
Copy link
Contributor

ZzN1NJ4 commented Sep 10, 2022

Hello, May I work on this issue please sir/ma'am ?

@timhoffm
Copy link
Member

@ZzN1NJ4 Go for it.

@jvanhoefer
Copy link
Author

As a more general remark: Setting the aspect ratio via set_aspect does not work together with inverting the y axis with invert_yaxis. (As invert_yaxis does not give an equal aspect ratio, while set_aspect(1) leaves me with a non-inverted y axis...).

Should I open a feature-request issue on that?

@jklymak
Copy link
Member

jklymak commented Sep 10, 2022

That seems like a bug, and a yes, please open a different issue

@jvanhoefer
Copy link
Author

jvanhoefer commented Sep 11, 2022

As a more general remark: Setting the aspect ratio via set_aspect does not work together with inverting the y axis with invert_yaxis. (As invert_yaxis does not give an equal aspect ratio, while set_aspect(1) leaves me with a non-inverted y axis...).

Should I open a feature-request issue on that?

Regarding my above comment: It turned out, that the issue was with some other part of the code I was working on, and matplotlib actually works fine there. Sorry for the confusion.

@KelsonXie
Copy link

KelsonXie commented Sep 15, 2022

Hey, I would like to tackle this issue if you don't mind.

@story645
Copy link
Member

hi @KelsonXie, while we don't assign issues, we'd prefer that effort not get duplicated. Can you wait a few days and see if @ZzN1NJ4 puts in a PR and then help out? https://matplotlib.org/3.6.0/devel/contributing.html#issues-for-new-contributors

@wolbek
Copy link

wolbek commented Sep 17, 2022

Hello Sir/Madam, I would like to contribute to this issue.

@tacaswell
Copy link
Member

@wolbek Please open a PR! As we have said above, we do not assign issues pre-PR (opening the PR is claiming the work ;) ).

@story645
Copy link
Member

story645 commented Sep 18, 2022

@KelsonXie if you held off b/c I asked you to, please put in the PR when you can, or work with @wolbek, and let us know. Thanks!
ETA: since @ZzN1NJ4 opened the PR, can both of you please review and comment on it rather than creating your own.

@ajjayymahato
Copy link

Hi there! I would like to work on this issue. Can you please assign this issue to me?

@QuLogic
Copy link
Member

QuLogic commented Sep 26, 2022

We do not assign issues. As you can see above there is already a PR open, though it is currently in draft.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues Good first issue Open a pull request against these issues if there are no active ones!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants