NUM_COLORS = 22
cm = pylab.get_cmap('gist_rainbow')
for i in range(NUM_COLORS):
    color = cm(1.0 * i / NUM_COLORS)
cgen = (cm(1.0 * i / NUM_COLORS) for i in range(NUM_COLORS))
Create a color generator from given colormap in matplotlib