Parameters ---------- boundaries : array-like Monotonically increasing sequence of at least 2 bin edges: data falling in the n-th bin will be mapped to the n-th color. ncolors : int Number of colors in the colormap to be used.
(self, boundaries, ncolors, clip=False, *, extend='neither')
| 3210 | # of conversions back and forth between int and float. |
| 3211 | |
| 3212 | def __init__(self, boundaries, ncolors, clip=False, *, extend='neither'): |
| 3213 | """ |
| 3214 | Parameters |
| 3215 | ---------- |
| 3216 | boundaries : array-like |
| 3217 | Monotonically increasing sequence of at least 2 bin edges: data |
| 3218 | falling in the n-th bin will be mapped to the n-th color. |
| 3219 | |
| 3220 | ncolors : int |
| 3221 | Number of colors in the colormap to be used. |
| 3222 | |
| 3223 | clip : bool, optional |
| 3224 | If clip is ``True``, out of range values are mapped to 0 if they |
| 3225 | are below ``boundaries[0]`` or mapped to ``ncolors - 1`` if they |
| 3226 | are above ``boundaries[-1]``. |
| 3227 | |
| 3228 | If clip is ``False``, out of range values are mapped to -1 if |
| 3229 | they are below ``boundaries[0]`` or mapped to *ncolors* if they are |
| 3230 | above ``boundaries[-1]``. These are then converted to valid indices |
| 3231 | by `Colormap.__call__`. |
| 3232 | |
| 3233 | extend : {'neither', 'both', 'min', 'max'}, default: 'neither' |
| 3234 | Extend the number of bins to include one or both of the |
| 3235 | regions beyond the boundaries. For example, if ``extend`` |
| 3236 | is 'min', then the color to which the region between the first |
| 3237 | pair of boundaries is mapped will be distinct from the first |
| 3238 | color in the colormap, and by default a |
| 3239 | `~matplotlib.colorbar.Colorbar` will be drawn with |
| 3240 | the triangle extension on the left or lower end. |
| 3241 | |
| 3242 | Notes |
| 3243 | ----- |
| 3244 | If there are fewer bins (including extensions) than colors, then the |
| 3245 | color index is chosen by linearly interpolating the ``[0, nbins - 1]`` |
| 3246 | range onto the ``[0, ncolors - 1]`` range, effectively skipping some |
| 3247 | colors in the middle of the colormap. |
| 3248 | """ |
| 3249 | if clip and extend != 'neither': |
| 3250 | raise ValueError("'clip=True' is not compatible with 'extend'") |
| 3251 | super().__init__(vmin=boundaries[0], vmax=boundaries[-1], clip=clip) |
| 3252 | self.boundaries = np.asarray(boundaries) |
| 3253 | self.N = len(self.boundaries) |
| 3254 | if self.N < 2: |
| 3255 | raise ValueError("You must provide at least 2 boundaries " |
| 3256 | f"(1 region) but you passed in {boundaries!r}") |
| 3257 | self.Ncmap = ncolors |
| 3258 | self.extend = extend |
| 3259 | |
| 3260 | self._scale = None # don't use the default scale. |
| 3261 | |
| 3262 | self._n_regions = self.N - 1 # number of colors needed |
| 3263 | self._offset = 0 |
| 3264 | if extend in ('min', 'both'): |
| 3265 | self._n_regions += 1 |
| 3266 | self._offset = 1 |
| 3267 | if extend in ('max', 'both'): |
| 3268 | self._n_regions += 1 |
| 3269 | if self._n_regions > self.Ncmap: |