22grdcontour - Plot a contour figure.
33"""
44
5+ import warnings
6+
57from 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+ )
715
816__doctest_skip__ = ["grdcontour" ]
917
2735 p = "perspective" ,
2836 t = "transparency" ,
2937)
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" )
3339def grdcontour (self , grid , ** kwargs ):
3440 r"""
3541 Convert grids or images to contours and plot them on maps.
@@ -43,26 +49,26 @@ def grdcontour(self, grid, **kwargs):
4349 Parameters
4450 ----------
4551 {grid}
46- interval : str or int
52+ interval : float, list, or str
4753 Specify the contour lines to generate.
4854
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
5762 Specify or disable annotated contour levels, modifies annotated
5863 contours specified in ``interval``.
5964
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`.
6672 limit : str or list of 2 ints
6773 *low*/*high*.
6874 Do no draw contours below `low` or above `high`, specify as string
@@ -96,32 +102,58 @@ def grdcontour(self, grid, **kwargs):
96102 Example
97103 -------
98104 >>> 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
101107 >>> grid = pygmt.datasets.load_earth_relief(
102108 ... resolution="15m",
103109 ... region=[-92.5, -82.5, -3, 7],
104110 ... registration="gridline",
105111 ... )
106- >>> # create a new plot with pygmt.Figure()
112+ >>> # Create a new plot with pygmt.Figure()
107113 >>> fig = pygmt.Figure()
108- >>> # create the contour plot
114+ >>> # Create the contour plot
109115 >>> fig.grdcontour(
110- ... # pass in the grid downloaded above
116+ ... # Pass in the grid downloaded above
111117 ... grid=grid,
112- ... # set the interval for contour lines at 250 meters
118+ ... # Set the interval for contour lines at 250 meters
113119 ... 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
115121 ... annotation=1000,
116- ... # add a frame for the plot
122+ ... # Add a frame for the plot
117123 ... frame="a",
118- ... # set the projection to Mercator for the 10 cm figure
124+ ... # Set the projection to Mercator for the 10 cm figure
119125 ... projection="M10c",
120126 ... )
121- >>> # show the plot
127+ >>> # Show the plot
122128 >>> fig.show()
123129 """
124130 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+
125157 with Session () as lib :
126158 with lib .virtualfile_in (check_kind = "raster" , data = grid ) as vingrd :
127159 lib .call_module (
0 commit comments