Generate a colormap index based on discrete intervals. Unlike `Normalize` or `LogNorm`, `BoundaryNorm` maps values to integers instead of to the interval 0-1.
| 3198 | |
| 3199 | |
| 3200 | class BoundaryNorm(Normalize): |
| 3201 | """ |
| 3202 | Generate a colormap index based on discrete intervals. |
| 3203 | |
| 3204 | Unlike `Normalize` or `LogNorm`, `BoundaryNorm` maps values to integers |
| 3205 | instead of to the interval 0-1. |
| 3206 | """ |
| 3207 | |
| 3208 | # Mapping to the 0-1 interval could have been done via piece-wise linear |
| 3209 | # interpolation, but using integers seems simpler, and reduces the number |
| 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 |
no outgoing calls
searching dependent graphs…