Skip to content

Commit 6dbfad3

Browse files
authored
Add type hints to Figure._preview method and improve docstrings (#3491)
1 parent c471769 commit 6dbfad3

File tree

1 file changed

+20
-29
lines changed

1 file changed

+20
-29
lines changed

pygmt/figure.py

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
except ImportError:
1616
_HAS_IPYTHON = False
1717

18-
18+
import numpy as np
1919
from pygmt.clib import Session
2020
from pygmt.exceptions import GMTError, GMTInvalidInput
2121
from pygmt.helpers import (
@@ -75,12 +75,9 @@ class Figure:
7575
"""
7676
A GMT figure to handle all plotting.
7777
78-
Use the plotting methods of this class to add elements to the figure. You
79-
can preview the figure using :meth:`pygmt.Figure.show` and save the figure
80-
to a file using :meth:`pygmt.Figure.savefig`.
81-
82-
Unlike traditional GMT figures, no figure file is generated until you call
83-
:meth:`pygmt.Figure.savefig` or :meth:`pygmt.Figure.psconvert`.
78+
Use the plotting methods of this class to add elements to the figure. You can
79+
preview the figure using :meth:`pygmt.Figure.show` and save the figure to a file
80+
using :meth:`pygmt.Figure.savefig`.
8481
8582
Examples
8683
--------
@@ -94,8 +91,8 @@ class Figure:
9491
>>> assert Path("my-figure.png").exists()
9592
>>> Path("my-figure.png").unlink()
9693
97-
The plot region can be specified through ISO country codes (for example,
98-
``"JP"`` for Japan):
94+
The plot region can be specified through ISO country codes (for example, ``"JP"``
95+
for Japan):
9996
10097
>>> import pygmt
10198
>>> fig = pygmt.Figure()
@@ -122,14 +119,8 @@ def _activate_figure(self):
122119
Start and/or activate the current figure.
123120
124121
All plotting commands run afterward will append to this figure.
125-
126-
Unlike the command-line version (``gmt figure``), this method does not
127-
trigger the generation of a figure file. An explicit call to
128-
:meth:`pygmt.Figure.savefig` or :meth:`pygmt.Figure.psconvert` must be
129-
made in order to get a file.
130122
"""
131-
# Passing format '-' tells pygmt.end to not produce any files.
132-
fmt = "-"
123+
fmt = "-" # Passing format "-" tells pygmt.end to not produce any files.
133124
with Session() as lib:
134125
lib.call_module(module="figure", args=[self._name, fmt])
135126

@@ -142,7 +133,7 @@ def _preprocess(self, **kwargs):
142133
return kwargs
143134

144135
@property
145-
def region(self):
136+
def region(self) -> np.ndarray:
146137
"""
147138
The geographic WESN bounding box for the current figure.
148139
"""
@@ -480,7 +471,7 @@ def show(
480471
pdf = self._preview(
481472
fmt="pdf", dpi=dpi, anti_alias=False, as_bytes=False, **kwargs
482473
)
483-
launch_external_viewer(pdf, waiting=waiting)
474+
launch_external_viewer(pdf, waiting=waiting) # type: ignore[arg-type]
484475
case "none":
485476
pass # Do nothing
486477
case _:
@@ -489,26 +480,26 @@ def show(
489480
"'notebook', 'none' or None."
490481
)
491482

492-
def _preview(self, fmt, dpi, as_bytes=False, **kwargs):
483+
def _preview(self, fmt: str, dpi: int, as_bytes: bool = False, **kwargs):
493484
"""
494485
Grab a preview of the figure.
495486
496487
Parameters
497488
----------
498-
fmt : str
499-
The image format. Can be any extension that
500-
:meth:`pygmt.Figure.savefig` recognizes.
501-
dpi : int
489+
fmt
490+
The image format. Can be any extension that :meth:`pygmt.Figure.savefig`
491+
recognizes.
492+
dpi
502493
The image resolution (dots per inch).
503-
as_bytes : bool
504-
If ``True``, will load the image as a bytes string and return that
505-
instead of the file name.
494+
as_bytes
495+
If ``True``, will load the binary contents of the image as a bytes object,
496+
and return that instead of the file name.
506497
507498
Returns
508499
-------
509-
preview : str or bytes
510-
If ``as_bytes=False``, this is the file name of the preview image
511-
file. Else, it is the file content loaded as a bytes string.
500+
preview
501+
If ``as_bytes = False``, this is the file name of the preview image file.
502+
Otherwise, it is the file content loaded as a bytes object.
512503
"""
513504
fname = Path(self._preview_dir.name) / f"{self._name}.{fmt}"
514505
self.savefig(fname, dpi=dpi, **kwargs)

0 commit comments

Comments
 (0)