The answer to your question is related to two other SO questions.
Answer to How to choose a new color for each plotted line in a shape in matplotlib? explains how to define a default color list, choose the next color for the plot. This is done using the Axes.set_color_cycle method.
You want the right list of colors, although this is easiest to do using a color map, as explained in the answer to this question: Create a color generator from the given color map in matplotlib . There, the color map takes a value from 0 to 1 and returns the color.
So, for your 20 lines, you want to cycle from 0 to 1 in increments of 1/20. In particular, you want to cycle the form from 0 to 19/20, because 1 maps back to 0.
This is done in this example:
import matplotlib.pyplot as plt import numpy as np NUM_COLORS = 20 cm = plt.get_cmap('gist_rainbow') fig = plt.figure() ax = fig.add_subplot(111) ax.set_color_cycle([cm(1.*i/NUM_COLORS) for i in range(NUM_COLORS)]) for i in range(NUM_COLORS): ax.plot(np.arange(10)*(i+1)) fig.savefig('moreColors.png') plt.show()
This is the result:

Alternative, better (debatable) solution:
There is an alternative way to use the ScalarMappable object to convert a range of values โโinto colors. The advantage of this method is that you can use non-linear Normalization to convert from the row index to the actual color. The following code gives the exact same result:
import matplotlib.pyplot as plt import matplotlib.cm as mplcm import matplotlib.colors as colors import numpy as np NUM_COLORS = 20 cm = plt.get_cmap('gist_rainbow') cNorm = colors.Normalize(vmin=0, vmax=NUM_COLORS-1) scalarMap = mplcm.ScalarMappable(norm=cNorm, cmap=cm) fig = plt.figure() ax = fig.add_subplot(111)
Yann Dec 05 '11 at 20:29 2011-12-05 20:29
source share