pufferfish
pufferfish

Reputation: 17415

Array of colors in python

What's the quickest way to get an array of colors in python? Something I can index and pass to as the "color=" argument plotting in pylab.

The best I can come up with is:

colors = [(random(),random(),random()) for i in range(10)]

but a solution that can generate well-spaced colors (interpolated?) would be preferable.

Upvotes: 6

Views: 51644

Answers (3)

Umur Kontacı
Umur Kontacı

Reputation: 35478

from random import randint
colors = []

for i in range(10):
    colors.append('#%06X' % randint(0, 0xFFFFFF))

Result Example:

['#37AB65', '#3DF735', '#AD6D70', '#EC2504', '#8C0B90', '#C0E4FF', '#27B502', '#7C60A8', '#CF95D7', '#145JKH']

ps: thanks to @Inerdia

Upvotes: 18

millimoose
millimoose

Reputation: 39950

It seems like matplotlib comes with several builtin "colormaps". You can get one using get_cmap().

Upvotes: 5

Griffin
Griffin

Reputation: 644

You can simply make a list or a dictionary and add the colors to it, I don't know how pylab works but I found an example which might work for you here.

from pylab import *
cdict = {'red': ((0.0, 0.0, 0.0),
                 (0.5, 1.0, 0.7),
                 (1.0, 1.0, 1.0)),
         'green': ((0.0, 0.0, 0.0),
                   (0.5, 1.0, 0.0),
                   (1.0, 1.0, 1.0)),
         'blue': ((0.0, 0.0, 0.0),
                  (0.5, 1.0, 0.0),
                  (1.0, 0.5, 1.0))}
my_cmap = matplotlib.colors.LinearSegmentedColormap('my_colormap',cdict,256)
pcolor(rand(10,10),cmap=my_cmap)
colorbar()

Upvotes: 2

Related Questions