Skip to content
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
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()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes to something like "png": "G" if transparent is True else "g" at line #210.


if transparent and ext not in {"kml", "png"}:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of checking fmt != "g", here we explicitly check if 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