@@ -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
0 commit comments