Skip to content

Commit

Permalink
Added identity_pal
Browse files Browse the repository at this point in the history
closes #9
  • Loading branch information
has2k1 committed Jan 17, 2018
1 parent d5e71b5 commit 01282af
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
2 changes: 2 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ v0.4.5
------
*(not-yet-released)*

- Added :class:`~mizani.palettes.identity_pal`

v0.4.4
------
*(2017-12-13)*
Expand Down
22 changes: 22 additions & 0 deletions mizani/palettes.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

from .external import husl, xkcd_rgb, crayon_rgb
from .bounds import rescale
from .utils import identity


__all__ = ['hls_palette', 'husl_palette', 'rescale_pal',
Expand Down Expand Up @@ -743,3 +744,24 @@ def cubehelix_palette(n):
return [mcolors.rgb2hex(cubehelix_cmap(x)) for x in values]

return cubehelix_palette


def identity_pal():
"""
Create palette that maps values onto themselves
Returns
-------
out : function
Palette function that takes a value or sequence of values
and returns the same values.
Examples
--------
>>> palette = identity_pal()
>>> palette(9)
9
>>> palette([2, 4, 6])
[2, 4, 6]
"""
return identity
13 changes: 12 additions & 1 deletion mizani/tests/test_palettes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
area_pal, abs_area, grey_pal, hue_pal,
brewer_pal, gradient_n_pal, cmap_pal,
desaturate_pal, manual_pal, xkcd_palette,
crayon_palette, cubehelix_pal)
crayon_palette, cubehelix_pal, identity_pal)


def test_hls_palette():
Expand Down Expand Up @@ -169,3 +169,14 @@ def test_cubehelix_pal():
values = palette(5)
assert len(values) == 5
assert all(s[0] == '#' and len(s) == 7 for s in values)


def test_identity_pal():
palette = identity_pal()

x = [1, 2, 3]
values = palette(x)
assert values == x

value = palette(10)
assert value == 10
11 changes: 10 additions & 1 deletion mizani/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

__all__ = ['seq', 'fullseq', 'round_any', 'min_max', 'match',
'precision', 'first_element', 'multitype_sort',
'is_close_to_int', 'same_log10_order_of_magnitude']
'is_close_to_int', 'same_log10_order_of_magnitude',
'identity'
]

DISCRETE_KINDS = 'ObUS'
CONTINUOUS_KINDS = 'ifuc'
Expand Down Expand Up @@ -325,3 +327,10 @@ def same_log10_order_of_magnitude(rng, delta=0.045):
"""
rng_adjusted = np.array(rng) + [-delta, +delta]
return np.diff(rng_adjusted.astype(int))[0] == 0


def identity(*args):
"""
Return whatever is passed in
"""
return args if len(args) > 1 else args[0]

0 comments on commit 01282af

Please sign in to comment.