Skip to content

Commit 38f91e1

Browse files
Figure.contour: Adjust processing of arguments passed to the "annotation" and "levels" parameters (#2706)
1 parent 0e630e3 commit 38f91e1

File tree

6 files changed

+105
-13
lines changed

6 files changed

+105
-13
lines changed

pygmt/datasets/earth_relief.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def load_earth_relief(
140140
land_only_srtm_resolutions = ["03s", "01s"]
141141

142142
# Map data source to prefix
143-
prefix = {
143+
prefix = {
144144
"igpp": "earth_relief",
145145
"gebco": "earth_gebco",
146146
"gebcosi": "earth_gebcosi",

pygmt/src/contour.py

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
"""
44

55
from pygmt.clib import Session
6-
from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias
6+
from pygmt.helpers import (
7+
build_arg_list,
8+
fmt_docstring,
9+
is_nonstr_iter,
10+
kwargs_to_strings,
11+
use_alias,
12+
)
713

814

915
@fmt_docstring
@@ -54,23 +60,26 @@ def contour(self, data=None, x=None, y=None, z=None, **kwargs):
5460
Arrays of x and y coordinates and values z of the data points.
5561
{projection}
5662
{region}
57-
annotation : str or int
63+
annotation : float, list, or str
5864
Specify or disable annotated contour levels, modifies annotated
5965
contours specified in ``levels``.
6066
61-
- Specify a fixed annotation interval *annot_int* or a
62-
single annotation level +\ *annot_int*.
67+
- Specify a fixed annotation interval.
68+
- Specify a list of annotation levels.
69+
- Disable all annotations by setting ``annotation="n"``.
70+
- Adjust the appearance by appending different modifiers, e.g.,
71+
``"annot_int+f10p+gred"`` gives annotations with a font size of 10 points and
72+
a red filled box. For all available modifiers see :gmt-docs:`contour.html#a`.
6373
{frame}
64-
levels : str or int
74+
levels : float, list, or str
6575
Specify the contour lines to generate.
6676
67-
- The file name of a CPT file where the color boundaries will
68-
be used as contour levels.
69-
- The file name of a 2 (or 3) column file containing the contour
70-
levels (col 1), (**C**)ontour or (**A**)nnotate (col 2), and optional
71-
angle (col 3).
72-
- A fixed contour interval *cont_int* or a single contour with
73-
+\ *cont_int*.
77+
- The file name of a CPT file where the color boundaries will be used as
78+
contour levels.
79+
- The file name of a 2 (or 3) column file containing the contour levels (col 0),
80+
(**C**)ontour or (**A**)nnotate (col 1), and optional angle (col 2).
81+
- A fixed contour interval.
82+
- A list of contour levels.
7483
D : str
7584
Dump contour coordinates.
7685
E : str
@@ -114,6 +123,17 @@ def contour(self, data=None, x=None, y=None, z=None, **kwargs):
114123
"""
115124
kwargs = self._preprocess(**kwargs)
116125

126+
# Specify levels for contours or annotations.
127+
# One level is converted to a string with a trailing comma to separate it from
128+
# specifying an interval.
129+
# Multiple levels are concatenated to a comma-separated string.
130+
for arg in ["A", "C"]:
131+
if is_nonstr_iter(kwargs.get(arg)):
132+
if len(kwargs[arg]) == 1: # One level
133+
kwargs[arg] = str(kwargs[arg][0]) + ","
134+
else: # Multiple levels
135+
kwargs[arg] = ",".join(f"{item}" for item in kwargs[arg])
136+
117137
with Session() as lib:
118138
with lib.virtualfile_in(
119139
check_kind="vector", data=data, x=x, y=y, z=z, required_z=True
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
outs:
2+
- md5: 44d70a0b17bc7c7939462184bf06e4da
3+
size: 50998
4+
isexec: true
5+
hash: md5
6+
path: test_contour_interval.png
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
outs:
2+
- md5: 7bef85a616c46b9f05f4dbed07bd703d
3+
size: 29247
4+
isexec: true
5+
hash: md5
6+
path: test_contour_multiple_levels.png
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
outs:
2+
- md5: 8c1ed221788e3af76279a7765640ea43
3+
size: 28882
4+
isexec: true
5+
hash: md5
6+
path: test_contour_one_level.png

pygmt/tests/test_contour.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,60 @@ def test_contour_from_file(region):
7575
return fig
7676

7777

78+
@pytest.mark.mpl_image_compare
79+
def test_contour_interval(region):
80+
"""
81+
Plot data with fixed (different) contour and annotation intervals.
82+
"""
83+
fig = Figure()
84+
fig.contour(
85+
data=POINTS_DATA,
86+
projection="X10c",
87+
region=region,
88+
frame="af",
89+
levels=0.1,
90+
annotation=0.2,
91+
pen=True,
92+
)
93+
return fig
94+
95+
96+
@pytest.mark.mpl_image_compare
97+
def test_contour_one_level(region):
98+
"""
99+
Plot data with one contour level and one (different) annotation level.
100+
"""
101+
fig = Figure()
102+
fig.contour(
103+
data=POINTS_DATA,
104+
projection="X10c",
105+
region=region,
106+
frame="af",
107+
levels=[0.4],
108+
annotation=[0.5],
109+
pen=True,
110+
)
111+
return fig
112+
113+
114+
@pytest.mark.mpl_image_compare
115+
def test_contour_multiple_levels(region):
116+
"""
117+
Plot data with multiple (different) contour and annotation levels.
118+
"""
119+
fig = Figure()
120+
fig.contour(
121+
data=POINTS_DATA,
122+
projection="X10c",
123+
region=region,
124+
frame="af",
125+
levels=[0.2, 0.3],
126+
annotation=[0.4, 0.45],
127+
pen=True,
128+
)
129+
return fig
130+
131+
78132
@pytest.mark.mpl_image_compare(filename="test_contour_vec.png")
79133
def test_contour_incols_transposed_data(region):
80134
"""

0 commit comments

Comments
 (0)