python

원그래프 그리기 / 가장 많은 부분 강조

jaeha_lee 2024. 1. 24. 20:30

아래 코드 output

 

import seaborn as sns
import matplotlib.pyplot as plt

# 샘플 데이터 생성
data1 = [40, 30, 20, 10]
data2 = [30, 20, 25, 25]
data3 = [55, 25, 35, 25]

# 가장 많은 비율을 가진 항목을 강조하기 위해 explode 설정
explode = (0.1, 0, 0, 0)  # 첫 번째 항목을 10% 떼어냄

# 색상 지정
colors = ['#FFD700', '#D3D3D3', '#C0C0C0', '#A9A9A9']

# 그래프 스타일 설정
sns.set(style="whitegrid")

# 여러 개의 subplot 생성
fig, axes = plt.subplots(1, 3, figsize=(18, 6))

# 첫 번째 subplot에 원 그래프 그리기
axes[0].pie(data1, labels=["Category A", "Category B", "Category C", "Category D"], autopct='%1.1f%%',
            startangle=140, explode=explode, colors=colors,
            textprops={'fontsize': 14, 'fontweight': 'bold', 'color': 'white'})
axes[0].set_title("Graph 1")

# 두 번째 subplot에 원 그래프 그리기
axes[1].pie(data2, labels=None, autopct='%1.1f%%', startangle=140, explode=explode, colors=colors,
            textprops={'fontsize': 14, 'fontweight': 'bold', 'color': 'white'})
axes[1].set_title("Graph 2")

# 세 번째 subplot에 원 그래프 그리기
axes[2].pie(data3, labels=None, autopct='%1.1f%%', startangle=140, explode=explode, colors=colors,
            textprops={'fontsize': 14, 'fontweight': 'bold', 'color': 'white'})
axes[2].set_title("Graph 3")

# subplot 간 간격 조절
plt.tight_layout()

# 그래프 표시
plt.show()