Skip to content

Commit 3718bfa

Browse files
committed
Figure.savefig: Clarify that 'transparent' also works for the PNG file associated with the KML format (#3579)
1 parent 44f9447 commit 3718bfa

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

pygmt/figure.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def region(self) -> np.ndarray:
135135
wesn = lib.extract_region()
136136
return wesn
137137

138-
def savefig( # noqa: PLR0912
138+
def savefig(
139139
self,
140140
fname: str | PurePath,
141141
transparent: bool = False,
@@ -177,7 +177,8 @@ def savefig( # noqa: PLR0912
177177
The desired figure file name, including the extension. See the list of
178178
supported formats and their extensions above.
179179
transparent
180-
Use a transparent background for the figure. Only valid for PNG format.
180+
Use a transparent background for the figure. Only valid for PNG format and
181+
the PNG file asscoiated with KML format.
181182
crop
182183
Crop the figure canvas (page) to the plot area.
183184
anti_alias
@@ -203,9 +204,9 @@ def savefig( # noqa: PLR0912
203204
"bmp": "b",
204205
"eps": "e",
205206
"jpg": "j",
206-
"kml": "g",
207+
"kml": "G" if transparent is True else "g",
207208
"pdf": "f",
208-
"png": "g",
209+
"png": "G" if transparent is True else "g",
209210
"ppm": "m",
210211
"tif": "t",
211212
"tiff": None, # GeoTIFF doesn't need the -T option
@@ -226,14 +227,12 @@ def savefig( # noqa: PLR0912
226227
msg = "Extension '.ps' is not supported. Use '.eps' or '.pdf' instead."
227228
raise GMTInvalidInput(msg)
228229
case ext if ext not in fmts:
229-
raise GMTInvalidInput(f"Unknown extension '.{ext}'.")
230-
231-
fmt = fmts[ext]
232-
if transparent:
233-
if fmt != "g":
234-
msg = f"Transparency unavailable for '{ext}', only for png."
230+
msg = f"Unknown extension '.{ext}'."
235231
raise GMTInvalidInput(msg)
236-
fmt = fmt.upper()
232+
233+
if transparent and ext not in {"kml", "png"}:
234+
msg = f"Transparency unavailable for '{ext}', only for png and kml."
235+
raise GMTInvalidInput(msg)
237236
if anti_alias:
238237
kwargs["Qt"] = 2
239238
kwargs["Qg"] = 2
@@ -244,18 +243,17 @@ def savefig( # noqa: PLR0912
244243
raise GMTInvalidInput(msg)
245244
kwargs["W"] = True
246245

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

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

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

pygmt/tests/test_figure.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,21 @@ def test_figure_savefig_transparent():
196196
fname = f"{prefix}.{fmt}"
197197
with pytest.raises(GMTInvalidInput):
198198
fig.savefig(fname, transparent=True)
199-
# png should not raise an error
199+
200+
# PNG should support transparency and should not raise an error.
200201
fname = Path(f"{prefix}.png")
201202
fig.savefig(fname, transparent=True)
202203
assert fname.exists()
203204
fname.unlink()
204205

206+
# The companion PNG file with KML format should also support transparency.
207+
fname = Path(f"{prefix}.kml")
208+
fig.savefig(fname, transparent=True)
209+
assert fname.exists()
210+
fname.unlink()
211+
assert fname.with_suffix(".png").exists()
212+
fname.with_suffix(".png").unlink()
213+
205214

206215
def test_figure_savefig_filename_with_spaces():
207216
"""

0 commit comments

Comments
 (0)