Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

DOC: Explain how to start the mainloop after show(block=False)#29742

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
timhoffm wants to merge1 commit intomatplotlib:main
base:main
Choose a base branch
Loading
fromtimhoffm:doc-show

Conversation

timhoffm
Copy link
Member

No description provided.

@timhoffmtimhoffm added this to thev3.11.0 milestoneMar 12, 2025
@timhoffmtimhoffm added the Documentation: APIfiles in lib/ and doc/api labelMar 12, 2025
Comment on lines +586 to +589
that the event loop is running to have responsive figures; in the
simplest form by calling ``fig.canvas.manager.mainloop()`` which is
what a blocking show is doing internally. Note that the GUI mainloop
itself is blocking.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
thattheeventloopisrunningtohaveresponsivefigures;inthe
simplestformbycalling``fig.canvas.manager.mainloop()``whichis
whatablockingshowisdoinginternally.NotethattheGUImainloop
itselfisblocking.
thattheeventloopisrunningtohaveresponsivefigures.Calling``fig.canvas.manager.mainloop()``
willworkforthesimplecasebecausethatiswhatablockingshowisdoinginternally.NotethattheGUImainloopisitselfblocking.

I think I'm getting tripped up by what simplest form is supposed to mean here

@tacaswell
Copy link
Member

This was vague to avoid having to talk about prompt's inputhooks as well.

@timhoffm
Copy link
MemberAuthor

The current statement is a bit thin:

In this case, you are responsible for ensuring that the event loop is running to have responsive figures.

I guess only a tiny fraction of our users know what the event loop is, and even fewer whether they have to start it and if so how. I know that the answer is a bit more complex, but I think we can give more guidance.

Background: I was asked how to do this if one has N figures (in a simple script), each shown withplt.show(block=False) and in the end wants to start the event loop so that they become responsive. My answer was putfig.canvas.manager.mainloop() at the end. But thinking again, possibly the better answer would be: Don't call show on all the figures individually, but rather use a finalplt.show() to show all collected figures. Right now, I can't think of a scenario where one would useblock=False and then have to explicitly start the event loop yourself later. Do you have one?

@tacaswell
Copy link
Member

A loop where you create / show figures, callplt.pause(N) / otherwise spin the eventloop, and then have a finalplt.show(block=True) at the end to block until all of the figures are closed.

@jklymak
Copy link
Member

Is there a longer guide to this somewhere we can link out to?

@timhoffm
Copy link
MemberAuthor

Not that I'm aware of.

To collect use cases forplt.show(block=False)

  • plt.show(block=False); plt.pause(n) to run the event loop for a limited time.
  • plt.show(block=False); ... plt.show(block=False); ... plt.show(); to show multiple figures - but usually better done by not calling the intermediate shows at all.
  • plt.show(block=False); ... plt.show(block=False); do_something(); fig.canvas.manager.mainloop(), wheredo_something() could e.g. reach for the windows and arrange them in a specific way.
  • inipython to, which does some magic behind the scenes to run the event loop but still not block.
  • ... more ideas?

@jklymak
Copy link
Member

I guess this is somewhat discussed athttps://matplotlib.org/stable/users/explain/figure/interactive.html, andhttps://matplotlib.org/stable/users/explain/figure/interactive_guide.html and in particularhttps://matplotlib.org/stable/users/explain/figure/interactive_guide.html#blocking-the-prompt So maybe just linking that here would help?

timhoffm reacted with thumbs up emoji

@timhoffm
Copy link
MemberAuthor

timhoffm commentedMar 15, 2025
edited
Loading

Semi-OT: maybe we have to rewrite/reframe the "interactive" docs at some time

  • they have been written with a prompt or script workflow in mind and read a bit awkward for people using notebooks.

  • "interactive" is used with several meanings and is thus confusing, at least

    • a basic figure redraw for figure window size changes - a user would likely not call this "interactive" but it ties into the technical mechanisms since it needs an event loop.
    • simple zoom/pan/mouse coordinates
    • the matplotlib event system
    • pyplot "interactive mode", i.e. immediate non-blocking showing of a figure and continuous updates on additional plot commands
    • more refined user-interactivity like ploty has (tooltips, data selection, connected plots, etc.) while one can build that through the event system above, IMO the difference is that the event system is low level infrastructure, and it's quite cumbersome to build interactivity that way. We don't have high-level api that would make this reasonably simple to end users (read: you have to be rather a software developer than a (data) scientist to do that)

@jklymak
Copy link
Member

For sure those docs are very dated. But they do explain blocking etc so linking them would be of some value in my opinion.

@story645
Copy link
Member

Semi-OT: maybe we have to rewrite/reframe the "interactive" docs at some time

Xref:#28722

@timhoffmtimhoffm marked this pull request as draftMarch 17, 2025 13:13
@tacaswell
Copy link
Member

tacaswell commentedMar 18, 2025
edited
Loading

The first three bullet points should be grouped into one: you want windows to pop up and update while some other (slow) loop is running. e.g.

fig, ax = plt.subplots()ln, = ax.plot([], [])plt.show(block=False)old_data = Nonefor step in compute_loop:    new_data = expensive_compute(step, old_data)    ln.set_data(*new_data)    plt.pause(0) # or fig.canvas.manager.mainloop() or fig.canvas.flush_events()    old_data = new_data

Add as much complexity of only updating the plot ever N, having multiple figures, passing in a callback, ..., but I think that is the minimal pseudo-code.

@timhoffm
Copy link
MemberAuthor

timhoffm commentedMar 18, 2025
edited
Loading

The first three bullet points should be grouped into one

While they rely on the same underlying technology, these are quite different from a user perspective - which is what counts for our user docs. Being able to resize your window and expecting that the content adapts, is a natural expectation for GUIs and no user would regard this explicitly as "interactive" (conversely not responding to are window resize would be considered "broken"). That's quite different from "I can zoom into the data", and again quite different from "I can write callbacks to respond to user actions".

timhoffm added a commit to timhoffm/matplotlib that referenced this pull requestMar 18, 2025
Side-topic spinning of frommatplotlib#29742.- Move the paragraph on "GUI events" to it's own section and before  "Event loops". It's related but not directly needed for the event loop  discussion. It was an odd side topic in the old location between  "Event loops" and "Command prompt integration".- Rephrase the section on "Blocking the prompt" and some other smaller  wording improvementsNote: There will be a follow-up onmatplotlib#29742, but I think it's best tokeep these unrelated changes separate and get them out of the way beforereworkingmatplotlib#29742.
timhoffm added a commit that referenced this pull requestApr 3, 2025
* DOC: Improve interactive figures guide / Blocking inputSide-topic spinning of from#29742.- Move the paragraph on "GUI events" to it's own section and before  "Event loops". It's related but not directly needed for the event loop  discussion. It was an odd side topic in the old location between  "Event loops" and "Command prompt integration".- Rephrase the section on "Blocking the prompt" and some other smaller  wording improvementsNote: There will be a follow-up on#29742, but I think it's best tokeep these unrelated changes separate and get them out of the way beforereworking#29742.* Update galleries/users_explain/figure/interactive_guide.rstCo-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>---------Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@story645story645story645 left review comments

At least 1 approving review is required to merge this pull request.

Assignees
No one assigned
Labels
Documentation: APIfiles in lib/ and doc/apitopic: pyplot API
Projects
None yet
Milestone
v3.11.0
Development

Successfully merging this pull request may close these issues.

4 participants
@timhoffm@tacaswell@jklymak@story645

[8]ページ先頭

©2009-2025 Movatter.jp