|
15 | 15 | normalization.
|
16 | 16 | """
|
17 | 17 |
|
18 |
| -from collections.abc import MutableMapping |
| 18 | +from collections.abc import Mapping, MutableMapping |
19 | 19 |
|
20 | 20 | import numpy as np
|
21 | 21 | from numpy import ma
|
@@ -91,13 +91,86 @@ def _warn_deprecated(self):
|
91 | 91 | )
|
92 | 92 |
|
93 | 93 |
|
| 94 | +class ColormapRegistry(Mapping): |
| 95 | + r""" |
| 96 | + Container for colormaps that are known to Matplotlib by name. |
| 97 | +
|
| 98 | + There should be no need for users to instantiate this class themselves. |
| 99 | +
|
| 100 | + The universal registry instance is `matplotlib.colormaps`. |
| 101 | +
|
| 102 | + Read access uses a dict-like interface mapping names to `.Colormap`\s:: |
| 103 | +
|
| 104 | + import matplotlib as mpl |
| 105 | + cmap = mpl.colormaps['viridis'] |
| 106 | +
|
| 107 | + Returned `.Colormap`\s are copies, so that their modification does not |
| 108 | + change the global definition of the colormap. |
| 109 | +
|
| 110 | + Additional colormaps can be added via `.ColormapRegistry.register`:: |
| 111 | +
|
| 112 | + mpl.colormaps.register(my_colormap) |
| 113 | + """ |
| 114 | + def __init__(self, cmaps): |
| 115 | + self._cmaps = cmaps |
| 116 | + |
| 117 | + def __getitem__(self, item): |
| 118 | + try: |
| 119 | + return self._cmaps[item].copy() |
| 120 | + except KeyError: |
| 121 | + raise KeyError(f"{item!r} is not a known colormap name") |
| 122 | + |
| 123 | + def __iter__(self): |
| 124 | + return iter(self._cmaps) |
| 125 | + |
| 126 | + def __len__(self): |
| 127 | + return len(self._cmaps) |
| 128 | + |
| 129 | + def __str__(self): |
| 130 | + return ('ColormapRegistry; available colormaps:\n' + |
| 131 | + ', '.join(f"'{name}'" for name in self)) |
| 132 | + |
| 133 | + def register(self, cmap, *, name=None, force=False): |
| 134 | + """ |
| 135 | + Register a new colormap. |
| 136 | +
|
| 137 | + The colormap name can then be used as a string argument to any ``cmap`` |
| 138 | + parameter in Matplotlib. It is also available in `.pyplot.get_cmap`. |
| 139 | +
|
| 140 | + The colormap registry stores a copy of the given colormap, so that |
| 141 | + future changes to the original colormap instance do not affect the |
| 142 | + registered colormap. Think of this as the registry taking a snapshot |
| 143 | + of the colormap at registration. |
| 144 | +
|
| 145 | + Parameters |
| 146 | + ---------- |
| 147 | + cmap : matplotlib.colors.Colormap |
| 148 | + The colormap to register. |
| 149 | +
|
| 150 | + name : str, optional |
| 151 | + The name for the colormap. If not given, the |
| 152 | + :attr:`~.Colormap.name` attribute of *cmap* is used. |
| 153 | +
|
| 154 | + force: bool, default: False |
| 155 | +
|
| 156 | + """ |
| 157 | + name_ = name or cmap.name |
| 158 | + if name_ in self and not force: |
| 159 | + raise ValueError( |
| 160 | + f'A colormap named "{name}" is already registered.') |
| 161 | + register_cmap(name, cmap.copy()) |
| 162 | + |
| 163 | + |
94 | 164 | _cmap_registry = _gen_cmap_registry()
|
95 | 165 | globals().update(_cmap_registry)
|
96 | 166 | # This is no longer considered public API
|
97 | 167 | cmap_d = _DeprecatedCmapDictWrapper(_cmap_registry)
|
98 | 168 | __builtin_cmaps = tuple(_cmap_registry)
|
99 | 169 |
|
100 |
| -# Continue with definitions ... |
| 170 | +# public acces to the colormaps should be via `matplotlib.colormaps`. For now, |
| 171 | +# we still create the registry here, but that should stay an implementation |
| 172 | +# detail. |
| 173 | +_colormaps = ColormapRegistry(_cmap_registry) |
101 | 174 |
|
102 | 175 |
|
103 | 176 | def register_cmap(name=None, cmap=None, *, override_builtin=False):
|
|
0 commit comments