/usr/lib/python3/dist-packages/photutils/utils/colormaps.py is in python3-photutils 0.3-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | # Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import numpy as np
from .check_random_state import check_random_state
__all__ = ['random_cmap']
def random_cmap(ncolors=256, background_color='black', random_state=None):
"""
Generate a matplotlib colormap consisting of random (muted) colors.
A random colormap is very useful for plotting segmentation images.
Parameters
----------
ncolors : int, optional
The number of colors in the colormap. For use with segmentation
images, ``ncolors`` should be set to the number of labels. The
default is 256.
background_color : str, optional
The name of the background (first) color in the colormap. Valid
colors names are defined by ``matplotlib.colors.cnames``. The
default is ``'black'``.
random_state : int or `~numpy.random.RandomState`, optional
The pseudo-random number generator state used for random
sampling. Separate function calls with the same
``random_state`` will generate the same colormap.
Returns
-------
cmap : `matplotlib.colors.Colormap`
The matplotlib colormap with random colors.
"""
from matplotlib import colors
prng = check_random_state(random_state)
h = prng.uniform(low=0.0, high=1.0, size=ncolors)
s = prng.uniform(low=0.2, high=0.7, size=ncolors)
v = prng.uniform(low=0.5, high=1.0, size=ncolors)
hsv = np.dstack((h, s, v))
rgb = np.squeeze(colors.hsv_to_rgb(hsv))
if background_color is not None:
if background_color not in colors.cnames:
raise ValueError('"{0}" is not a valid background color '
'name'.format(background_color))
rgb[0] = colors.hex2color(colors.cnames[background_color])
return colors.ListedColormap(rgb)
|