extract RGB values from linear colourmap made using LinearSegmentedColormap
<p>I would like to create a linear colourmap from a list of discrete colors, and extract the underlying RGB values. I've managed to do the first step using the example script in the matplotlib document.</p>

<pre><code>from matplotlib import cm
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1)]
colormap = LinearSegmentedColormap.from_list('colormapX', colors, N=100)

x = np.arange(0, np.pi, 0.1)
y = np.arange(0, 2*np.pi, 0.1)
X, Y = np.meshgrid(x, y)
Z = np.cos(X) * np.sin(Y) * 10

fig, ax = plt.subplots()
im = ax.imshow(Z, interpolation='nearest', origin='lower', cmap=colormap)
fig.colorbar(im, ax=ax)
plt.show()
</code></pre>

<p>This colormap is based on 100 colours that were derived from the interpolation of the the original three colours. How do I extract an ndarray with the RGB values of these 100 colours?</p>

<p>Don't know if it's available directly somewhere, or if one can ask the <code>colormap</code> to evaluate a given value (return the colour corresponding to a given number), but you can make the list yourself in this simple case:</p>

<pre><code>def color_interpolation(c1, c2, fraction=0.5):
    return ((1.-fraction)*c1[0] + fraction*c2[0],
            (1.-fraction)*c1[1] + fraction*c2[1],
            (1.-fraction)*c1[2] + fraction*c2[2],)

def make_color_interpolation_list(colors, N):
    n_colors = len(colors)
    n_steps_between_colors = N/(n_colors-1)
    fraction_step = 1. / n_steps_between_colors
    color_array = np.zeros((N,3))
    color_index = 0
    while color_index &lt; n_colors-1:
        fraction_index = 0
        while fraction_index &lt; n_steps_between_colors:
            index = color_index*n_steps_between_colors+fraction_index
            color_array[index]= color_interpolation(c1=colors[color_index],
                                    c2=colors[color_index+1],
                                    fraction=fraction_index*fraction_step)
            fraction_index += 1
        color_index += 1
    if index != len(color_array)-1:
        color_array[-1] = colors[-1]
    return color_array
</code></pre>

python matplotlib colormap