visualization - Coloring slices of pie chart in matplotlib python -


assume, have pie chart following; , color slices based on values in vector, gradient between blue red. blue -1, , red +1, there no value greater 1 nor less -1.

fracs = [33,33,33] starting_angle = 90 axis('equal') patches, texts, autotexts = pie(fracs, labels = none, autopct='%1.1f%%', startangle=90)  print patches[2]  item in autotexts:     item.set_text("")  subplots_adjust(left=0.125, bottom=0.1, right=0.9, top=0.9, wspace=0.0, hspace=-0.4)   savefig('/home/superiois/downloads/projectx3/grail/pie2') show() 

fracs = [33,33,33] starting_angle = 90 axis('equal')  color_vals = [-1, 0, 1]  my_norm = matplotlib.colors.normalize(-1, 1) # maps data range [0, 1] my_cmap = matplotlib.cm.get_cmap('jet') # can pick color map  patches, texts, autotexts = pie(fracs, labels = none, autopct='%1.1f%%', startangle=90,                 colors=my_cmap(my_norm(color_vals)))  print patches[2]  item in autotexts:     item.set_text("")   subplots_adjust(left=0.125, bottom=0.1, right=0.9, top=0.9, wspace=0.0, hspace=-0.4)  show() 

you can set color of patches colors kwarg. can leverage existing color mapping code (used in imshow example) generate gradient want. normalize object takes care of normalizing data range color maps expect input, , color map converts float color.

normalize doc

pie doc

colormap doc


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -