Skip to content
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

Figure.savefig: Support saving figures in PPM (.ppm) format #2771

Merged
merged 4 commits into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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``)
seisman marked this conversation as resolved.
Show resolved Hide resolved
yvonnefroehlich marked this conversation as resolved.
Show resolved Hide resolved
- 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