Skip to content

Commit

Permalink
Figure.savefig: Support saving figures in PPM (.ppm) format (#2771)
Browse files Browse the repository at this point in the history
* Figure.savefig: Support PPM (.ppm) format
* Sort formats alphabetically
* Improve the docstrings to clarify raster and vector image formats
  • Loading branch information
seisman authored Oct 28, 2023
1 parent f828bc5 commit 6cc61d3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
41 changes: 24 additions & 17 deletions pygmt/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,20 +255,27 @@ def savefig(
self, fname, transparent=False, crop=True, anti_alias=True, show=False, **kwargs
):
"""
Save the figure to a file.
Save the figure to an image file.
Supported file formats and their extensions:
Supported image formats and their extensions:
**Raster image formats**
- PNG (``.png``)
- JPEG (``.jpg`` or ``.jpeg``)
- PDF (``.pdf``)
- BMP (``.bmp``)
- TIFF (``.tif``)
- JPEG (``.jpg`` or ``.jpeg``)
- GeoTIFF (``.tiff``)
- PNG (``.png``)
- PPM (``.ppm``)
- TIFF (``.tif``)
**Vector image formats**
- EPS (``.eps``)
- KML (``.kml``)
- PDF (``.pdf``)
For KML format, a companion PNG file is also generated.
Beside the above formats, you can also save the figure to a KML file
(``.kml``), with a companion PNG file generated automatically. The KML
file can be viewed in Google Earth.
You can pass in any keyword arguments that
:meth:`pygmt.Figure.psconvert` accepts.
Expand All @@ -284,11 +291,10 @@ def savefig(
crop : bool
If ``True``, will crop the figure canvas (page) to the plot area.
anti_alias: bool
If ``True``, will use anti-aliasing when creating raster images
(BMP, PNG, JPEG, TIFF, and GeoTIFF). More specifically, it passes
the arguments ``"t2"`` and ``"g2"`` to the ``anti_aliasing``
parameter of :meth:`pygmt.Figure.psconvert`. Ignored if creating
vector graphics.
If ``True``, will use anti-aliasing when creating raster images.
More specifically, it passes the arguments ``"t2"`` and ``"g2"``
to the ``anti_aliasing`` parameter of
:meth:`pygmt.Figure.psconvert`. Ignored if creating vector images.
show: bool
If ``True``, will open the figure in an external viewer.
dpi : int
Expand All @@ -301,14 +307,15 @@ def savefig(
"""
# All supported formats
fmts = {
"png": "g",
"pdf": "f",
"jpg": "j",
"bmp": "b",
"eps": "e",
"jpg": "j",
"kml": "g",
"pdf": "f",
"png": "g",
"ppm": "m",
"tif": "t",
"tiff": None, # GeoTIFF doesn't need the -T option
"kml": "g",
}

fname = Path(fname)
Expand Down
2 changes: 1 addition & 1 deletion pygmt/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def test_figure_savefig_exists():
fig = Figure()
fig.basemap(region="10/70/-300/800", projection="X3i/5i", frame="af")
prefix = "test_figure_savefig_exists"
for fmt in "png pdf jpg jpeg bmp eps tif PNG JPG JPEG Png".split():
for fmt in "bmp eps jpg jpeg pdf png ppm tif PNG JPG JPEG Png".split():
fname = ".".join([prefix, fmt])
fig.savefig(fname)

Expand Down

0 comments on commit 6cc61d3

Please sign in to comment.