2013-12-12 4 views
8

matplotlib 원형 차트에서 "폭발"옵션을 정말 좋아합니다. 나는 "그룹에서 폭발"할 수 있기를 바랬다. 3 개 또는 4 개의 카테고리로 분류되는 많은 조각을 계획하고 있습니다. 그룹으로 모든 작은 조각을 함께 폭발시키고 싶습니다. matplotlib에서 여러 조각의 파이를 함께 분해하십시오

나는 완전히 분명하지 않다 파악, 그래서는 기본 모양을 그려 내 심하게 부족한 능력에 전화하고이 그림은 내가 할 노력하고있어 보여 만들어진 :

Grouped, Exploded Pie

작은 유의하시기 바랍니다 그룹들 사이의 음영이없는 틈.

이것이 가능합니까?

덕분에 많은

, 알렉스

답변

8

내가 그룹화 폭발 파이를 지정할 수있는 직접적인 방법을 알고 아니지만,이 같은 그룹과 파이를 다시 그리기 위해 패치를 사용하는 것은 매우 간단하다

# original part (left) 
import numpy as np 
import matplotlib.pyplot as plt 
f,ax = plt.subplots(1,2) 
ax[0].set_aspect('equal') 
data=np.abs(np.random.randn(7)) 
wedges, texts = ax[0].pie(data) 

# redraw plot using patches (right) 
import matplotlib.patches as mpatches 
from matplotlib.collections import PatchCollection 
ax[1].set_aspect('equal') 
ax[1].axis('off') 

groups=[[0,1,2],[3,4],[5,6]] 
radfraction = 0.1 
patches = [] 
for i in groups: 
    ang = np.deg2rad((wedges[i[-1]].theta2 + wedges[i[0]].theta1)/2,) 
    for j in i: 
    we = wedges[j] 
    center = (radfraction*we.r*np.cos(ang), radfraction*we.r*np.sin(ang)) 
    patches.append(mpatches.Wedge(center, we.r, we.theta1, we.theta2)) 

colors = np.linspace(0, 1, len(patches)) 
collection = PatchCollection(patches, cmap=plt.cm.hsv) 
collection.set_array(np.array(colors)) 
ax[1].add_collection(collection) 
ax[1].autoscale(True) 

이것은 물론, 당신은 단순히 대신 미리 계산 된 것들을 사용하여 데이터를 기반으로 필요한 세타 각도를 계산할 수
enter image description here

처럼 음모가 발생할 수 있습니다.

관련 문제