2012-05-18 128 views
2

我有一個餅圖(示例),其中fracs = [10, 20, 50, 30]。用matplotlib繪製這個沒有問題。我如何獲得第一個楔子(10)的細分爲64?理想情況下,我想要20的第二個楔子,以分解爲10,3,7。這將顯示爲特定楔形或餅圖附近的條形圖(這將使其成爲與Excel中餅圖類似的餅圖)。Matplotlib:繪製餅圖並將楔形分解爲條形圖

回答

3

這是一種做法(可能不是最好的...)。我已經調整了here, on the matplotlib site中的一些代碼來創建little_pie函數,該函數將在任意位置繪製小餅圖。

from pylab import * 
import math 
import numpy as np 

def little_pie(breakdown,location,size): 
    breakdown = [0] + list(np.cumsum(breakdown)* 1.0/sum(breakdown)) 
    for i in xrange(len(breakdown)-1): 
     x = [0] + np.cos(np.linspace(2 * math.pi * breakdown[i], 2 * math.pi *  
          breakdown[i+1], 20)).tolist() 
     y = [0] + np.sin(np.linspace(2 * math.pi * breakdown[i], 2 * math.pi * 
          breakdown[i+1], 20)).tolist() 
     xy = zip(x,y) 
     scatter(location[0], location[1], marker=(xy,0), s=size, facecolor= 
       ['gold','yellow', 'orange', 'red','purple','indigo','violet'][i%7]) 

figure(1, figsize=(6,6)) 

little_pie([10,3,7],(1,1),600) 
little_pie([10,27,4,8,4,5,6,17,33],(-1,1),800) 

fracs = [10, 8, 7, 10] 
explode=(0, 0, 0.1, 0) 
pie(fracs, explode=explode, autopct='%1.1f%%') 
show() 

enter image description here