Skip to content

Commit 9947a96

Browse files
committed
Wrap colorbar
Initial commit for wrapping the colorbar function raised first at #109 but also #231, to be implemented under base_plotting.py alongside the other mapping related stuff. Original GMT `colorbar` documentation can be found at https://docs.generic-mapping-tools.org/latest/colorbar.html. Storing sample test cases under test_colorbar.py. Current implementation has an alias for position (D) which is arguably the most important one. and the one we've focused writing the tests for. Also wrapped around common aliases region (R), projection (J), frame (B) and cmap (C).
1 parent 5122397 commit 9947a96

8 files changed

+103
-0
lines changed

doc/api/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Plotting data and laying out the map:
2424

2525
Figure.basemap
2626
Figure.coast
27+
Figure.colorbar
2728
Figure.plot
2829
Figure.contour
2930
Figure.grdcontour

pygmt/base_plotting.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,49 @@ def coast(self, **kwargs):
125125
with Session() as lib:
126126
lib.call_module("coast", build_arg_string(kwargs))
127127

128+
@fmt_docstring
129+
@use_alias(R="region", J="projection", B="frame", C="cmap", D="position")
130+
@kwargs_to_strings()
131+
def colorbar(self, **kwargs):
132+
"""
133+
Plot a gray or color scale-bar on maps.
134+
135+
Both horizontal and vertical scales are supported. For CPTs with gradational
136+
colors (i.e., the lower and upper boundary of an interval have different colors)
137+
we will interpolate to give a continuous scale. Variations in intensity due to
138+
shading/illumination may be displayed by setting the option -I. Colors may be
139+
spaced according to a linear scale, all be equal size, or by providing a file
140+
with individual tile widths.
141+
142+
Full option list at :gmt-docs:`colorbar.html`
143+
144+
Parameters
145+
----------
146+
position (D) : str
147+
``[g|j|J|n|x]refpoint[+wlength[/width]][+e[b|f][length]][+h|v][+jjustify]
148+
[+m[a|c|l|u]][+n[txt]][+odx[/dy]]``.
149+
Defines the reference point on the map for the color scale using one of four
150+
coordinate systems:
151+
(1) Use -Dg for map (user) coordinates,
152+
(2) use -Dj or -DJ for setting refpoint via a 2-char justification code that
153+
refers to the (invisible) map domain rectangle,
154+
(3) use -Dn for normalized (0-1) coordinates, or
155+
(4) use -Dx for plot coordinates (inches, cm, etc.).\
156+
All but -Dx requires both -R and -J to be specified.
157+
Append +w followed by the length and width of the color bar.
158+
If width is not specified then it is set to 4% of the given length.
159+
Give a negative length to reverse the scale bar.
160+
Append +h to get a horizontal scale [Default is vertical (+v)].
161+
By default, the anchor point on the scale is assumed to be the bottom left
162+
corner (BL), but this can be changed by appending +j followed by a 2-char
163+
justification code justify.
164+
165+
{aliases}
166+
"""
167+
kwargs = self._preprocess(**kwargs)
168+
with Session() as lib:
169+
lib.call_module("colorbar", build_arg_string(kwargs))
170+
128171
@fmt_docstring
129172
@use_alias(
130173
A="annotation",
Loading
Loading
Loading
Loading
Loading

pygmt/tests/test_colorbar.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""
2+
Tests colorbar
3+
"""
4+
import pytest
5+
6+
from .. import Figure
7+
8+
9+
@pytest.mark.mpl_image_compare
10+
def test_colorbar_using_paper_coordinates():
11+
"""
12+
Create colorbar positioned at 0cm,0cm with length 1cm and width 0.5cm.
13+
"""
14+
fig = Figure()
15+
fig.colorbar(cmap="rainbow", position="x0c/0c+w1c/0.5c")
16+
return fig
17+
18+
19+
@pytest.mark.mpl_image_compare
20+
def test_colorbar_using_paper_coordinates_horizontal():
21+
"""
22+
Create colorbar positioned at 0cm,0cm with length 2cm oriented horizontally.
23+
"""
24+
fig = Figure()
25+
fig.colorbar(cmap="rainbow", position="x0c/0c+w2c+h")
26+
return fig
27+
28+
29+
@pytest.mark.mpl_image_compare
30+
def test_colorbar_positioned_using_map_coordinates():
31+
"""
32+
Create colorbar positioned at latitude,longitude 3,6 with length 2cm.
33+
"""
34+
fig = Figure()
35+
fig.basemap(region=[2, 4, 6, 8], projection="t0/2c", frame=True)
36+
fig.colorbar(cmap="rainbow", position="g3/6+w2c")
37+
return fig
38+
39+
40+
@pytest.mark.mpl_image_compare
41+
def test_colorbar_positioned_using_justification_code():
42+
"""
43+
Create colorbar positioned at Top Center inside the map frame with length 2cm.
44+
"""
45+
fig = Figure()
46+
fig.basemap(region=[2, 4, 6, 8], projection="t0/2c", frame=True)
47+
fig.colorbar(cmap="rainbow", position="jTC+w2c")
48+
return fig
49+
50+
51+
@pytest.mark.mpl_image_compare
52+
def test_colorbar_positioned_using_normalized_coords():
53+
"""
54+
Create colorbar positioned at normalized coordinates 0.75,0.25 with length 2cm.
55+
"""
56+
fig = Figure()
57+
fig.basemap(region=[2, 4, 6, 8], projection="t0/2c", frame=True)
58+
fig.colorbar(cmap="rainbow", position="n0.75/0.25+w2c")
59+
return fig

0 commit comments

Comments
 (0)