You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

404 lines
12 KiB

4 years ago
  1. """
  2. Builtin colormaps, colormap handling utilities, and the `ScalarMappable` mixin.
  3. .. seealso::
  4. :doc:`/gallery/color/colormap_reference` for a list of builtin
  5. colormaps.
  6. :doc:`/tutorials/colors/colormap-manipulation` for examples of how to
  7. make colormaps and
  8. :doc:`/tutorials/colors/colormaps` an in-depth discussion of
  9. choosing colormaps.
  10. :doc:`/tutorials/colors/colormapnorms` for more details about data
  11. normalization
  12. """
  13. import functools
  14. import numpy as np
  15. from numpy import ma
  16. import matplotlib as mpl
  17. import matplotlib.colors as colors
  18. import matplotlib.cbook as cbook
  19. from matplotlib._cm import datad
  20. from matplotlib._cm_listed import cmaps as cmaps_listed
  21. cmap_d = {}
  22. # reverse all the colormaps.
  23. # reversed colormaps have '_r' appended to the name.
  24. def _reverser(f, x=None):
  25. """Helper such that ``_reverser(f)(x) == f(1 - x)``."""
  26. if x is None:
  27. # Returning a partial object keeps it picklable.
  28. return functools.partial(_reverser, f)
  29. return f(1 - x)
  30. def revcmap(data):
  31. """Can only handle specification *data* in dictionary format."""
  32. data_r = {}
  33. for key, val in data.items():
  34. if callable(val):
  35. valnew = _reverser(val)
  36. # This doesn't work: lambda x: val(1-x)
  37. # The same "val" (the first one) is used
  38. # each time, so the colors are identical
  39. # and the result is shades of gray.
  40. else:
  41. # Flip x and exchange the y values facing x = 0 and x = 1.
  42. valnew = [(1.0 - x, y1, y0) for x, y0, y1 in reversed(val)]
  43. data_r[key] = valnew
  44. return data_r
  45. def _reverse_cmap_spec(spec):
  46. """Reverses cmap specification *spec*, can handle both dict and tuple
  47. type specs."""
  48. if 'listed' in spec:
  49. return {'listed': spec['listed'][::-1]}
  50. if 'red' in spec:
  51. return revcmap(spec)
  52. else:
  53. revspec = list(reversed(spec))
  54. if len(revspec[0]) == 2: # e.g., (1, (1.0, 0.0, 1.0))
  55. revspec = [(1.0 - a, b) for a, b in revspec]
  56. return revspec
  57. def _generate_cmap(name, lutsize):
  58. """Generates the requested cmap from its *name*. The lut size is
  59. *lutsize*."""
  60. spec = datad[name]
  61. # Generate the colormap object.
  62. if 'red' in spec:
  63. return colors.LinearSegmentedColormap(name, spec, lutsize)
  64. elif 'listed' in spec:
  65. return colors.ListedColormap(spec['listed'], name)
  66. else:
  67. return colors.LinearSegmentedColormap.from_list(name, spec, lutsize)
  68. LUTSIZE = mpl.rcParams['image.lut']
  69. # Generate the reversed specifications (all at once, to avoid
  70. # modify-when-iterating).
  71. datad.update({cmapname + '_r': _reverse_cmap_spec(spec)
  72. for cmapname, spec in datad.items()})
  73. # Precache the cmaps with ``lutsize = LUTSIZE``.
  74. # Also add the reversed ones added in the section above:
  75. for cmapname in datad:
  76. cmap_d[cmapname] = _generate_cmap(cmapname, LUTSIZE)
  77. cmap_d.update(cmaps_listed)
  78. locals().update(cmap_d)
  79. # Continue with definitions ...
  80. def register_cmap(name=None, cmap=None, data=None, lut=None):
  81. """
  82. Add a colormap to the set recognized by :func:`get_cmap`.
  83. It can be used in two ways::
  84. register_cmap(name='swirly', cmap=swirly_cmap)
  85. register_cmap(name='choppy', data=choppydata, lut=128)
  86. In the first case, *cmap* must be a :class:`matplotlib.colors.Colormap`
  87. instance. The *name* is optional; if absent, the name will
  88. be the :attr:`~matplotlib.colors.Colormap.name` attribute of the *cmap*.
  89. In the second case, the three arguments are passed to
  90. the :class:`~matplotlib.colors.LinearSegmentedColormap` initializer,
  91. and the resulting colormap is registered.
  92. """
  93. if name is None:
  94. try:
  95. name = cmap.name
  96. except AttributeError:
  97. raise ValueError("Arguments must include a name or a Colormap")
  98. if not isinstance(name, str):
  99. raise ValueError("Colormap name must be a string")
  100. if isinstance(cmap, colors.Colormap):
  101. cmap_d[name] = cmap
  102. return
  103. # For the remainder, let exceptions propagate.
  104. if lut is None:
  105. lut = mpl.rcParams['image.lut']
  106. cmap = colors.LinearSegmentedColormap(name, data, lut)
  107. cmap_d[name] = cmap
  108. def get_cmap(name=None, lut=None):
  109. """
  110. Get a colormap instance, defaulting to rc values if *name* is None.
  111. Colormaps added with :func:`register_cmap` take precedence over
  112. built-in colormaps.
  113. If *name* is a :class:`matplotlib.colors.Colormap` instance, it will be
  114. returned.
  115. If *lut* is not None it must be an integer giving the number of
  116. entries desired in the lookup table, and *name* must be a standard
  117. mpl colormap name.
  118. """
  119. if name is None:
  120. name = mpl.rcParams['image.cmap']
  121. if isinstance(name, colors.Colormap):
  122. return name
  123. if name in cmap_d:
  124. if lut is None:
  125. return cmap_d[name]
  126. else:
  127. return cmap_d[name]._resample(lut)
  128. else:
  129. raise ValueError(
  130. "Colormap %s is not recognized. Possible values are: %s"
  131. % (name, ', '.join(sorted(cmap_d))))
  132. class ScalarMappable(object):
  133. """
  134. This is a mixin class to support scalar data to RGBA mapping.
  135. The ScalarMappable makes use of data normalization before returning
  136. RGBA colors from the given colormap.
  137. """
  138. def __init__(self, norm=None, cmap=None):
  139. r"""
  140. Parameters
  141. ----------
  142. norm : :class:`matplotlib.colors.Normalize` instance
  143. The normalizing object which scales data, typically into the
  144. interval ``[0, 1]``.
  145. If *None*, *norm* defaults to a *colors.Normalize* object which
  146. initializes its scaling based on the first data processed.
  147. cmap : str or :class:`~matplotlib.colors.Colormap` instance
  148. The colormap used to map normalized data values to RGBA colors.
  149. """
  150. self.callbacksSM = cbook.CallbackRegistry()
  151. if cmap is None:
  152. cmap = get_cmap()
  153. if norm is None:
  154. norm = colors.Normalize()
  155. self._A = None
  156. #: The Normalization instance of this ScalarMappable.
  157. self.norm = norm
  158. #: The Colormap instance of this ScalarMappable.
  159. self.cmap = get_cmap(cmap)
  160. #: The last colorbar associated with this ScalarMappable. May be None.
  161. self.colorbar = None
  162. self.update_dict = {'array': False}
  163. def to_rgba(self, x, alpha=None, bytes=False, norm=True):
  164. """
  165. Return a normalized rgba array corresponding to *x*.
  166. In the normal case, *x* is a 1-D or 2-D sequence of scalars, and
  167. the corresponding ndarray of rgba values will be returned,
  168. based on the norm and colormap set for this ScalarMappable.
  169. There is one special case, for handling images that are already
  170. rgb or rgba, such as might have been read from an image file.
  171. If *x* is an ndarray with 3 dimensions,
  172. and the last dimension is either 3 or 4, then it will be
  173. treated as an rgb or rgba array, and no mapping will be done.
  174. The array can be uint8, or it can be floating point with
  175. values in the 0-1 range; otherwise a ValueError will be raised.
  176. If it is a masked array, the mask will be ignored.
  177. If the last dimension is 3, the *alpha* kwarg (defaulting to 1)
  178. will be used to fill in the transparency. If the last dimension
  179. is 4, the *alpha* kwarg is ignored; it does not
  180. replace the pre-existing alpha. A ValueError will be raised
  181. if the third dimension is other than 3 or 4.
  182. In either case, if *bytes* is *False* (default), the rgba
  183. array will be floats in the 0-1 range; if it is *True*,
  184. the returned rgba array will be uint8 in the 0 to 255 range.
  185. If norm is False, no normalization of the input data is
  186. performed, and it is assumed to be in the range (0-1).
  187. """
  188. # First check for special case, image input:
  189. try:
  190. if x.ndim == 3:
  191. if x.shape[2] == 3:
  192. if alpha is None:
  193. alpha = 1
  194. if x.dtype == np.uint8:
  195. alpha = np.uint8(alpha * 255)
  196. m, n = x.shape[:2]
  197. xx = np.empty(shape=(m, n, 4), dtype=x.dtype)
  198. xx[:, :, :3] = x
  199. xx[:, :, 3] = alpha
  200. elif x.shape[2] == 4:
  201. xx = x
  202. else:
  203. raise ValueError("third dimension must be 3 or 4")
  204. if xx.dtype.kind == 'f':
  205. if norm and (xx.max() > 1 or xx.min() < 0):
  206. raise ValueError("Floating point image RGB values "
  207. "must be in the 0..1 range.")
  208. if bytes:
  209. xx = (xx * 255).astype(np.uint8)
  210. elif xx.dtype == np.uint8:
  211. if not bytes:
  212. xx = xx.astype(np.float32) / 255
  213. else:
  214. raise ValueError("Image RGB array must be uint8 or "
  215. "floating point; found %s" % xx.dtype)
  216. return xx
  217. except AttributeError:
  218. # e.g., x is not an ndarray; so try mapping it
  219. pass
  220. # This is the normal case, mapping a scalar array:
  221. x = ma.asarray(x)
  222. if norm:
  223. x = self.norm(x)
  224. rgba = self.cmap(x, alpha=alpha, bytes=bytes)
  225. return rgba
  226. def set_array(self, A):
  227. """Set the image array from numpy array *A*.
  228. Parameters
  229. ----------
  230. A : ndarray
  231. """
  232. self._A = A
  233. self.update_dict['array'] = True
  234. def get_array(self):
  235. 'Return the array'
  236. return self._A
  237. def get_cmap(self):
  238. 'return the colormap'
  239. return self.cmap
  240. def get_clim(self):
  241. 'return the min, max of the color limits for image scaling'
  242. return self.norm.vmin, self.norm.vmax
  243. def set_clim(self, vmin=None, vmax=None):
  244. """
  245. set the norm limits for image scaling; if *vmin* is a length2
  246. sequence, interpret it as ``(vmin, vmax)`` which is used to
  247. support setp
  248. ACCEPTS: a length 2 sequence of floats; may be overridden in methods
  249. that have ``vmin`` and ``vmax`` kwargs.
  250. """
  251. if vmax is None:
  252. try:
  253. vmin, vmax = vmin
  254. except (TypeError, ValueError):
  255. pass
  256. if vmin is not None:
  257. self.norm.vmin = colors._sanitize_extrema(vmin)
  258. if vmax is not None:
  259. self.norm.vmax = colors._sanitize_extrema(vmax)
  260. self.changed()
  261. def set_cmap(self, cmap):
  262. """
  263. set the colormap for luminance data
  264. Parameters
  265. ----------
  266. cmap : colormap or registered colormap name
  267. """
  268. cmap = get_cmap(cmap)
  269. self.cmap = cmap
  270. self.changed()
  271. def set_norm(self, norm):
  272. """Set the normalization instance.
  273. Parameters
  274. ----------
  275. norm : `.Normalize`
  276. """
  277. if norm is None:
  278. norm = colors.Normalize()
  279. self.norm = norm
  280. self.changed()
  281. def autoscale(self):
  282. """
  283. Autoscale the scalar limits on the norm instance using the
  284. current array
  285. """
  286. if self._A is None:
  287. raise TypeError('You must first set_array for mappable')
  288. self.norm.autoscale(self._A)
  289. self.changed()
  290. def autoscale_None(self):
  291. """
  292. Autoscale the scalar limits on the norm instance using the
  293. current array, changing only limits that are None
  294. """
  295. if self._A is None:
  296. raise TypeError('You must first set_array for mappable')
  297. self.norm.autoscale_None(self._A)
  298. self.changed()
  299. def add_checker(self, checker):
  300. """
  301. Add an entry to a dictionary of boolean flags
  302. that are set to True when the mappable is changed.
  303. """
  304. self.update_dict[checker] = False
  305. def check_update(self, checker):
  306. """
  307. If mappable has changed since the last check,
  308. return True; else return False
  309. """
  310. if self.update_dict[checker]:
  311. self.update_dict[checker] = False
  312. return True
  313. return False
  314. def changed(self):
  315. """
  316. Call this whenever the mappable is changed to notify all the
  317. callbackSM listeners to the 'changed' signal
  318. """
  319. self.callbacksSM.process('changed', self)
  320. for key in self.update_dict:
  321. self.update_dict[key] = True
  322. self.stale = True