-
-
Notifications
You must be signed in to change notification settings - Fork 8k
ENH: Allow to register standalone figures with pyplot #29855
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
Open
timhoffm
wants to merge
14
commits into
matplotlib:main
Choose a base branch
from
timhoffm:pyplot-register-figure
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
bc31177
ENH: Allow to register standalone figures with pyplot
timhoffm c718eae
Clarifying comment
timhoffm a38f9bb
Fix: properly decouple from pyplot and specific backends when destroying
timhoffm eab4a89
TST: do not use non-baseclass methods to get renderer in tests
tacaswell 2396e90
TST: fix wx window closing test
tacaswell b7a5cfd
TST: try forcing the dpi when testing saving after closing window
tacaswell c578ec6
FIX: do not add super().destroy to Canvas destroy method
tacaswell 06b97a8
Add what's new note
timhoffm db30cbe
MNT: restore the old DPI when resetting the canvas
tacaswell 1ea856d
TST: verify that dpi is restored on close
tacaswell 0e078a7
Update lib/matplotlib/figure.py
timhoffm 1784c73
Add docs to state that you should not keep a reference to the canvas
timhoffm 782750e
Reword what's new message
timhoffm 8b77fff
DOC: move whats new to new location
tacaswell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
Figures can be attached to and removed from pyplot | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
Figures can now be attached to and removed from management through pyplot, which in | ||
the background also means a less strict coupling to backends. | ||
|
||
In particular, standalone figures (created with the `.Figure` constructor) can now be | ||
registered with the `.pyplot` module by calling ``plt.figure(fig)``. This allows to | ||
show them with ``plt.show()`` as you would do with any figure created with pyplot | ||
factory methods such as ``plt.figure()`` or ``plt.subplots()``. | ||
|
||
When closing a shown figure window, the related figure is reset to the standalone | ||
state, i.e. it's not visible to pyplot anymore, but if you still hold a reference | ||
to it, you can continue to work with it (e.g. do ``fig.savefig()``, or re-add it | ||
to pyplot with ``plt.figure(fig)`` and then show it again). | ||
|
||
The following is now possible - though the example is exaggerated to show what's | ||
possible. In practice, you'll stick with much simpler versions for better | ||
consistency :: | ||
|
||
import matplotlib.pyplot as plt | ||
from matplotlib.figure import Figure | ||
|
||
# Create a standalone figure | ||
fig = Figure() | ||
ax = fig.add_subplot() | ||
ax.plot([1, 2, 3], [4, 5, 6]) | ||
|
||
# Register it with pyplot | ||
plt.figure(fig) | ||
|
||
# Modify the figure through pyplot | ||
plt.xlabel("x label") | ||
|
||
# Show the figure | ||
plt.show() | ||
|
||
# Close the figure window through the GUI | ||
|
||
# Continue to work on the figure | ||
fig.savefig("my_figure.png") | ||
ax.set_ylabel("y label") | ||
|
||
# Re-register the figure and show it again | ||
plt.figure(fig) | ||
plt.show() | ||
|
||
Technical detail: Standalone figures use `.FigureCanvasBase` as canvas. This is | ||
replaced by a backend-dependent subclass when registering with pyplot, and is | ||
reset to `.FigureCanvasBase` when the figure is closed. `.Figure.savefig` uses | ||
the current canvas to save the figure (if possible). Since `.FigureCanvasBase` | ||
can not render the figure, when using savefig it will fallback to`.FigureCanvasAgg` | ||
which is Agg-based. Any Agg-based backend will create the same file output, however | ||
There may be slight differences for non-Agg backends; e.g. if you use "GTK4Cairo" as | ||
interactive backend, ``fig.savefig("file.png")`` may create a slightly different | ||
image depending on whether the figure is registered with pyplot or not. In | ||
general, you should not store a reference to the canvas, but rather always | ||
obtain it from the figure with ``fig.canvas``. This will return the current | ||
canvas, which is either the original `.FigureCanvasBase` or a backend-dependent | ||
subclass, depending on whether the figure is registered with pyplot or not. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we want to be technically correct, hopefully without giving too many details: "when saving the figure, it will fallback to a suitable canvas subclass, e.g. FigureCanvasAgg for raster outputs such as png" (fallback is to FigureCanvasPdf for pdf, though that also yields different results if one was using gtk4cairo, as (gtk4)cairo can also directly output to pdf).