Skip to content

Commit

Permalink
Figure.savefig: Clarify that 'transparent' also works for the PNG fil…
Browse files Browse the repository at this point in the history
…e associated with the KML format (#3579)
  • Loading branch information
seisman authored Nov 6, 2024
1 parent 38694be commit 40edcf7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
32 changes: 15 additions & 17 deletions pygmt/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def region(self) -> np.ndarray:
wesn = lib.extract_region()
return wesn

def savefig( # noqa: PLR0912
def savefig(
self,
fname: str | PurePath,
transparent: bool = False,
Expand Down Expand Up @@ -177,7 +177,8 @@ def savefig( # noqa: PLR0912
The desired figure file name, including the extension. See the list of
supported formats and their extensions above.
transparent
Use a transparent background for the figure. Only valid for PNG format.
Use a transparent background for the figure. Only valid for PNG format and
the PNG file asscoiated with KML format.
crop
Crop the figure canvas (page) to the plot area.
anti_alias
Expand All @@ -203,9 +204,9 @@ def savefig( # noqa: PLR0912
"bmp": "b",
"eps": "e",
"jpg": "j",
"kml": "g",
"kml": "G" if transparent is True else "g",
"pdf": "f",
"png": "g",
"png": "G" if transparent is True else "g",
"ppm": "m",
"tif": "t",
"tiff": None, # GeoTIFF doesn't need the -T option
Expand All @@ -226,14 +227,12 @@ def savefig( # noqa: PLR0912
msg = "Extension '.ps' is not supported. Use '.eps' or '.pdf' instead."
raise GMTInvalidInput(msg)
case ext if ext not in fmts:
raise GMTInvalidInput(f"Unknown extension '.{ext}'.")

fmt = fmts[ext]
if transparent:
if fmt != "g":
msg = f"Transparency unavailable for '{ext}', only for png."
msg = f"Unknown extension '.{ext}'."
raise GMTInvalidInput(msg)
fmt = fmt.upper()

if transparent and ext not in {"kml", "png"}:
msg = f"Transparency unavailable for '{ext}', only for png and kml."
raise GMTInvalidInput(msg)
if anti_alias:
kwargs["Qt"] = 2
kwargs["Qg"] = 2
Expand All @@ -244,18 +243,17 @@ def savefig( # noqa: PLR0912
raise GMTInvalidInput(msg)
kwargs["W"] = True

# pytest-mpl v0.17.0 added the "metadata" parameter to Figure.savefig, which
# is not recognized. So remove it before calling Figure.psconvert.
# pytest-mpl v0.17.0 added the "metadata" parameter to Figure.savefig, which is
# not recognized. So remove it before calling Figure.psconvert.
kwargs.pop("metadata", None)
self.psconvert(prefix=prefix, fmt=fmt, crop=crop, **kwargs)
self.psconvert(prefix=prefix, fmt=fmts[ext], crop=crop, **kwargs)

# Remove the .pgw world file if exists.
# Not necessary after GMT 6.5.0.
# Remove the .pgw world file if exists. Not necessary after GMT 6.5.0.
# See upstream fix https://github.com/GenericMappingTools/gmt/pull/7865
if ext == "tiff":
fname.with_suffix(".pgw").unlink(missing_ok=True)

# Rename if file extension doesn't match the input file suffix
# Rename if file extension doesn't match the input file suffix.
if ext != suffix[1:]:
fname.with_suffix("." + ext).rename(fname)

Expand Down
11 changes: 10 additions & 1 deletion pygmt/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,21 @@ def test_figure_savefig_transparent():
fname = f"{prefix}.{fmt}"
with pytest.raises(GMTInvalidInput):
fig.savefig(fname, transparent=True)
# png should not raise an error

# PNG should support transparency and should not raise an error.
fname = Path(f"{prefix}.png")
fig.savefig(fname, transparent=True)
assert fname.exists()
fname.unlink()

# The companion PNG file with KML format should also support transparency.
fname = Path(f"{prefix}.kml")
fig.savefig(fname, transparent=True)
assert fname.exists()
fname.unlink()
assert fname.with_suffix(".png").exists()
fname.with_suffix(".png").unlink()


def test_figure_savefig_filename_with_spaces():
"""
Expand Down

0 comments on commit 40edcf7

Please sign in to comment.