2
2
grdcontour - Plot a contour figure.
3
3
"""
4
4
5
+ import warnings
6
+
5
7
from pygmt .clib import Session
6
- from pygmt .helpers import build_arg_list , fmt_docstring , kwargs_to_strings , use_alias
8
+ from pygmt .helpers import (
9
+ build_arg_list ,
10
+ fmt_docstring ,
11
+ is_nonstr_iter ,
12
+ kwargs_to_strings ,
13
+ use_alias ,
14
+ )
7
15
8
16
__doctest_skip__ = ["grdcontour" ]
9
17
27
35
p = "perspective" ,
28
36
t = "transparency" ,
29
37
)
30
- @kwargs_to_strings (
31
- R = "sequence" , L = "sequence" , A = "sequence_plus" , c = "sequence_comma" , p = "sequence"
32
- )
38
+ @kwargs_to_strings (R = "sequence" , L = "sequence" , c = "sequence_comma" , p = "sequence" )
33
39
def grdcontour (self , grid , ** kwargs ):
34
40
r"""
35
41
Convert grids or images to contours and plot them on maps.
@@ -43,26 +49,26 @@ def grdcontour(self, grid, **kwargs):
43
49
Parameters
44
50
----------
45
51
{grid}
46
- interval : str or int
52
+ interval : float, list, or str
47
53
Specify the contour lines to generate.
48
54
49
- - The file name of a CPT file where the color boundaries will
50
- be used as contour levels.
51
- - The file name of a 2 (or 3) column file containing the contour
52
- levels (col 1), (**C**)ontour or (**A**)nnotate (col 2), and optional
53
- angle (col 3).
54
- - A fixed contour interval *cont_int* or a single contour with
55
- +\ *cont_int*.
56
- annotation : str, int, or list
55
+ - The file name of a CPT file where the color boundaries will be used as
56
+ contour levels.
57
+ - The file name of a 2 (or 3) column file containing the contour levels (col 0),
58
+ (**C**)ontour or (**A**)nnotate (col 1), and optional angle (col 2).
59
+ - A fixed contour interval.
60
+ - A list of contour levels.
61
+ annotation : float, list, or str
57
62
Specify or disable annotated contour levels, modifies annotated
58
63
contours specified in ``interval``.
59
64
60
- - Specify a fixed annotation interval *annot_int* or a
61
- single annotation level +\ *annot_int*.
62
- - Disable all annotation with **-**.
63
- - Optional label modifiers can be specified as a single string
64
- ``"[annot_int]+e"`` or with a list of arguments
65
- ``([annot_int], "e", "f10p", "gred")``.
65
+ - Specify a fixed annotation interval.
66
+ - Specify a list of annotation levels.
67
+ - Disable all annotations by setting ``annotation="n"``.
68
+ - Adjust the appearance by appending different modifiers, e.g.,
69
+ ``"annot_int+f10p+gred"`` gives annotations with a font size of 10 points
70
+ and a red filled box. For all available modifiers see
71
+ :gmt-docs:`grdcontour.html#a`.
66
72
limit : str or list of 2 ints
67
73
*low*/*high*.
68
74
Do no draw contours below `low` or above `high`, specify as string
@@ -96,32 +102,58 @@ def grdcontour(self, grid, **kwargs):
96
102
Example
97
103
-------
98
104
>>> import pygmt
99
- >>> # load the 15 arc-minutes grid with "gridline" registration
100
- >>> # in a specified region
105
+ >>> # Load the 15 arc-minutes grid with "gridline" registration in the
106
+ >>> # specified region
101
107
>>> grid = pygmt.datasets.load_earth_relief(
102
108
... resolution="15m",
103
109
... region=[-92.5, -82.5, -3, 7],
104
110
... registration="gridline",
105
111
... )
106
- >>> # create a new plot with pygmt.Figure()
112
+ >>> # Create a new plot with pygmt.Figure()
107
113
>>> fig = pygmt.Figure()
108
- >>> # create the contour plot
114
+ >>> # Create the contour plot
109
115
>>> fig.grdcontour(
110
- ... # pass in the grid downloaded above
116
+ ... # Pass in the grid downloaded above
111
117
... grid=grid,
112
- ... # set the interval for contour lines at 250 meters
118
+ ... # Set the interval for contour lines at 250 meters
113
119
... interval=250,
114
- ... # set the interval for annotated contour lines at 1,000 meters
120
+ ... # Set the interval for annotated contour lines at 1,000 meters
115
121
... annotation=1000,
116
- ... # add a frame for the plot
122
+ ... # Add a frame for the plot
117
123
... frame="a",
118
- ... # set the projection to Mercator for the 10 cm figure
124
+ ... # Set the projection to Mercator for the 10 cm figure
119
125
... projection="M10c",
120
126
... )
121
- >>> # show the plot
127
+ >>> # Show the plot
122
128
>>> fig.show()
123
129
"""
124
130
kwargs = self ._preprocess (** kwargs )
131
+
132
+ # Backward compatibility with the old syntax for the annotation parameter, e.g.,
133
+ # [100, "e", "f10p", "gred"].
134
+ if is_nonstr_iter (kwargs .get ("A" )) and any (
135
+ i [0 ] in "acdefgijlLnoprtuvwx=" for i in kwargs ["A" ] if isinstance (i , str )
136
+ ):
137
+ msg = (
138
+ "Argument of the parameter 'annotation'/'A' is using the old, deprecated "
139
+ "syntax. Please refer to the PyGMT documentation for the new syntax. "
140
+ "The warning will be removed in v0.14.0 and the old syntax will no longer "
141
+ "be supported. "
142
+ )
143
+ warnings .warn (msg , category = FutureWarning , stacklevel = 2 )
144
+ kwargs ["A" ] = "+" .join (f"{ item } " for item in kwargs ["A" ])
145
+
146
+ # Specify levels for the annotation and interval parameters.
147
+ # One level is converted to a string with a trailing comma to separate it from
148
+ # specifying an interval.
149
+ # Multiple levels are concatenated to a comma-separated string.
150
+ for arg in ["A" , "C" ]:
151
+ if is_nonstr_iter (kwargs .get (arg )):
152
+ if len (kwargs [arg ]) == 1 : # One level
153
+ kwargs [arg ] = str (kwargs [arg ][0 ]) + ","
154
+ else : # Multiple levels
155
+ kwargs [arg ] = "," .join (f"{ item } " for item in kwargs [arg ])
156
+
125
157
with Session () as lib :
126
158
with lib .virtualfile_in (check_kind = "raster" , data = grid ) as vingrd :
127
159
lib .call_module (
0 commit comments