'하위 그룹을 가지는 도넛 그래프'에 해당되는 글 1건

  1. 2019.01.12 [Python] 원 그래프 (Pie Chart), 하위 그룹을 포함한 도넛 그래프 (Donut Chart with Subgroups) 1

이번 포스팅에서는 시장 점유율과 같이 구성비율을 시각화하는데 사용하는 (1) 원 그래프 (Pie Chart), (2) 하위 그룹을 포함한 도넛 그래프 (Donut Chart with Subgraphs) 그리는 방법을 소개하겠습니다. 


두 개 모두 matplotlib 의 pie() 함수를 사용합니다. 



  (1) 원 그래프 (Pie Chart)



# importing library and set figure size

import matplotlib.pyplot as plt

plt.rcParams['figure.figsize'] = [12, 8]

 



# information of groups

group_names = ['Group_A', 'Group_B', 'Group_C']

group_sizes = [95, 54, 25]

group_colors = ['yellowgreen', 'lightskyblue', 'lightcoral']

group_explodes = (0.1, 0, 0) # explode 1st slice

 



# Pie chart

plt.pie(group_sizes, 

        explode=group_explodes, 

        labels=group_names, 

        colors=group_colors, 

        autopct='%1.2f%%', # second decimal place

        shadow=True, 

        startangle=90,

        textprops={'fontsize': 14}) # text font size

plt.axis('equal') #  equal length of X and Y axis

plt.title('Pie Chart of Market Share', fontsize=20)

plt.show()




  (2) 하위 그룹을 포함한 도넛 그래프 (Donut Chart with Subgraphs)


상위 그룹 A, B, C가 있고, 각 상위 그룹에 딸린 하위 그룹으로 'A_1'~'A_4', 'B_1'~'B_3', 'C_1'~'C_2' 의 하위 그룹(subgroups)이 있는 경우 가운데가 뚫린 도넛 모양의 도넛 그래프로 표현해주면 효과적입니다. 


기본 원리는 간단합니다. matplotlib 으로 원 그래프를 그리되, radius 와 width 의 숫자를 적절히 조절하여 (a) 바깥쪽의 상위 그룹의 원 그래프를 반지름이 크고 폭은 작아서 가운데가 뚫린 도넛 그래프로 만들고, (b) 안쪽에는 하위 그룹의 원 그래프를 반지름이 상위 그룹의 것보다는 작고(즉, 상위 그룹 도넛 그래프의 폭 만큼을 빼줌) 폭은 상위 그룹과 같은 가운데가 뚫린 도넛 그래프를 만들여서, (a)와 (b)의 두개 도넛 그래프를 겹치게 그리는 것입니다. 



# importing library and set figure size

import matplotlib.pyplot as plt

plt.rcParams['figure.figsize'] = [12, 8]

 



 # info. of groups

group_names = ['Group_A', 'Group_B', 'Group_C']

group_sizes = [95, 54, 25]


# info. of subgroups

subgroup_names = ['A_1', 'A_2', 'A_3', 'A_4', 

                  'B_1', 'B_2', 'B_3', 

                  'C_1', 'C_2']

subgroup_sizes = [50, 30, 10, 5, 30, 20, 4, 20, 5]


# colors

a, b, c = [plt.cm.Reds, plt.cm.Greens, plt.cm.Blues]


# width

width_num = 0.4

 



# Outside Ring

fig, ax = plt.subplots()

ax.axis('equal')

pie_outside, _ = ax.pie(group_sizes, 

                        radius=1.3

                        labels=group_names, 

                        labeldistance=0.8,

                        colors=[a(0.6), b(0.6), c(0.6)])

plt.setp(pie_outside

         width=width_num

         edgecolor='white')


# Inside Ring

pie_inside, plt_labels, junk = \

    ax.pie(subgroup_sizes, 

           radius=(1.3 - width_num), 

           labels=subgroup_names, 

           labeldistance=0.75, 

           autopct='%1.1f%%', 

           colors=[a(0.5), a(0.4), a(0.3), a(0.2), 

                   b(0.5), b(0.4), b(0.3), 

                   c(0.5), c(0.4)])

plt.setp(pie_inside

         width=width_num

         edgecolor='white')

plt.title('Donut Plot with Subgroups', fontsize=20)

plt.show()

 



많은 도움이 되었기를 바랍니다. 


이번 포스팅이 도움이 되었다면 아래의 '공감~'를 꾹 눌러주세요. :-)



728x90
반응형
Posted by Rfriend
,