Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a0ac7cf

Browse files
committed
Improve docs for format determination in savefig()/imsave().
Most importantly, the current backend is mostly irrelevant (unlike what the docs state), because we handle backend switching transparently.
1 parent c22847b commit a0ac7cf

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

lib/matplotlib/figure.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,17 +2009,17 @@ def savefig(self, fname, *, frameon=None, transparent=None, **kwargs):
20092009
Parameters
20102010
----------
20112011
2012-
fname : str or file-like object
2013-
A string containing a path to a filename, or a Python
2014-
file-like object, or possibly some backend-dependent object
2015-
such as :class:`~matplotlib.backends.backend_pdf.PdfPages`.
2012+
fname : str or PathLike or file-like object
2013+
A path, or a Python file-like object, or
2014+
possibly some backend-dependent object such as
2015+
`matplotlib.backends.backend_pdf.PdfPages`.
20162016
2017-
If *format* is *None* and *fname* is a string, the output
2018-
format is deduced from the extension of the filename. If
2019-
the filename has no extension, :rc:`savefig.format` is used.
2017+
If *format* is not set, then the output format is inferred from
2018+
the extension of *fname*, if any, and from :rc:`savefig.format`
2019+
otherwise. If *format* is set, it determines the output format.
20202020
2021-
If *fname* is not a string, remember to specify *format* to
2022-
ensure that the correct backend is used.
2021+
Hence, if *fname* is not a path or has no extension, remember to
2022+
specify *format* to ensure that the correct backend is used.
20232023
20242024
Other Parameters
20252025
----------------
@@ -2063,8 +2063,8 @@ def savefig(self, fname, *, frameon=None, transparent=None, **kwargs):
20632063
output.
20642064
20652065
format : str
2066-
One of the file extensions supported by the active
2067-
backend. Most backends support png, pdf, ps, eps and svg.
2066+
The file format, e.g. 'png', 'pdf', 'svg', ... The behavior when
2067+
this is unset is documented under *fname*.
20682068
20692069
transparent : bool
20702070
If *True*, the axes patches will all be transparent; the

lib/matplotlib/image.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,16 +1398,15 @@ def imread(fname, format=None):
13981398
def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
13991399
origin=None, dpi=100):
14001400
"""
1401-
Save an array as in image file.
1402-
1403-
The output formats available depend on the backend being used.
1401+
Save an array as an image file.
14041402
14051403
Parameters
14061404
----------
1407-
fname : str or file-like
1408-
The filename or a Python file-like object to store the image in.
1409-
The necessary output format is inferred from the filename extension
1410-
but may be explicitly overwritten using *format*.
1405+
fname : str or PathLike file-like
1406+
A path or a Python file-like object to store the image in.
1407+
If *format* is not set, then the output format is inferred from the
1408+
extension of *fname*, if any, and from :rc:`savefig.format` otherwise.
1409+
If *format* is set, it determines the output format.
14111410
arr : array-like
14121411
The image data. The shape can be one of
14131412
MxN (luminance), MxNx3 (RGB) or MxNx4 (RGBA).
@@ -1421,9 +1420,8 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
14211420
maps scalar data to colors. It is ignored for RGB(A) data.
14221421
Defaults to :rc:`image.cmap` ('viridis').
14231422
format : str, optional
1424-
The file format, e.g. 'png', 'pdf', 'svg', ... . If not given, the
1425-
format is deduced form the filename extension in *fname*.
1426-
See `.Figure.savefig` for details.
1423+
The file format, e.g. 'png', 'pdf', 'svg', ... The behavior when this
1424+
is unset is documented under *fname*.
14271425
origin : {'upper', 'lower'}, optional
14281426
Indicates whether the ``(0, 0)`` index of the array is in the upper
14291427
left or lower left corner of the axes. Defaults to :rc:`image.origin`

0 commit comments

Comments
 (0)