Skip to content

Commit 540ac0f

Browse files
willschlitzerweiji14seisman
authored
Move plotting functions to separate modules (GenericMappingTools#808)
*Move plotting functions from base_plotting.py to separate functions in pygmt/src *Add plotting function imports to pygmt/src/__init__.py *Import plotting functions directly into figure.py *Remove base_plotting.py Co-authored-by: Wei Ji <weiji.leong@vuw.ac.nz> Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
1 parent d4cb9ea commit 540ac0f

17 files changed

+1788
-1647
lines changed

pygmt/base_plotting.py

Lines changed: 0 additions & 1645 deletions
This file was deleted.

pygmt/figure.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
except ImportError:
1111
Image = None
1212

13-
from pygmt.base_plotting import BasePlotting
1413
from pygmt.clib import Session
1514
from pygmt.exceptions import GMTError, GMTInvalidInput
1615
from pygmt.helpers import (
@@ -27,7 +26,7 @@
2726
SHOWED_FIGURES = []
2827

2928

30-
class Figure(BasePlotting):
29+
class Figure:
3130
"""
3231
A GMT figure to handle all plotting.
3332
@@ -372,3 +371,21 @@ def _repr_html_(self):
372371
base64_png = base64.encodebytes(raw_png)
373372
html = '<img src="data:image/png;base64,{image}" width="{width}px">'
374373
return html.format(image=base64_png.decode("utf-8"), width=500)
374+
375+
from pygmt.src import ( # pylint: disable=import-outside-toplevel
376+
basemap,
377+
coast,
378+
colorbar,
379+
contour,
380+
grdcontour,
381+
grdimage,
382+
grdview,
383+
image,
384+
inset,
385+
legend,
386+
logo,
387+
meca,
388+
plot,
389+
plot3d,
390+
text,
391+
)

pygmt/src/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,26 @@
22
Source code for PyGMT modules.
33
"""
44
# pylint: disable=import-outside-toplevel
5+
from pygmt.src.basemap import basemap
56
from pygmt.src.blockmedian import blockmedian
7+
from pygmt.src.coast import coast
8+
from pygmt.src.colorbar import colorbar
9+
from pygmt.src.contour import contour
10+
from pygmt.src.grdcontour import grdcontour
611
from pygmt.src.grdcut import grdcut
712
from pygmt.src.grdfilter import grdfilter
13+
from pygmt.src.grdimage import grdimage
814
from pygmt.src.grdtrack import grdtrack
15+
from pygmt.src.grdview import grdview
16+
from pygmt.src.image import image
917
from pygmt.src.info import info
1018
from pygmt.src.inset import inset
19+
from pygmt.src.legend import legend
20+
from pygmt.src.logo import logo
1121
from pygmt.src.makecpt import makecpt
1222
from pygmt.src.meca import meca
23+
from pygmt.src.plot import plot
24+
from pygmt.src.plot3d import plot3d
1325
from pygmt.src.surface import surface
26+
from pygmt.src.text import text_ as text # "text" is an argument within "text_"
1427
from pygmt.src.which import which

pygmt/src/basemap.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"""
2+
basemap - Plot base maps and frames for the figure.
3+
"""
4+
5+
from pygmt.clib import Session
6+
from pygmt.exceptions import GMTInvalidInput
7+
from pygmt.helpers import (
8+
args_in_kwargs,
9+
build_arg_string,
10+
fmt_docstring,
11+
kwargs_to_strings,
12+
use_alias,
13+
)
14+
15+
16+
@fmt_docstring
17+
@use_alias(
18+
R="region",
19+
J="projection",
20+
Jz="zscale",
21+
JZ="zsize",
22+
B="frame",
23+
L="map_scale",
24+
Td="rose",
25+
Tm="compass",
26+
U="timestamp",
27+
V="verbose",
28+
X="xshift",
29+
Y="yshift",
30+
p="perspective",
31+
t="transparency",
32+
)
33+
@kwargs_to_strings(R="sequence", p="sequence")
34+
def basemap(self, **kwargs):
35+
"""
36+
Plot base maps and frames for the figure.
37+
38+
Creates a basic or fancy basemap with axes, fill, and titles. Several
39+
map projections are available, and the user may specify separate
40+
tick-mark intervals for boundary annotation, ticking, and [optionally]
41+
gridlines. A simple map scale or directional rose may also be plotted.
42+
43+
At least one of the options *frame*, *map_scale*, *rose* or *compass*
44+
must be specified.
45+
46+
Full option list at :gmt-docs:`basemap.html`
47+
48+
{aliases}
49+
50+
Parameters
51+
----------
52+
{J}
53+
zscale/zsize : float or str
54+
Set z-axis scaling or z-axis size.
55+
{R}
56+
{B}
57+
map_scale : str
58+
``'[g|j|J|n|x]refpoint'``
59+
Draws a simple map scale centered on the reference point specified.
60+
rose : str
61+
Draws a map directional rose on the map at the location defined by
62+
the reference and anchor points.
63+
compass : str
64+
Draws a map magnetic rose on the map at the location defined by the
65+
reference and anchor points
66+
{U}
67+
{V}
68+
{XY}
69+
{p}
70+
{t}
71+
"""
72+
kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access
73+
if not args_in_kwargs(args=["B", "L", "Td", "Tm"], kwargs=kwargs):
74+
raise GMTInvalidInput(
75+
"At least one of frame, map_scale, compass, or rose must be specified."
76+
)
77+
with Session() as lib:
78+
lib.call_module("basemap", build_arg_string(kwargs))

pygmt/src/coast.py

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
"""
2+
coast - Plot land and water.
3+
"""
4+
5+
from pygmt.clib import Session
6+
from pygmt.exceptions import GMTInvalidInput
7+
from pygmt.helpers import (
8+
args_in_kwargs,
9+
build_arg_string,
10+
fmt_docstring,
11+
kwargs_to_strings,
12+
use_alias,
13+
)
14+
15+
16+
@fmt_docstring
17+
@use_alias(
18+
R="region",
19+
J="projection",
20+
A="area_thresh",
21+
C="lakes",
22+
B="frame",
23+
D="resolution",
24+
E="dcw",
25+
I="rivers",
26+
L="map_scale",
27+
N="borders",
28+
W="shorelines",
29+
G="land",
30+
S="water",
31+
U="timestamp",
32+
V="verbose",
33+
X="xshift",
34+
Y="yshift",
35+
p="perspective",
36+
t="transparency",
37+
)
38+
@kwargs_to_strings(R="sequence", p="sequence")
39+
def coast(self, **kwargs):
40+
r"""
41+
Plot continents, shorelines, rivers, and borders on maps
42+
43+
Plots grayshaded, colored, or textured land-masses [or water-masses] on
44+
maps and [optionally] draws coastlines, rivers, and political
45+
boundaries. Alternatively, it can (1) issue clip paths that will
46+
contain all land or all water areas, or (2) dump the data to an ASCII
47+
table. The data files come in 5 different resolutions: (**f**)ull,
48+
(**h**)igh, (**i**)ntermediate, (**l**)ow, and (**c**)rude. The full
49+
resolution files amount to more than 55 Mb of data and provide great
50+
detail; for maps of larger geographical extent it is more economical to
51+
use one of the other resolutions. If the user selects to paint the
52+
land-areas and does not specify fill of water-areas then the latter
53+
will be transparent (i.e., earlier graphics drawn in those areas will
54+
not be overwritten). Likewise, if the water-areas are painted and no
55+
land fill is set then the land-areas will be transparent.
56+
57+
A map projection must be supplied.
58+
59+
Full option list at :gmt-docs:`coast.html`
60+
61+
{aliases}
62+
63+
Parameters
64+
----------
65+
{J}
66+
{R}
67+
area_thresh : int, float, or str
68+
*min_area*\ [/*min_level*/*max_level*][**+ag**\|\ **i**\
69+
\|\ **s**\|\ **S**][**+r**\|\ **l**][**+p**\
70+
*percent*].
71+
Features with an area smaller than min_area in km^2 or of
72+
hierarchical level that is lower than min_level or higher than
73+
max_level will not be plotted.
74+
{B}
75+
lakes : str or list
76+
*fill*\ [**+l**\|\ **+r**].
77+
Set the shade, color, or pattern for lakes and river-lakes. The
78+
default is the fill chosen for wet areas set by the ``water``
79+
argument. Optionally, specify separate fills by appending
80+
**+l** for lakes or **+r** for river-lakes, and passing multiple
81+
strings in a list.
82+
resolution : str
83+
**f**\|\ **h**\|\ **i**\|\ **l**\|\ **c**.
84+
Selects the resolution of the data set to: (**f**\ )ull,
85+
(**h**\ )igh, (**i**\ )ntermediate, (**l**\ )ow,
86+
and (**c**\ )rude.
87+
land : str
88+
Select filling or clipping of “dry” areas.
89+
rivers : int or str or list
90+
*river*\ [/*pen*].
91+
Draw rivers. Specify the type of rivers and [optionally] append
92+
pen attributes [Default pen: width = default, color = black,
93+
style = solid].
94+
95+
Choose from the list of river types below; pass a list to
96+
``rivers`` to use multiple arguments.
97+
98+
0 = Double-lined rivers (river-lakes)
99+
100+
1 = Permanent major rivers
101+
102+
2 = Additional major rivers
103+
104+
3 = Additional rivers
105+
106+
4 = Minor rivers
107+
108+
5 = Intermittent rivers - major
109+
110+
6 = Intermittent rivers - additional
111+
112+
7 = Intermittent rivers - minor
113+
114+
8 = Major canals
115+
116+
9 = Minor canals
117+
118+
10 = Irrigation canals
119+
120+
You can also choose from several preconfigured river groups:
121+
122+
a = All rivers and canals (0-10)
123+
124+
A = All rivers and canals except river-lakes (1-10)
125+
126+
r = All permanent rivers (0-4)
127+
128+
R = All permanent rivers except river-lakes (1-4)
129+
130+
i = All intermittent rivers (5-7)
131+
132+
c = All canals (8-10)
133+
map_scale : str
134+
[**g**\|\ **j**\|\ **J**\|\ **n**\|\ **x**]\ *refpoint*.
135+
Draws a simple map scale centered on the reference point specified.
136+
borders : int or str or list
137+
*border*\ [/*pen*].
138+
Draw political boundaries. Specify the type of boundary and
139+
[optionally] append pen attributes [Default pen: width = default,
140+
color = black, style = solid].
141+
142+
Choose from the list of boundaries below. Pass a list to
143+
``borders`` to use multiple arguments.
144+
145+
1 = National boundaries
146+
147+
2 = State boundaries within the Americas
148+
149+
3 = Marine boundaries
150+
151+
a = All boundaries (1-3)
152+
water : str
153+
Select filling or clipping of “wet” areas.
154+
{U}
155+
shorelines : int or str or list
156+
[*level*\ /]\ *pen*.
157+
Draw shorelines [Default is no shorelines]. Append pen attributes
158+
[Defaults: width = default, color = black, style = solid] which
159+
apply to all four levels. To set the pen for a single level,
160+
pass a string with *level*\ /*pen*\ , where level is
161+
1-4 and represent coastline, lakeshore, island-in-lake shore, and
162+
lake-in-island-in-lake shore. Pass a list of *level*\ /*pen*
163+
strings to ``shorelines`` to set multiple levels. When specific
164+
level pens are set, those not listed will not be drawn.
165+
dcw : str or list
166+
*code1,code2,…*\ [**+l**\|\ **L**\ ][**+g**\ *fill*\ ]
167+
[**+p**\ *pen*\ ][**+z**].
168+
Select painting or dumping country polygons from the
169+
`Digital Chart of the World
170+
<https://en.wikipedia.org/wiki/Digital_Chart_of_the_World>`__.
171+
Append one or more comma-separated countries using the 2-character
172+
`ISO 3166-1 alpha-2 convention
173+
<https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2>`__.
174+
To select a state of a country (if available), append
175+
.\ *state*, (e.g, US.TX for Texas). To specify a whole continent,
176+
prepend **=** to any of the continent codes (e.g. =EU for Europe).
177+
Append **+p**\ *pen* to draw polygon outlines
178+
(default is no outline) and **+g**\ *fill* to fill them
179+
(default is no fill). Append **+l**\|\ **+L** to *=continent* to
180+
only list countries in that continent; repeat if more than one
181+
continent is requested. Append **+z** to place the country code in
182+
the segment headers via **-Z**\ *code* settings.To apply different
183+
settings to different countries, pass a list of string arguments.
184+
{XY}
185+
{p}
186+
{t}
187+
{V}
188+
"""
189+
kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access
190+
if not args_in_kwargs(args=["C", "G", "S", "I", "N", "Q", "W"], kwargs=kwargs):
191+
raise GMTInvalidInput(
192+
"""At least one of the following arguments must be specified:
193+
lakes, land, water, rivers, borders, Q, or shorelines"""
194+
)
195+
with Session() as lib:
196+
lib.call_module("coast", build_arg_string(kwargs))

0 commit comments

Comments
 (0)