본문 바로가기

AI Tech

Matplotlib Pie Chart

 

Pie and polar charts — Matplotlib 3.9.2 documentation

 

matplotlib.org

Pie Chart (파이 차트) 만들기

import matplotlib.pyplot as plt

# 데이터 준비
sizes = [15, 30, 45, 10]  # 각 조각의 비율
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'  # 각 조각의 레이블

# 파이 차트 생성
fig, ax = plt.subplots()
ax.pie(sizes, labels=labels, autopct='%1.1f%%')

# 제목 추가
ax.set_title('Basic Pie Chart')

plt.show()

 

sizeslabels로 크기와 레이블을 전달한다.

autopct를 사용하여 슬라이스의 백분율 크기를 출력한다.

 

ax.pie(sizes, labels=labels,
       colors=['olivedrab', 'rosybrown', 'gray', 'saddlebrown'])

colors에 리스트를 전달하여 색깔을 변경한다.

 

ax.pie(sizes, labels=labels, autopct='%1.1f%%',
       pctdistance=1.25, labeldistance=.6)

pctdistance로 앞서 출력한 백분율을 어디에 출력할지 정해준다.

labeldistance로 Label을 어디에 출력할지 정해준다.

0: 파이차트의 중앙의 위치

1: 파이차트의 가장자리

1보다 클경우 파이차트 밖으로 나가게 된다.

 

explode = (0, 0.1, 0, 0)  # only "explode" the 2nd slice (i.e. 'Hogs')

ax.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
       shadow=True, startangle=90)

explode를 사용하여 파이차트의 특정 부분을 강조 할 수 있다.

startangle을 조절하여 파이차트의 시작 위치를 조절할 수 있다. (반시계 방향으로 증가한다.)

shadow를 사용하여 차트에 그림자 효과를 줄 수 있다.

shadow={'ox': -0.04, 'edgecolor': 'none', 'shade': 0.9}

shadow 내부의 값들을 조절해주어 다양한 shadow를 만들어 낼 수 있다.