From d8d5382a7a186f413d5cd9abd8bd00a566c757fc Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Tue, 13 Aug 2024 15:20:15 +0800 Subject: [PATCH 1/4] TYP: Add type hints to the Figure.savefig method --- pygmt/figure.py | 76 +++++++++++++++++++++++-------------------------- 1 file changed, 35 insertions(+), 41 deletions(-) diff --git a/pygmt/figure.py b/pygmt/figure.py index ce8693a43f3..d6ba39a709c 100644 --- a/pygmt/figure.py +++ b/pygmt/figure.py @@ -4,7 +4,7 @@ import base64 import os -from pathlib import Path +from pathlib import Path, PurePath from tempfile import TemporaryDirectory try: @@ -253,12 +253,12 @@ def psconvert(self, **kwargs): def savefig( # noqa: PLR0912 self, - fname, - transparent=False, - crop=True, - anti_alias=True, - show=False, - worldfile=False, + fname: str | PurePath, + transparent: bool = False, + crop: bool = True, + anti_alias: bool = True, + show: bool = False, + worldfile: bool = False, **kwargs, ): """ @@ -280,45 +280,39 @@ def savefig( # noqa: PLR0912 - EPS (``.eps``) - PDF (``.pdf``) - 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. + 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. + You can pass in any keyword arguments that :meth:`pygmt.Figure.psconvert` + accepts. Parameters ---------- - fname : str - The desired figure file name, including the extension. See the list - of supported formats and their extensions above. - transparent : bool - If ``True``, will use a transparent background for the figure. - Only valid for PNG format. - 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. - 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. - worldfile : bool - If ``True``, will create a companion - `world file `__ for the - figure. The world file will have the same name as the figure file - but with different extension (e.g. tfw for tif). See - https://en.wikipedia.org/wiki/World_file#Filename_extension - for the convention of world file extensions. This parameter only - works for raster image formats (except GeoTIFF). - dpi : int - Set raster resolution in dpi [Default is ``720`` for PDF, ``300`` - for others]. + fname + 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. + crop + Crop the figure canvas (page) to the plot area. + anti_alias + Use anti-aliasing when creating raster images. Ignored if creating vector + images. More specifically, it passes the arguments ``"t2"`` and ``"g2"`` to + the ``anti_aliasing`` parameter of :meth:`pygmt.Figure.psconvert`. + show + Display the figure in an external viewer. + worldfile + Create a companion `world file `__ + for the figure. The world file will have the same name as the figure file + but with different extension (e.g., ``.tfw`` for ``.tif``). See + https://en.wikipedia.org/wiki/World_file#Filename_extension for the + convention of world file extensions. This parameter only works for raster + image formats (except GeoTIFF). **kwargs : dict - Additional keyword arguments passed to - :meth:`pygmt.Figure.psconvert`. Valid parameters are ``gs_path``, - ``gs_option``, ``resize``, ``bb_style``, and ``verbose``. + Additional keyword arguments passed to :meth:`pygmt.Figure.psconvert`. Valid + parameters are ``dpi``, ``gs_path``, ``gs_option``, ``resize``, + ``bb_style``, and ``verbose``. """ # All supported formats fmts = { From 96452fb2d93cfc7234254c0702403c81e4675f2c Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 28 Aug 2024 13:53:31 +0800 Subject: [PATCH 2/4] Update pygmt/figure.py Co-authored-by: Michael Grund <23025878+michaelgrund@users.noreply.github.com> --- pygmt/figure.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/figure.py b/pygmt/figure.py index d6ba39a709c..382f117fd60 100644 --- a/pygmt/figure.py +++ b/pygmt/figure.py @@ -280,7 +280,7 @@ def savefig( # noqa: PLR0912 - EPS (``.eps``) - PDF (``.pdf``) - Beside the above formats, you can also save the figure to a KML file (``.kml``), + Besides 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. From 1d283f4c7b679ff68ba84559f2675b97076bd394 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 28 Aug 2024 14:10:46 +0800 Subject: [PATCH 3/4] Fix styling --- pygmt/figure.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pygmt/figure.py b/pygmt/figure.py index 382f117fd60..922a9fb59a7 100644 --- a/pygmt/figure.py +++ b/pygmt/figure.py @@ -280,9 +280,9 @@ def savefig( # noqa: PLR0912 - EPS (``.eps``) - PDF (``.pdf``) - Besides 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. + Besides 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. From 6088ea1c90bd5219871dedd38993806cd458e383 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 28 Aug 2024 14:15:34 +0800 Subject: [PATCH 4/4] Fix typing issue --- pygmt/figure.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/figure.py b/pygmt/figure.py index 922a9fb59a7..0509e765eb7 100644 --- a/pygmt/figure.py +++ b/pygmt/figure.py @@ -376,7 +376,7 @@ def savefig( # noqa: PLR0912 fname.with_suffix("." + ext).rename(fname) if show: - launch_external_viewer(fname) + launch_external_viewer(str(fname)) def show(self, dpi=300, width=500, method=None, waiting=0.5, **kwargs): """