palettes.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. import colorsys
  2. from itertools import cycle
  3. import numpy as np
  4. import matplotlib as mpl
  5. from .external import husl
  6. from .utils import desaturate, get_color_cycle
  7. from .colors import xkcd_rgb, crayons
  8. from ._compat import get_colormap
  9. __all__ = ["color_palette", "hls_palette", "husl_palette", "mpl_palette",
  10. "dark_palette", "light_palette", "diverging_palette",
  11. "blend_palette", "xkcd_palette", "crayon_palette",
  12. "cubehelix_palette", "set_color_codes"]
  13. SEABORN_PALETTES = dict(
  14. deep=["#4C72B0", "#DD8452", "#55A868", "#C44E52", "#8172B3",
  15. "#937860", "#DA8BC3", "#8C8C8C", "#CCB974", "#64B5CD"],
  16. deep6=["#4C72B0", "#55A868", "#C44E52",
  17. "#8172B3", "#CCB974", "#64B5CD"],
  18. muted=["#4878D0", "#EE854A", "#6ACC64", "#D65F5F", "#956CB4",
  19. "#8C613C", "#DC7EC0", "#797979", "#D5BB67", "#82C6E2"],
  20. muted6=["#4878D0", "#6ACC64", "#D65F5F",
  21. "#956CB4", "#D5BB67", "#82C6E2"],
  22. pastel=["#A1C9F4", "#FFB482", "#8DE5A1", "#FF9F9B", "#D0BBFF",
  23. "#DEBB9B", "#FAB0E4", "#CFCFCF", "#FFFEA3", "#B9F2F0"],
  24. pastel6=["#A1C9F4", "#8DE5A1", "#FF9F9B",
  25. "#D0BBFF", "#FFFEA3", "#B9F2F0"],
  26. bright=["#023EFF", "#FF7C00", "#1AC938", "#E8000B", "#8B2BE2",
  27. "#9F4800", "#F14CC1", "#A3A3A3", "#FFC400", "#00D7FF"],
  28. bright6=["#023EFF", "#1AC938", "#E8000B",
  29. "#8B2BE2", "#FFC400", "#00D7FF"],
  30. dark=["#001C7F", "#B1400D", "#12711C", "#8C0800", "#591E71",
  31. "#592F0D", "#A23582", "#3C3C3C", "#B8850A", "#006374"],
  32. dark6=["#001C7F", "#12711C", "#8C0800",
  33. "#591E71", "#B8850A", "#006374"],
  34. colorblind=["#0173B2", "#DE8F05", "#029E73", "#D55E00", "#CC78BC",
  35. "#CA9161", "#FBAFE4", "#949494", "#ECE133", "#56B4E9"],
  36. colorblind6=["#0173B2", "#029E73", "#D55E00",
  37. "#CC78BC", "#ECE133", "#56B4E9"]
  38. )
  39. MPL_QUAL_PALS = {
  40. "tab10": 10, "tab20": 20, "tab20b": 20, "tab20c": 20,
  41. "Set1": 9, "Set2": 8, "Set3": 12,
  42. "Accent": 8, "Paired": 12,
  43. "Pastel1": 9, "Pastel2": 8, "Dark2": 8,
  44. }
  45. QUAL_PALETTE_SIZES = MPL_QUAL_PALS.copy()
  46. QUAL_PALETTE_SIZES.update({k: len(v) for k, v in SEABORN_PALETTES.items()})
  47. QUAL_PALETTES = list(QUAL_PALETTE_SIZES.keys())
  48. class _ColorPalette(list):
  49. """Set the color palette in a with statement, otherwise be a list."""
  50. def __enter__(self):
  51. """Open the context."""
  52. from .rcmod import set_palette
  53. self._orig_palette = color_palette()
  54. set_palette(self)
  55. return self
  56. def __exit__(self, *args):
  57. """Close the context."""
  58. from .rcmod import set_palette
  59. set_palette(self._orig_palette)
  60. def as_hex(self):
  61. """Return a color palette with hex codes instead of RGB values."""
  62. hex = [mpl.colors.rgb2hex(rgb) for rgb in self]
  63. return _ColorPalette(hex)
  64. def _repr_html_(self):
  65. """Rich display of the color palette in an HTML frontend."""
  66. s = 55
  67. n = len(self)
  68. html = f'<svg width="{n * s}" height="{s}">'
  69. for i, c in enumerate(self.as_hex()):
  70. html += (
  71. f'<rect x="{i * s}" y="0" width="{s}" height="{s}" style="fill:{c};'
  72. 'stroke-width:2;stroke:rgb(255,255,255)"/>'
  73. )
  74. html += '</svg>'
  75. return html
  76. def _patch_colormap_display():
  77. """Simplify the rich display of matplotlib color maps in a notebook."""
  78. def _repr_png_(self):
  79. """Generate a PNG representation of the Colormap."""
  80. import io
  81. from PIL import Image
  82. import numpy as np
  83. IMAGE_SIZE = (400, 50)
  84. X = np.tile(np.linspace(0, 1, IMAGE_SIZE[0]), (IMAGE_SIZE[1], 1))
  85. pixels = self(X, bytes=True)
  86. png_bytes = io.BytesIO()
  87. Image.fromarray(pixels).save(png_bytes, format='png')
  88. return png_bytes.getvalue()
  89. def _repr_html_(self):
  90. """Generate an HTML representation of the Colormap."""
  91. import base64
  92. png_bytes = self._repr_png_()
  93. png_base64 = base64.b64encode(png_bytes).decode('ascii')
  94. return ('<img '
  95. + 'alt="' + self.name + ' color map" '
  96. + 'title="' + self.name + '"'
  97. + 'src="data:image/png;base64,' + png_base64 + '">')
  98. mpl.colors.Colormap._repr_png_ = _repr_png_
  99. mpl.colors.Colormap._repr_html_ = _repr_html_
  100. def color_palette(palette=None, n_colors=None, desat=None, as_cmap=False):
  101. """Return a list of colors or continuous colormap defining a palette.
  102. Possible ``palette`` values include:
  103. - Name of a seaborn palette (deep, muted, bright, pastel, dark, colorblind)
  104. - Name of matplotlib colormap
  105. - 'husl' or 'hls'
  106. - 'ch:<cubehelix arguments>'
  107. - 'light:<color>', 'dark:<color>', 'blend:<color>,<color>',
  108. - A sequence of colors in any format matplotlib accepts
  109. Calling this function with ``palette=None`` will return the current
  110. matplotlib color cycle.
  111. This function can also be used in a ``with`` statement to temporarily
  112. set the color cycle for a plot or set of plots.
  113. See the :ref:`tutorial <palette_tutorial>` for more information.
  114. Parameters
  115. ----------
  116. palette : None, string, or sequence, optional
  117. Name of palette or None to return current palette. If a sequence, input
  118. colors are used but possibly cycled and desaturated.
  119. n_colors : int, optional
  120. Number of colors in the palette. If ``None``, the default will depend
  121. on how ``palette`` is specified. Named palettes default to 6 colors,
  122. but grabbing the current palette or passing in a list of colors will
  123. not change the number of colors unless this is specified. Asking for
  124. more colors than exist in the palette will cause it to cycle. Ignored
  125. when ``as_cmap`` is True.
  126. desat : float, optional
  127. Proportion to desaturate each color by.
  128. as_cmap : bool
  129. If True, return a :class:`matplotlib.colors.ListedColormap`.
  130. Returns
  131. -------
  132. list of RGB tuples or :class:`matplotlib.colors.ListedColormap`
  133. See Also
  134. --------
  135. set_palette : Set the default color cycle for all plots.
  136. set_color_codes : Reassign color codes like ``"b"``, ``"g"``, etc. to
  137. colors from one of the seaborn palettes.
  138. Examples
  139. --------
  140. .. include:: ../docstrings/color_palette.rst
  141. """
  142. if palette is None:
  143. palette = get_color_cycle()
  144. if n_colors is None:
  145. n_colors = len(palette)
  146. elif not isinstance(palette, str):
  147. palette = palette
  148. if n_colors is None:
  149. n_colors = len(palette)
  150. else:
  151. if n_colors is None:
  152. # Use all colors in a qualitative palette or 6 of another kind
  153. n_colors = QUAL_PALETTE_SIZES.get(palette, 6)
  154. if palette in SEABORN_PALETTES:
  155. # Named "seaborn variant" of matplotlib default color cycle
  156. palette = SEABORN_PALETTES[palette]
  157. elif palette == "hls":
  158. # Evenly spaced colors in cylindrical RGB space
  159. palette = hls_palette(n_colors, as_cmap=as_cmap)
  160. elif palette == "husl":
  161. # Evenly spaced colors in cylindrical Lab space
  162. palette = husl_palette(n_colors, as_cmap=as_cmap)
  163. elif palette.lower() == "jet":
  164. # Paternalism
  165. raise ValueError("No.")
  166. elif palette.startswith("ch:"):
  167. # Cubehelix palette with params specified in string
  168. args, kwargs = _parse_cubehelix_args(palette)
  169. palette = cubehelix_palette(n_colors, *args, **kwargs, as_cmap=as_cmap)
  170. elif palette.startswith("light:"):
  171. # light palette to color specified in string
  172. _, color = palette.split(":")
  173. reverse = color.endswith("_r")
  174. if reverse:
  175. color = color[:-2]
  176. palette = light_palette(color, n_colors, reverse=reverse, as_cmap=as_cmap)
  177. elif palette.startswith("dark:"):
  178. # light palette to color specified in string
  179. _, color = palette.split(":")
  180. reverse = color.endswith("_r")
  181. if reverse:
  182. color = color[:-2]
  183. palette = dark_palette(color, n_colors, reverse=reverse, as_cmap=as_cmap)
  184. elif palette.startswith("blend:"):
  185. # blend palette between colors specified in string
  186. _, colors = palette.split(":")
  187. colors = colors.split(",")
  188. palette = blend_palette(colors, n_colors, as_cmap=as_cmap)
  189. else:
  190. try:
  191. # Perhaps a named matplotlib colormap?
  192. palette = mpl_palette(palette, n_colors, as_cmap=as_cmap)
  193. except (ValueError, KeyError): # Error class changed in mpl36
  194. raise ValueError(f"{palette!r} is not a valid palette name")
  195. if desat is not None:
  196. palette = [desaturate(c, desat) for c in palette]
  197. if not as_cmap:
  198. # Always return as many colors as we asked for
  199. pal_cycle = cycle(palette)
  200. palette = [next(pal_cycle) for _ in range(n_colors)]
  201. # Always return in r, g, b tuple format
  202. try:
  203. palette = map(mpl.colors.colorConverter.to_rgb, palette)
  204. palette = _ColorPalette(palette)
  205. except ValueError:
  206. raise ValueError(f"Could not generate a palette for {palette}")
  207. return palette
  208. def hls_palette(n_colors=6, h=.01, l=.6, s=.65, as_cmap=False): # noqa
  209. """
  210. Return hues with constant lightness and saturation in the HLS system.
  211. The hues are evenly sampled along a circular path. The resulting palette will be
  212. appropriate for categorical or cyclical data.
  213. The `h`, `l`, and `s` values should be between 0 and 1.
  214. .. note::
  215. While the separation of the resulting colors will be mathematically
  216. constant, the HLS system does not construct a perceptually-uniform space,
  217. so their apparent intensity will vary.
  218. Parameters
  219. ----------
  220. n_colors : int
  221. Number of colors in the palette.
  222. h : float
  223. The value of the first hue.
  224. l : float
  225. The lightness value.
  226. s : float
  227. The saturation intensity.
  228. as_cmap : bool
  229. If True, return a matplotlib colormap object.
  230. Returns
  231. -------
  232. palette
  233. list of RGB tuples or :class:`matplotlib.colors.ListedColormap`
  234. See Also
  235. --------
  236. husl_palette : Make a palette using evenly spaced hues in the HUSL system.
  237. Examples
  238. --------
  239. .. include:: ../docstrings/hls_palette.rst
  240. """
  241. if as_cmap:
  242. n_colors = 256
  243. hues = np.linspace(0, 1, int(n_colors) + 1)[:-1]
  244. hues += h
  245. hues %= 1
  246. hues -= hues.astype(int)
  247. palette = [colorsys.hls_to_rgb(h_i, l, s) for h_i in hues]
  248. if as_cmap:
  249. return mpl.colors.ListedColormap(palette, "hls")
  250. else:
  251. return _ColorPalette(palette)
  252. def husl_palette(n_colors=6, h=.01, s=.9, l=.65, as_cmap=False): # noqa
  253. """
  254. Return hues with constant lightness and saturation in the HUSL system.
  255. The hues are evenly sampled along a circular path. The resulting palette will be
  256. appropriate for categorical or cyclical data.
  257. The `h`, `l`, and `s` values should be between 0 and 1.
  258. This function is similar to :func:`hls_palette`, but it uses a nonlinear color
  259. space that is more perceptually uniform.
  260. Parameters
  261. ----------
  262. n_colors : int
  263. Number of colors in the palette.
  264. h : float
  265. The value of the first hue.
  266. l : float
  267. The lightness value.
  268. s : float
  269. The saturation intensity.
  270. as_cmap : bool
  271. If True, return a matplotlib colormap object.
  272. Returns
  273. -------
  274. palette
  275. list of RGB tuples or :class:`matplotlib.colors.ListedColormap`
  276. See Also
  277. --------
  278. hls_palette : Make a palette using evenly spaced hues in the HSL system.
  279. Examples
  280. --------
  281. .. include:: ../docstrings/husl_palette.rst
  282. """
  283. if as_cmap:
  284. n_colors = 256
  285. hues = np.linspace(0, 1, int(n_colors) + 1)[:-1]
  286. hues += h
  287. hues %= 1
  288. hues *= 359
  289. s *= 99
  290. l *= 99 # noqa
  291. palette = [_color_to_rgb((h_i, s, l), input="husl") for h_i in hues]
  292. if as_cmap:
  293. return mpl.colors.ListedColormap(palette, "hsl")
  294. else:
  295. return _ColorPalette(palette)
  296. def mpl_palette(name, n_colors=6, as_cmap=False):
  297. """
  298. Return a palette or colormap from the matplotlib registry.
  299. For continuous palettes, evenly-spaced discrete samples are chosen while
  300. excluding the minimum and maximum value in the colormap to provide better
  301. contrast at the extremes.
  302. For qualitative palettes (e.g. those from colorbrewer), exact values are
  303. indexed (rather than interpolated), but fewer than `n_colors` can be returned
  304. if the palette does not define that many.
  305. Parameters
  306. ----------
  307. name : string
  308. Name of the palette. This should be a named matplotlib colormap.
  309. n_colors : int
  310. Number of discrete colors in the palette.
  311. Returns
  312. -------
  313. list of RGB tuples or :class:`matplotlib.colors.ListedColormap`
  314. Examples
  315. --------
  316. .. include:: ../docstrings/mpl_palette.rst
  317. """
  318. if name.endswith("_d"):
  319. sub_name = name[:-2]
  320. if sub_name.endswith("_r"):
  321. reverse = True
  322. sub_name = sub_name[:-2]
  323. else:
  324. reverse = False
  325. pal = color_palette(sub_name, 2) + ["#333333"]
  326. if reverse:
  327. pal = pal[::-1]
  328. cmap = blend_palette(pal, n_colors, as_cmap=True)
  329. else:
  330. cmap = get_colormap(name)
  331. if name in MPL_QUAL_PALS:
  332. bins = np.linspace(0, 1, MPL_QUAL_PALS[name])[:n_colors]
  333. else:
  334. bins = np.linspace(0, 1, int(n_colors) + 2)[1:-1]
  335. palette = list(map(tuple, cmap(bins)[:, :3]))
  336. if as_cmap:
  337. return cmap
  338. else:
  339. return _ColorPalette(palette)
  340. def _color_to_rgb(color, input):
  341. """Add some more flexibility to color choices."""
  342. if input == "hls":
  343. color = colorsys.hls_to_rgb(*color)
  344. elif input == "husl":
  345. color = husl.husl_to_rgb(*color)
  346. color = tuple(np.clip(color, 0, 1))
  347. elif input == "xkcd":
  348. color = xkcd_rgb[color]
  349. return mpl.colors.to_rgb(color)
  350. def dark_palette(color, n_colors=6, reverse=False, as_cmap=False, input="rgb"):
  351. """Make a sequential palette that blends from dark to ``color``.
  352. This kind of palette is good for data that range between relatively
  353. uninteresting low values and interesting high values.
  354. The ``color`` parameter can be specified in a number of ways, including
  355. all options for defining a color in matplotlib and several additional
  356. color spaces that are handled by seaborn. You can also use the database
  357. of named colors from the XKCD color survey.
  358. If you are using the IPython notebook, you can also choose this palette
  359. interactively with the :func:`choose_dark_palette` function.
  360. Parameters
  361. ----------
  362. color : base color for high values
  363. hex, rgb-tuple, or html color name
  364. n_colors : int, optional
  365. number of colors in the palette
  366. reverse : bool, optional
  367. if True, reverse the direction of the blend
  368. as_cmap : bool, optional
  369. If True, return a :class:`matplotlib.colors.ListedColormap`.
  370. input : {'rgb', 'hls', 'husl', xkcd'}
  371. Color space to interpret the input color. The first three options
  372. apply to tuple inputs and the latter applies to string inputs.
  373. Returns
  374. -------
  375. palette
  376. list of RGB tuples or :class:`matplotlib.colors.ListedColormap`
  377. See Also
  378. --------
  379. light_palette : Create a sequential palette with bright low values.
  380. diverging_palette : Create a diverging palette with two colors.
  381. Examples
  382. --------
  383. .. include:: ../docstrings/dark_palette.rst
  384. """
  385. rgb = _color_to_rgb(color, input)
  386. hue, sat, _ = husl.rgb_to_husl(*rgb)
  387. gray_s, gray_l = .15 * sat, 15
  388. gray = _color_to_rgb((hue, gray_s, gray_l), input="husl")
  389. colors = [rgb, gray] if reverse else [gray, rgb]
  390. return blend_palette(colors, n_colors, as_cmap)
  391. def light_palette(color, n_colors=6, reverse=False, as_cmap=False, input="rgb"):
  392. """Make a sequential palette that blends from light to ``color``.
  393. The ``color`` parameter can be specified in a number of ways, including
  394. all options for defining a color in matplotlib and several additional
  395. color spaces that are handled by seaborn. You can also use the database
  396. of named colors from the XKCD color survey.
  397. If you are using a Jupyter notebook, you can also choose this palette
  398. interactively with the :func:`choose_light_palette` function.
  399. Parameters
  400. ----------
  401. color : base color for high values
  402. hex code, html color name, or tuple in `input` space.
  403. n_colors : int, optional
  404. number of colors in the palette
  405. reverse : bool, optional
  406. if True, reverse the direction of the blend
  407. as_cmap : bool, optional
  408. If True, return a :class:`matplotlib.colors.ListedColormap`.
  409. input : {'rgb', 'hls', 'husl', xkcd'}
  410. Color space to interpret the input color. The first three options
  411. apply to tuple inputs and the latter applies to string inputs.
  412. Returns
  413. -------
  414. palette
  415. list of RGB tuples or :class:`matplotlib.colors.ListedColormap`
  416. See Also
  417. --------
  418. dark_palette : Create a sequential palette with dark low values.
  419. diverging_palette : Create a diverging palette with two colors.
  420. Examples
  421. --------
  422. .. include:: ../docstrings/light_palette.rst
  423. """
  424. rgb = _color_to_rgb(color, input)
  425. hue, sat, _ = husl.rgb_to_husl(*rgb)
  426. gray_s, gray_l = .15 * sat, 95
  427. gray = _color_to_rgb((hue, gray_s, gray_l), input="husl")
  428. colors = [rgb, gray] if reverse else [gray, rgb]
  429. return blend_palette(colors, n_colors, as_cmap)
  430. def diverging_palette(h_neg, h_pos, s=75, l=50, sep=1, n=6, # noqa
  431. center="light", as_cmap=False):
  432. """Make a diverging palette between two HUSL colors.
  433. If you are using the IPython notebook, you can also choose this palette
  434. interactively with the :func:`choose_diverging_palette` function.
  435. Parameters
  436. ----------
  437. h_neg, h_pos : float in [0, 359]
  438. Anchor hues for negative and positive extents of the map.
  439. s : float in [0, 100], optional
  440. Anchor saturation for both extents of the map.
  441. l : float in [0, 100], optional
  442. Anchor lightness for both extents of the map.
  443. sep : int, optional
  444. Size of the intermediate region.
  445. n : int, optional
  446. Number of colors in the palette (if not returning a cmap)
  447. center : {"light", "dark"}, optional
  448. Whether the center of the palette is light or dark
  449. as_cmap : bool, optional
  450. If True, return a :class:`matplotlib.colors.ListedColormap`.
  451. Returns
  452. -------
  453. palette
  454. list of RGB tuples or :class:`matplotlib.colors.ListedColormap`
  455. See Also
  456. --------
  457. dark_palette : Create a sequential palette with dark values.
  458. light_palette : Create a sequential palette with light values.
  459. Examples
  460. --------
  461. .. include: ../docstrings/diverging_palette.rst
  462. """
  463. palfunc = dict(dark=dark_palette, light=light_palette)[center]
  464. n_half = int(128 - (sep // 2))
  465. neg = palfunc((h_neg, s, l), n_half, reverse=True, input="husl")
  466. pos = palfunc((h_pos, s, l), n_half, input="husl")
  467. midpoint = dict(light=[(.95, .95, .95)], dark=[(.133, .133, .133)])[center]
  468. mid = midpoint * sep
  469. pal = blend_palette(np.concatenate([neg, mid, pos]), n, as_cmap=as_cmap)
  470. return pal
  471. def blend_palette(colors, n_colors=6, as_cmap=False, input="rgb"):
  472. """Make a palette that blends between a list of colors.
  473. Parameters
  474. ----------
  475. colors : sequence of colors in various formats interpreted by `input`
  476. hex code, html color name, or tuple in `input` space.
  477. n_colors : int, optional
  478. Number of colors in the palette.
  479. as_cmap : bool, optional
  480. If True, return a :class:`matplotlib.colors.ListedColormap`.
  481. Returns
  482. -------
  483. palette
  484. list of RGB tuples or :class:`matplotlib.colors.ListedColormap`
  485. Examples
  486. --------
  487. .. include: ../docstrings/blend_palette.rst
  488. """
  489. colors = [_color_to_rgb(color, input) for color in colors]
  490. name = "blend"
  491. pal = mpl.colors.LinearSegmentedColormap.from_list(name, colors)
  492. if not as_cmap:
  493. rgb_array = pal(np.linspace(0, 1, int(n_colors)))[:, :3] # no alpha
  494. pal = _ColorPalette(map(tuple, rgb_array))
  495. return pal
  496. def xkcd_palette(colors):
  497. """Make a palette with color names from the xkcd color survey.
  498. See xkcd for the full list of colors: https://xkcd.com/color/rgb/
  499. This is just a simple wrapper around the `seaborn.xkcd_rgb` dictionary.
  500. Parameters
  501. ----------
  502. colors : list of strings
  503. List of keys in the `seaborn.xkcd_rgb` dictionary.
  504. Returns
  505. -------
  506. palette
  507. A list of colors as RGB tuples.
  508. See Also
  509. --------
  510. crayon_palette : Make a palette with Crayola crayon colors.
  511. """
  512. palette = [xkcd_rgb[name] for name in colors]
  513. return color_palette(palette, len(palette))
  514. def crayon_palette(colors):
  515. """Make a palette with color names from Crayola crayons.
  516. Colors are taken from here:
  517. https://en.wikipedia.org/wiki/List_of_Crayola_crayon_colors
  518. This is just a simple wrapper around the `seaborn.crayons` dictionary.
  519. Parameters
  520. ----------
  521. colors : list of strings
  522. List of keys in the `seaborn.crayons` dictionary.
  523. Returns
  524. -------
  525. palette
  526. A list of colors as RGB tuples.
  527. See Also
  528. --------
  529. xkcd_palette : Make a palette with named colors from the XKCD color survey.
  530. """
  531. palette = [crayons[name] for name in colors]
  532. return color_palette(palette, len(palette))
  533. def cubehelix_palette(n_colors=6, start=0, rot=.4, gamma=1.0, hue=0.8,
  534. light=.85, dark=.15, reverse=False, as_cmap=False):
  535. """Make a sequential palette from the cubehelix system.
  536. This produces a colormap with linearly-decreasing (or increasing)
  537. brightness. That means that information will be preserved if printed to
  538. black and white or viewed by someone who is colorblind. "cubehelix" is
  539. also available as a matplotlib-based palette, but this function gives the
  540. user more control over the look of the palette and has a different set of
  541. defaults.
  542. In addition to using this function, it is also possible to generate a
  543. cubehelix palette generally in seaborn using a string starting with
  544. `ch:` and containing other parameters (e.g. `"ch:s=.25,r=-.5"`).
  545. Parameters
  546. ----------
  547. n_colors : int
  548. Number of colors in the palette.
  549. start : float, 0 <= start <= 3
  550. The hue value at the start of the helix.
  551. rot : float
  552. Rotations around the hue wheel over the range of the palette.
  553. gamma : float 0 <= gamma
  554. Nonlinearity to emphasize dark (gamma < 1) or light (gamma > 1) colors.
  555. hue : float, 0 <= hue <= 1
  556. Saturation of the colors.
  557. dark : float 0 <= dark <= 1
  558. Intensity of the darkest color in the palette.
  559. light : float 0 <= light <= 1
  560. Intensity of the lightest color in the palette.
  561. reverse : bool
  562. If True, the palette will go from dark to light.
  563. as_cmap : bool
  564. If True, return a :class:`matplotlib.colors.ListedColormap`.
  565. Returns
  566. -------
  567. palette
  568. list of RGB tuples or :class:`matplotlib.colors.ListedColormap`
  569. See Also
  570. --------
  571. choose_cubehelix_palette : Launch an interactive widget to select cubehelix
  572. palette parameters.
  573. dark_palette : Create a sequential palette with dark low values.
  574. light_palette : Create a sequential palette with bright low values.
  575. References
  576. ----------
  577. Green, D. A. (2011). "A colour scheme for the display of astronomical
  578. intensity images". Bulletin of the Astromical Society of India, Vol. 39,
  579. p. 289-295.
  580. Examples
  581. --------
  582. .. include:: ../docstrings/cubehelix_palette.rst
  583. """
  584. def get_color_function(p0, p1):
  585. # Copied from matplotlib because it lives in private module
  586. def color(x):
  587. # Apply gamma factor to emphasise low or high intensity values
  588. xg = x ** gamma
  589. # Calculate amplitude and angle of deviation from the black
  590. # to white diagonal in the plane of constant
  591. # perceived intensity.
  592. a = hue * xg * (1 - xg) / 2
  593. phi = 2 * np.pi * (start / 3 + rot * x)
  594. return xg + a * (p0 * np.cos(phi) + p1 * np.sin(phi))
  595. return color
  596. cdict = {
  597. "red": get_color_function(-0.14861, 1.78277),
  598. "green": get_color_function(-0.29227, -0.90649),
  599. "blue": get_color_function(1.97294, 0.0),
  600. }
  601. cmap = mpl.colors.LinearSegmentedColormap("cubehelix", cdict)
  602. x = np.linspace(light, dark, int(n_colors))
  603. pal = cmap(x)[:, :3].tolist()
  604. if reverse:
  605. pal = pal[::-1]
  606. if as_cmap:
  607. x_256 = np.linspace(light, dark, 256)
  608. if reverse:
  609. x_256 = x_256[::-1]
  610. pal_256 = cmap(x_256)
  611. cmap = mpl.colors.ListedColormap(pal_256, "seaborn_cubehelix")
  612. return cmap
  613. else:
  614. return _ColorPalette(pal)
  615. def _parse_cubehelix_args(argstr):
  616. """Turn stringified cubehelix params into args/kwargs."""
  617. if argstr.startswith("ch:"):
  618. argstr = argstr[3:]
  619. if argstr.endswith("_r"):
  620. reverse = True
  621. argstr = argstr[:-2]
  622. else:
  623. reverse = False
  624. if not argstr:
  625. return [], {"reverse": reverse}
  626. all_args = argstr.split(",")
  627. args = [float(a.strip(" ")) for a in all_args if "=" not in a]
  628. kwargs = [a.split("=") for a in all_args if "=" in a]
  629. kwargs = {k.strip(" "): float(v.strip(" ")) for k, v in kwargs}
  630. kwarg_map = dict(
  631. s="start", r="rot", g="gamma",
  632. h="hue", l="light", d="dark", # noqa: E741
  633. )
  634. kwargs = {kwarg_map.get(k, k): v for k, v in kwargs.items()}
  635. if reverse:
  636. kwargs["reverse"] = True
  637. return args, kwargs
  638. def set_color_codes(palette="deep"):
  639. """Change how matplotlib color shorthands are interpreted.
  640. Calling this will change how shorthand codes like "b" or "g"
  641. are interpreted by matplotlib in subsequent plots.
  642. Parameters
  643. ----------
  644. palette : {deep, muted, pastel, dark, bright, colorblind}
  645. Named seaborn palette to use as the source of colors.
  646. See Also
  647. --------
  648. set : Color codes can be set through the high-level seaborn style
  649. manager.
  650. set_palette : Color codes can also be set through the function that
  651. sets the matplotlib color cycle.
  652. """
  653. if palette == "reset":
  654. colors = [
  655. (0., 0., 1.),
  656. (0., .5, 0.),
  657. (1., 0., 0.),
  658. (.75, 0., .75),
  659. (.75, .75, 0.),
  660. (0., .75, .75),
  661. (0., 0., 0.)
  662. ]
  663. elif not isinstance(palette, str):
  664. err = "set_color_codes requires a named seaborn palette"
  665. raise TypeError(err)
  666. elif palette in SEABORN_PALETTES:
  667. if not palette.endswith("6"):
  668. palette = palette + "6"
  669. colors = SEABORN_PALETTES[palette] + [(.1, .1, .1)]
  670. else:
  671. err = f"Cannot set colors with palette '{palette}'"
  672. raise ValueError(err)
  673. for code, color in zip("bgrmyck", colors):
  674. rgb = mpl.colors.colorConverter.to_rgb(color)
  675. mpl.colors.colorConverter.colors[code] = rgb