1

Assume, I have a pie chart as following; and I would like to color the slices based on some values in a vector, as a gradient between blue to red. Blue for -1, and red +1, there is no value greater than 1 nor less than -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]

for 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()
Areza
  • 5,623
  • 7
  • 48
  • 79

1 Answers1

1
fracs = [33,33,33]
starting_angle = 90
axis('equal')

color_vals = [-1, 0, 1]

my_norm = matplotlib.colors.Normalize(-1, 1) # maps your data to the range [0, 1]
my_cmap = matplotlib.cm.get_cmap('jet') # can pick your color map

patches, texts, autotexts = pie(fracs, labels = None, autopct='%1.1f%%', startangle=90,
                colors=my_cmap(my_norm(color_vals)))

print patches[2]

for 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 the color of the patches with the colors kwarg. You can leverage the existing color mapping code (used in imshow for example) to generate the gradient you want. The Normalize object takes care of normalizing your data to the range that the color maps expect as input, and the color map converts the float to a color.

Normalize doc

pie doc

ColorMap doc

tacaswell
  • 84,579
  • 22
  • 210
  • 199