Repository navigation
Expand file tree
/
Copy pathchartEx1.py
More file actions
93 lines (78 loc) · 3.6 KB
/
Copy pathchartEx1.py
File metadata and controls
93 lines (78 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 선 그래프와 막대 그래프 — 가장 많이 쓰는 두 가지
import matplotlib.pyplot as plt
import krfont
krfont.apply(verbose=False)
SHOW = False
월 = list(range(1, 13))
매출2019 = [355, 201, 268, 302, 142, 327, 257, 221, 187, 240, 250, 247]
매출2018 = [280, 240, 250, 290, 210, 300, 280, 260, 230, 255, 262, 300]
print('========== 선 그래프 : 시간에 따른 변화 ==========')
fig, ax = plt.subplots(figsize=(9, 4))
ax.plot(월, 매출2019, marker='o', label='2019', linewidth=2)
ax.plot(월, 매출2018, marker='s', label='2018', linestyle='--', alpha=0.7)
ax.set_title('연도별 월 매출 추이')
ax.set_xlabel('월')
ax.set_ylabel('매출 (천원)')
ax.set_xticks(월)
ax.legend()
ax.grid(alpha=0.3)
fig.savefig('output/10_line.png', dpi=100, bbox_inches='tight')
plt.close(fig)
print('10_line.png — 선은 "흐름" 을 보여 줍니다. x축이 시간일 때 씁니다.')
print('\n========== 막대 그래프 : 항목 간 비교 ==========')
분류 = ['음료', '베이커리', '디저트']
매출 = [1484, 762, 756]
fig, ax = plt.subplots(figsize=(6, 4))
bars = ax.bar(분류, 매출, color=['#4C72B0', '#DD8452', '#55A868'])
ax.set_title('분류별 매출')
ax.set_ylabel('매출 (천원)')
# 막대 위에 값 표시 — 그래프만으로는 정확한 값을 못 읽습니다
for bar, value in zip(bars, 매출):
ax.text(bar.get_x() + bar.get_width() / 2, value + 20,
f'{value:,}', ha='center', fontsize=10)
ax.set_ylim(0, max(매출) * 1.15) # 값 표시가 잘리지 않게 위쪽 여유
fig.savefig('output/11_bar.png', dpi=100, bbox_inches='tight')
plt.close(fig)
print('11_bar.png — 막대는 "크기 비교" 입니다. 항목이 범주형일 때 씁니다.')
print('\n========== 가로 막대 : 항목 이름이 길 때 ==========')
상품 = ['아메리카노', '카페라떼', '녹차라떼', '치즈케이크', '초코머핀']
판매 = [98, 76, 64, 52, 41]
fig, ax = plt.subplots(figsize=(6, 4))
ax.barh(상품[::-1], 판매[::-1], color='#4C72B0') # 뒤집어야 1위가 위로 온다
ax.set_title('상품별 판매량 TOP 5')
ax.set_xlabel('판매량')
fig.savefig('output/12_barh.png', dpi=100, bbox_inches='tight')
plt.close(fig)
print('12_barh.png — 이름이 길면 세로 막대에서 겹칩니다. 가로로 눕히세요.')
print('\n========== 누적 막대 : 전체와 구성을 함께 ==========')
지역 = ['서울', '경기', '부산', '대구', '광주']
매장 = [218, 237, 211, 146, 187]
배달 = [222, 219, 192, 169, 222]
포장 = [253, 83, 84, 156, 207]
fig, ax = plt.subplots(figsize=(7, 4))
ax.bar(지역, 매장, label='매장')
ax.bar(지역, 배달, bottom=매장, label='배달') # 아래에 쌓는다
ax.bar(지역, 포장, bottom=[m + d for m, d in zip(매장, 배달)], label='포장')
ax.set_title('지역별 채널 매출 (누적)')
ax.set_ylabel('매출 (천원)')
ax.legend()
fig.savefig('output/13_stacked.png', dpi=100, bbox_inches='tight')
plt.close(fig)
print('13_stacked.png — bottom 으로 아래에 무엇이 깔렸는지 알려 줍니다.')
print('\n========== 그룹 막대 : 항목끼리 직접 비교 ==========')
import numpy as np
x = np.arange(len(지역))
w = 0.25
fig, ax = plt.subplots(figsize=(8, 4))
ax.bar(x - w, 매장, w, label='매장')
ax.bar(x, 배달, w, label='배달')
ax.bar(x + w, 포장, w, label='포장')
ax.set_xticks(x)
ax.set_xticklabels(지역)
ax.set_title('지역별 채널 매출 (그룹)')
ax.legend()
fig.savefig('output/14_grouped.png', dpi=100, bbox_inches='tight')
plt.close(fig)
print('14_grouped.png — 누적은 "합계", 그룹은 "항목별 비교" 에 낫습니다.')
if SHOW:
plt.show()