1

Consider this example

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.scatter([1,2,3], [1,2,3], c=[0,1,2], cmap=plt.cm.get_cmap("brg"), s=100)

enter image description here

How can I see the RGBA values selected by this color map? Note that the docs state

LinearSegmentedColormaps do not have a .colors attribute. However, one may still call the colormap with an integer array, or with a float array between 0 and 1.

However, when I attempt this, it does not appear to work.

import numpy as np

cmap = plt.cm.get_cmap("brg")
cmap(np.array([0,1,2], dtype='int64'))
array([[0.        , 0.        , 1.        , 1.        ],
       [0.00784314, 0.        , 0.99215686, 1.        ],
       [0.01568627, 0.        , 0.98431373, 1.        ]])

These RGBA values don't align with what I expect (blue, red, green).

Ben
  • 20,038
  • 30
  • 112
  • 189
  • 1
    Does this answer your question? [How can I generate a colormap array from a simple array in matplotlib](https://stackoverflow.com/questions/28144142/how-can-i-generate-a-colormap-array-from-a-simple-array-in-matplotlib) – Chris Mar 29 '22 at 00:11
  • 1
    Matplotlib uses a ["norm"](https://matplotlib.org/stable/tutorials/colors/colormapnorms.html) to convert the `c=` values from their current range to `0...1`. By default, the lowest of the values in `c=` will be converted to `0` for the "lowest" color (blue in this case) and the highest value of `c=` will be converted to `1` to represent the "highest" color. The rest will go linearly in between. If you just call `cmap(...)` using an array, when the array is of float type, they are interpreted to from 0 (lowest) to 1 (highest). If, however, you use integers, they go from 0 to 255. – JohanC Mar 29 '22 at 00:25
  • You could use `cmap(np.linspace(0,1,256))`, or `cmap(np.arange(0, 256))` to obtain the full list of colors. Note that matplotlib precalculates these 256 colors for this "LinearSegmentedColormap". – JohanC Mar 29 '22 at 00:26
  • Thank you both. Quite helpful - I searched but couldn't find this. – Ben Mar 29 '22 at 00:28

0 Answers0