Tuesday 15 May 2012

python - Setting rectangle colour in matplotlib -



python - Setting rectangle colour in matplotlib -

for custom diagram drawing function, need way set colours of rectangles. of code:

# draw many rectangles label in labels: # derive coords... rect = mpatches.rectangle((x1, y1), width, height) rects.append(rect) collection = patchcollection(rects, alpha=0.3) ax.add_collection(collection) plt.savefig('test.png')

i'd set colour rectangles derived label. if phone call function 1 time again (and order of labels changes), same colour should assigned rectangle. don't care colour palette (some distinguishable colours sufficient each label). how go this?

update

the possible labels unknown, can't hardcode colour map. need hash function creates colour (the hash value) based on label input.

if understand question correctly, can dictionary , map.

colordict = {'l1':'red', 'l2':'green', 'l3':'magenta'} # then, plotting: labels = ['l2', 'l1', 'l3'] colorlist = map(colordict.get, labels)

this yields colorlist match loop

['green', 'red', 'magenta']

then, no matter order is, same label end same color. it's define goes ahead of time.

python matplotlib

No comments:

Post a Comment