Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Add a backend kwarg to savefig.#15536
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
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
6 changes: 6 additions & 0 deletionsdoc/users/next_whats_new/2019-10-27-savefig-backend.rst
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,6 @@ | ||
``savefig()`` gained a ``backend`` keyword argument | ||
--------------------------------------------------- | ||
The ``backend`` keyword argument to ``savefig`` can now be used to pick the | ||
rendering backend without having to globally set the backend; e.g. one can save | ||
pdfs using the pgf backend with ``savefig("file.pdf", backend="pgf")``. |
46 changes: 35 additions & 11 deletionslib/matplotlib/backend_bases.py
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 |
---|---|---|
@@ -1951,18 +1951,35 @@ def get_supported_filetypes_grouped(cls): | ||
groupings[name].sort() | ||
return groupings | ||
def _get_output_canvas(self,backend,fmt): | ||
""" | ||
Set the canvasin preparationfor savingthe figure. | ||
Parameters | ||
---------- | ||
backend : str or None | ||
If not None, switch the figure canvas to the ``FigureCanvas`` class | ||
of the given backend. | ||
fmt : str | ||
If *backend* is None, then determine a suitable canvas class for | ||
saving to format *fmt* -- either the current canvas class, if it | ||
supports *fmt*, or whatever `get_registered_canvas_class` returns; | ||
switch the figure canvas to that canvas class. | ||
""" | ||
if backend is not None: | ||
# Return a specific canvas class, if requested. | ||
canvas_class = ( | ||
tacaswell marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
importlib.import_module(cbook._backend_module_name(backend)) | ||
.FigureCanvas) | ||
if not hasattr(canvas_class, f"print_{fmt}"): | ||
raise ValueError( | ||
f"The {backend!r} backend does not support {fmt} output") | ||
elif hasattr(self, f"print_{fmt}"): | ||
# Return the current canvas if it supports the requested format. | ||
return self | ||
else: | ||
# Return a default canvas for the requested format, if it exists. | ||
canvas_class = get_registered_canvas_class(fmt) | ||
if canvas_class: | ||
return self.switch_backends(canvas_class) | ||
# Else report error for unsupported format. | ||
@@ -1972,7 +1989,7 @@ def _get_output_canvas(self, fmt): | ||
def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None, | ||
orientation='portrait', format=None, | ||
*, bbox_inches=None,backend=None,**kwargs): | ||
""" | ||
Render the figure to hardcopy. Set the figure patch face and edge | ||
colors. This is useful because some of the GUIs have a gray figure | ||
@@ -2012,6 +2029,13 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None, | ||
A list of extra artists that will be considered when the | ||
tight bbox is calculated. | ||
backend : str, optional | ||
Use a non-default backend to render the file, e.g. to render a | ||
png file with the "cairo" backend rather than the default "agg", | ||
or a pdf file with the "pgf" backend rather than the default | ||
"pdf". Note that the default backend is normally sufficient. See | ||
:ref:`the-builtin-backends` for a list of valid backends for each | ||
file format. Custom backends can be referenced as "module://...". | ||
""" | ||
if format is None: | ||
# get format from filename, or from backend's default filetype | ||
@@ -2026,7 +2050,7 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None, | ||
format = format.lower() | ||
# get canvas object and print method for format | ||
canvas = self._get_output_canvas(backend,format) | ||
print_method = getattr(canvas, 'print_%s' % format) | ||
if dpi is None: | ||
9 changes: 9 additions & 0 deletionslib/matplotlib/cbook/__init__.py
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
8 changes: 8 additions & 0 deletionslib/matplotlib/figure.py
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
5 changes: 1 addition & 4 deletionslib/matplotlib/pyplot.py
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
10 changes: 10 additions & 0 deletionslib/matplotlib/tests/test_figure.py
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
1 change: 1 addition & 0 deletionstutorials/introductory/usage.py
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
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.