2013-05-21 32 views
0

我有這樣的情節,並且想在右下方添加一個彩條代碼(哪種顏色對應於什麼數字)。我看到了一些用於imshow而不是餅圖的例子。只有1個彩條用於使用matlibplot的多個餅圖

#!/usr/bin/env python 
""" 
http://matplotlib.sf.net/matplotlib.pylab.html#-pie for the docstring. 
""" 
from pylab import * 

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

for item in range(9): 
    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('RdBu') # can pick your 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))) 


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.5) 

savefig('/home/superiois/Downloads/projectx3/GRAIL/pie1.png') 
show() 

此外,如果您告訴我如何自定義顏色條代碼的大小和位置,謝謝。

回答

2

通常情況下,圖例更適合離散值和連續值的顏色條。也就是說,自mpl以來它可能會出現,因此您可以從頭開始創建一個色條。

import matplotlib.pyplot as plt 
import matplotlib as 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() 

for n in range(9): 
    color_vals = [-1, 0, 1] 
    my_norm = mpl.colors.Normalize(-1, 1) # maps your data to the range [0, 1] 
    my_cmap = mpl.cm.get_cmap('RdBu', len(color_vals)) # can pick your 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') 

    for 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']) 

我已經添加了自定義標籤,以顯示如何工作,以獲得默認值只是刪除最後一行。

enter image description here