Skip to content
Prev Previous commit
Next Next commit
Alias cyclic (Ww) and ensure this cannot be used with categorical (W)
  • Loading branch information
weiji14 committed Nov 2, 2020
commit be5141cf0d0efe6344e34e30305d76ece37d2116
14 changes: 9 additions & 5 deletions pygmt/mathops.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
T="series",
V="verbose",
W="categorical",
Ww="cyclic",
Z="continuous",
)
@kwargs_to_strings(T="sequence", G="sequence")
Expand Down Expand Up @@ -135,16 +136,19 @@ def makecpt(**kwargs):
Force a continuous CPT when building from a list of colors and a list
of z-values [Default is None, i.e. discrete values].
{V}
categorical : bool or str
categorical : bool
Do not interpolate the input color table but pick the output colors
starting at the beginning of the color table, until colors for all
intervals are assigned. This is particularly useful in combination with
a categorical color table, like "categorical". Alternatively, use
``categorical='w'`` to produce a wrapped (cyclic) color table that
endlessly repeats its range.

a categorical color table, like ``cmap='categorical'``.
cyclic : bool
Produce a wrapped (cyclic) color table that endlessly repeats its
range. Note that ``cyclic=True`` cannot be set together with
``categorical=True``.
"""
with Session() as lib:
if "W" in kwargs and "Ww" in kwargs:
raise GMTInvalidInput("Set only categorical or cyclic to True, not both.")
if "H" not in kwargs.keys(): # if no output is set
arg_str = build_arg_string(kwargs)
elif "H" in kwargs.keys(): # if output is set
Expand Down
38 changes: 38 additions & 0 deletions pygmt/tests/test_makecpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,41 @@ def test_makecpt_continuous(grid):
makecpt(cmap="blue,white", continuous=True, series="-4500,4500")
fig.grdimage(grid, projection="W0/6i")
return fig


@check_figures_equal()
def test_makecpt_categorical(region):
"""
Use static color palette table that is categorical.
"""
fig_ref = Figure()
makecpt(C="categorical", W="")
fig_ref.colorbar(cmap=True, region=region, frame=True, position="JBC")

fig_test = Figure()
makecpt(cmap="categorical", categorical=True)
fig_test.colorbar(cmap=True, region=region, frame=True, position="JBC")
return fig_ref, fig_test


@check_figures_equal()
def test_makecpt_cyclic(region):
"""
Use static color palette table that is cyclic.
"""
fig_ref = Figure()
makecpt(C="cork", W="w")
fig_ref.colorbar(cmap=True, region=region, frame=True, position="JBC")

fig_test = Figure()
makecpt(cmap="cork", cyclic=True)
fig_test.colorbar(cmap=True, region=region, frame=True, position="JBC")
return fig_ref, fig_test


def test_makecpt_categorical_and_cyclic():
"""
Use incorrect setting by setting both categorical and cyclic to True.
"""
with pytest.raises(GMTInvalidInput):
makecpt(cmap="batlow", categorical=True, cyclic=True)