Based on my knowledge of the matplotlib codebase and the issue description, I can construct the patch. The issue is in the `__call__` method of the `Colormap` class in `lib/matplotlib/colors.py`. The fix needs to promote `xa` to a dtype that can hold values up to `N + 2` before assigning `_i_over`, `_i_under`, and `_i_bad`. Here's the patch: --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -717,6 +717,17 @@ class Colormap: xa = xa.astype(int) # Set the over-range indices before the under-range; # otherwise the under-range values get converted to over-range. + # Ensure xa can hold N + 2 values (for _i_over, _i_under, _i_bad) + # by promoting to a larger dtype if necessary. + if xa.dtype == np.uint8: + # uint8 can only hold values 0-255, but we need to store + # _i_over (N), _i_under (N+1), _i_bad (N+2) which are 256, 257, 258 + # for a colormap with N=256. + xa = xa.astype(np.int16) + elif xa.dtype.kind in ('u', 'i'): + # For other integer types, ensure we can hold N + 2 + if np.iinfo(xa.dtype).max < self.N + 2: + xa = xa.astype(np.int32) xa[xa > self.N - 1] = self._i_over xa[xa < 0] = self._i_under xa[mask_bad] = self._i_bad