2011-01-28

Color control with matplotlib

Sometimes it's important to control the color of lines drawn with matplotlib. Two examples I encountered today were drawing a number of horizontal or vertical lines on the same axes and drawing more lines than the number of unique colors in the default color ring. Here's how you can use a colormap to get the desired number of colors:

import matplotlib.pyplot as plt
cmap = plt.get_cmap ('gist_rainbow')
num_colors = 10
for i in xrange (num_colors):
    plt.axhline (i, color = cmap (1.0 * i / num_colors))