Fixes to .plot() (for Map) and .peek() (for all classes)#3103
Conversation
|
Thanks for the pull request @ayshih! Everything looks great! |
|
To be clear, |
.plot() (for Map) and .peek() (for all classes)
|
Not just for |
|
So I think I am pretty much fine with this, but I think we could simplify things by making a decorator which does this and is solely used for peek. The following decorator would maintain the current behaviour of def peek_show_interactive(func):
"""
A decorator to place on ``peek()`` methods to show the figure.
"""
@wraps(func)
def show_if_interactive(*args, **kwargs):
figure = func(*args, **kwargs)
# Show the figure if using an interactive Matplotlib backend
if mpl.get_backend() in mpl.rcsetup.interactive_bk:
figure.show()
# NOTE: We do not return `figure` here because `peek()` methods return `None`.
return show_if_interactiveWe could then optionally later on make the decorator return the |
dstansby
left a comment
There was a problem hiding this comment.
In general, figure.show() can't be relied upon to properly show a figure; there is a warning in the latest version of the Matplotlib docs which isn't yet released: https://matplotlib.org/devdocs/api/_as_gen/matplotlib.figure.Figure.html?highlight=figure%20show#matplotlib.figure.Figure.show
I think the calls to figure.show() should either be changed to plt.show(), or be removed completely. I think the 'pythonic' way of doing things would be to have .peek() create and return a figure and Axes, and then let the user control showing of the figure, but guess the idea of peek() is to show the figure immediately.
|
Oh goodness my brain. We stopped @dstansby Yeah we have always wanted the behaviour of Could we just replace the |
|
@ayshih I have added one commit on the end here which migrates this logic into a decorator. If you don't like it we can always remove it. After further conversations with @dstansby I think we might want to tweak the logic later, so having it all in once place makes that easier. |
|
👍 on the decorator approach 👎 on using
👎 on removing the check for an interactive backend
|
|
I definitely don't think using Sadly I think using I'm not sure what the problem is with calling |
I don't know what you are saying here. What do you think that
I don't use
I don't actually know either, but I'd prefer to avoid supporting a use pattern that Matplotlib discourages and says is "unsupported" (assuming that their FAQ reflects their current conception). |
|
I just did some testing, and |
|
Ha ha, I just stumbled across a comment of mine from just over five years ago:
I guess back then I thought that |
|
The changelog on this needs updating as the behaviour has now changed to |
|
Following all of the discussion on
Thoughts? |
|
@Cadair pointed out that my "script" use case may not actually exist, so I need to find out whether I am crazy or not |
|
That sounds good to me. |
|
Are we happy with this as it is? |
|
Mostly. I'm fairly convinced that the "script" use case that I was worried about earlier doesn't actually exist. However, I'd like the following changes:
@Cadair Should I make these changes myself? You have more commits to this PR than I do. =) |
|
@ayshih please always push things 😄 Whoever get's a chance first can do it! |
|
Okay, I've made the changes that I wanted. I didn't update the |
|
@nabobalis backport please. |
This PR:
toggle_pylabdecorator everywhere because it led to wonky behavior in Jupyter notebooks when using theinlinebackend, with figures sometimes auto-showing and sometimes not auto-showing depending on the sequence of callsChangespeek()so that it callsfigure.show()only if an interactive Matplotlib backend is being usedpeek()methods to useplt.show()instead ofFigure.show(), andplt.show()is called regardless of which Matplotlib backend is being used. This change means thatpeek()methods now respect Matplotlib's blocking/non-blocking behavior (i.e., whether interactive mode is on or off)peek()methods using a decorator (peek_show) so that we can easily modify the behavior ofpeek()methods in the future as needed (e.g., when Matplotlib enhancesplt.show()to be able to show only one figure)Fixes #3094 and closes #2352