matplotlib - only 1 colorbar for multiple pie chart using matlibplot -
i have such plot, , add colorbar code (which color corresponds number) on right hand below. saw example used imshow not pie chart.
#!/usr/bin/env python """ http://matplotlib.sf.net/matplotlib.pylab.html#-pie docstring. """ pylab import * fracs = [33,33,33] starting_angle = 90 axis('equal') item in range(9): color_vals = [-1, 0, 1] my_norm = matplotlib.colors.normalize(-1, 1) # maps data range [0, 1] my_cmap = matplotlib.cm.get_cmap('rdbu') # can pick color map patches, texts, autotexts = pie(fracs, labels = none, autopct='%1.1f%%', startangle=90, colors=my_cmap(my_norm(color_vals))) subplot(3,3,item+1) fracs = [33,33,33] starting_angle = 90 axis('equal') patches, texts, autotexts = pie(fracs, labels = none, autopct='%1.1f%%', startangle=90, colors=my_cmap(my_norm(color_vals))) 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.5) savefig('/home/superiois/downloads/projectx3/grail/pie1.png') show()
also, great if tell me how customize size , location of colorbar code; thanks.
usually legend more appropriate discrete values , colorbar continuous values. said, off course possible since mpl allows create colorbar scratch.
import matplotlib.pyplot plt import matplotlib mpl fracs = [33,33,33] starting_angle = 90 fig, axs = plt.subplots(3,3, figsize=(6,6)) fig.subplots_adjust(hspace=0.1,wspace=0.0) axs = axs.ravel() n in range(9): color_vals = [-1, 0, 1] my_norm = mpl.colors.normalize(-1, 1) # maps data range [0, 1] my_cmap = mpl.cm.get_cmap('rdbu', len(color_vals)) # can pick color map patches, texts, autotexts = axs[n].pie(fracs, labels = none, autopct='%1.1f%%', startangle=90, colors=my_cmap(my_norm(color_vals))) axs[n].set_aspect('equal') item in autotexts: item.set_text("") ax_cb = fig.add_axes([.9,.25,.03,.5]) cb = mpl.colorbar.colorbarbase(ax_cb, cmap=my_cmap, norm=my_norm, ticks=color_vals) cb.set_label('some label [-]') cb.set_ticklabels(['one', 'two', 'three'])
i have added custom ticklabels show how work, default values remove last line.
Comments
Post a Comment