Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Figure.contour: Adjust processing of arguments passed to the "annotation" and "levels" parameters #2706

Merged
merged 28 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
0a3e77d
Fix input types for A and C
yvonnefroehlich Sep 30, 2023
066ab45
Use always 'or'
yvonnefroehlich Sep 30, 2023
64c3cf7
Merge branch 'main' into fix-input-contour-AC
yvonnefroehlich Oct 2, 2023
5c15f75
Merge branch 'main' into fix-input-contour-AC
yvonnefroehlich Oct 4, 2023
a5f8a81
Remove second tilemap example
yvonnefroehlich Oct 4, 2023
aa7180e
Add second code example back with zoom=13
yvonnefroehlich Oct 4, 2023
03f1599
Run second code example with zoom=15
yvonnefroehlich Oct 4, 2023
ea3e74c
Revert changes for tilemap gallery example
yvonnefroehlich Oct 5, 2023
2330067
Merge branch 'main' into fix-input-contour-AC
yvonnefroehlich Oct 5, 2023
eea2341
Merge branch 'main' into fix-input-contour-AC
yvonnefroehlich Oct 17, 2023
177ae86
Merge branch 'main' into fix-input-contour-AC
yvonnefroehlich Nov 2, 2023
e0c0a28
Merge branch 'main' into fix-input-contour-AC
yvonnefroehlich Nov 11, 2023
88a643e
Merge branch 'main' into fix-input-contour-AC
yvonnefroehlich Dec 5, 2023
b55030c
Merge branch 'main' into fix-input-contour-AC
yvonnefroehlich Jan 5, 2024
a5051a2
Merge branch 'main' into fix-input-contour-AC
yvonnefroehlich Mar 14, 2024
6756c43
Merge branch 'main' into fix-input-contour-AC
yvonnefroehlich Mar 20, 2024
991252a
Merge branch 'main' into fix-input-contour-AC
yvonnefroehlich Apr 26, 2024
5788d6b
Add docs and code for 'A' and 'C' processing
yvonnefroehlich Apr 26, 2024
4e74984
Add 'is_nonstr_iter' from helpers
yvonnefroehlich Apr 26, 2024
640b274
Add tests
yvonnefroehlich Apr 26, 2024
76a6031
Add test_contour_interval.png into DVC
yvonnefroehlich Apr 26, 2024
3741d82
Add test_contour_one_level.png into DVC
yvonnefroehlich Apr 26, 2024
65708eb
Add test_contour_multiple_levels.png into DVC
yvonnefroehlich Apr 26, 2024
cb4dc4d
[format-command] fixes
actions-bot Apr 26, 2024
490ee65
Revert change in 'grdcontour'
yvonnefroehlich Apr 26, 2024
2b97d42
Merge branch 'main' into fix-input-contour-AC
yvonnefroehlich Apr 27, 2024
2d64613
Merge branch 'main' into fix-input-contour-AC
seisman Apr 28, 2024
6c24c49
[format-command] fixes
actions-bot Apr 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pygmt/datasets/earth_relief.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def load_earth_relief(
land_only_srtm_resolutions = ["03s", "01s"]

# Map data source to prefix
prefix = {
prefix = {
"igpp": "earth_relief",
"gebco": "earth_gebco",
"gebcosi": "earth_gebcosi",
Expand Down
44 changes: 32 additions & 12 deletions pygmt/src/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
"""

from pygmt.clib import Session
from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias
from pygmt.helpers import (
build_arg_list,
fmt_docstring,
is_nonstr_iter,
kwargs_to_strings,
use_alias,
)


@fmt_docstring
Expand Down Expand Up @@ -54,23 +60,26 @@ def contour(self, data=None, x=None, y=None, z=None, **kwargs):
Arrays of x and y coordinates and values z of the data points.
{projection}
{region}
annotation : str or int
annotation : float, list, or str
Specify or disable annotated contour levels, modifies annotated
contours specified in ``levels``.

- Specify a fixed annotation interval *annot_int* or a
single annotation level +\ *annot_int*.
- Specify a fixed annotation interval.
- Specify a list of annotation levels.
- Disable all annotations by setting ``annotation="n"``.
- Adjust the appearance by appending different modifiers, e.g.,
``"annot_int+f10p+gred"`` gives annotations with a font size of 10 points and
a red filled box. For all available modifiers see :gmt-docs:`contour.html#a`.
{frame}
levels : str or int
levels : float, list, or str
Specify the contour lines to generate.

- The file name of a CPT file where the color boundaries will
be used as contour levels.
- The file name of a 2 (or 3) column file containing the contour
levels (col 1), (**C**)ontour or (**A**)nnotate (col 2), and optional
angle (col 3).
- A fixed contour interval *cont_int* or a single contour with
+\ *cont_int*.
- The file name of a CPT file where the color boundaries will be used as
contour levels.
- The file name of a 2 (or 3) column file containing the contour levels (col 0),
(**C**)ontour or (**A**)nnotate (col 1), and optional angle (col 2).
- A fixed contour interval.
- A list of contour levels.
D : str
Dump contour coordinates.
E : str
Expand Down Expand Up @@ -114,6 +123,17 @@ def contour(self, data=None, x=None, y=None, z=None, **kwargs):
"""
kwargs = self._preprocess(**kwargs)

# Specify levels for contours or annotations.
# One level is converted to a string with a trailing comma to separate it from
# specifying an interval.
# Multiple levels are concatenated to a comma-separated string.
for arg in ["A", "C"]:
if is_nonstr_iter(kwargs.get(arg)):
if len(kwargs[arg]) == 1: # One level
kwargs[arg] = str(kwargs[arg][0]) + ","
else: # Multiple levels
kwargs[arg] = ",".join(f"{item}" for item in kwargs[arg])

with Session() as lib:
with lib.virtualfile_in(
check_kind="vector", data=data, x=x, y=y, z=z, required_z=True
Expand Down
6 changes: 6 additions & 0 deletions pygmt/tests/baseline/test_contour_interval.png.dvc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
outs:
- md5: 44d70a0b17bc7c7939462184bf06e4da
size: 50998
isexec: true
hash: md5
path: test_contour_interval.png
6 changes: 6 additions & 0 deletions pygmt/tests/baseline/test_contour_multiple_levels.png.dvc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
outs:
- md5: 7bef85a616c46b9f05f4dbed07bd703d
size: 29247
isexec: true
hash: md5
path: test_contour_multiple_levels.png
6 changes: 6 additions & 0 deletions pygmt/tests/baseline/test_contour_one_level.png.dvc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
outs:
- md5: 8c1ed221788e3af76279a7765640ea43
size: 28882
isexec: true
hash: md5
path: test_contour_one_level.png
54 changes: 54 additions & 0 deletions pygmt/tests/test_contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,60 @@ def test_contour_from_file(region):
return fig


@pytest.mark.mpl_image_compare
def test_contour_interval(region):
"""
Plot data with fixed (different) contour and annotation intervals.
"""
fig = Figure()
fig.contour(
data=POINTS_DATA,
projection="X10c",
region=region,
frame="af",
levels=0.1,
annotation=0.2,
pen=True,
)
return fig


@pytest.mark.mpl_image_compare
def test_contour_one_level(region):
"""
Plot data with one contour level and one (different) annotation level.
"""
fig = Figure()
fig.contour(
data=POINTS_DATA,
projection="X10c",
region=region,
frame="af",
levels=[0.4],
annotation=[0.5],
pen=True,
)
return fig


@pytest.mark.mpl_image_compare
def test_contour_multiple_levels(region):
"""
Plot data with multiple (different) contour and annotation levels.
"""
fig = Figure()
fig.contour(
data=POINTS_DATA,
projection="X10c",
region=region,
frame="af",
levels=[0.2, 0.3],
annotation=[0.4, 0.45],
pen=True,
)
return fig


@pytest.mark.mpl_image_compare(filename="test_contour_vec.png")
def test_contour_incols_transposed_data(region):
"""
Expand Down
Loading