1"""
2๋ฐ์ดํฐ ์๊ฐํ (Data Visualization)
3Matplotlib and Seaborn Examples
4
5๋ฐ์ดํฐ๋ฅผ ์๊ฐ์ ์ผ๋ก ํํํ๋ ๋ฐฉ๋ฒ์ ๋ค๋ฃน๋๋ค.
6"""
7
8import numpy as np
9import pandas as pd
10import matplotlib.pyplot as plt
11
12# Seaborn์ ์ ํ์ (์์ผ๋ฉด ๊ฑด๋๋)
13try:
14 import seaborn as sns
15 HAS_SEABORN = True
16except ImportError:
17 HAS_SEABORN = False
18 print("Seaborn not installed. Some examples will be skipped.")
19
20
21# =============================================================================
22# 1. ๊ธฐ๋ณธ ์ ๊ทธ๋ํ
23# =============================================================================
24def line_plot():
25 """์ ๊ทธ๋ํ"""
26 print("\n[1] ์ ๊ทธ๋ํ (Line Plot)")
27 print("=" * 50)
28
29 # ๋ฐ์ดํฐ ์ค๋น
30 x = np.linspace(0, 2 * np.pi, 100)
31 y1 = np.sin(x)
32 y2 = np.cos(x)
33
34 # ๊ทธ๋ํ ์์ฑ
35 plt.figure(figsize=(10, 6))
36
37 plt.plot(x, y1, 'b-', label='sin(x)', linewidth=2)
38 plt.plot(x, y2, 'r--', label='cos(x)', linewidth=2)
39
40 plt.title('์ผ๊ฐ ํจ์ ๊ทธ๋ํ', fontsize=14)
41 plt.xlabel('x', fontsize=12)
42 plt.ylabel('y', fontsize=12)
43 plt.legend(loc='upper right')
44 plt.grid(True, alpha=0.3)
45 plt.xlim(0, 2 * np.pi)
46 plt.ylim(-1.5, 1.5)
47
48 plt.tight_layout()
49 plt.savefig('/opt/projects/01_Personal/03_Study/Data_Analysis/examples/01_line_plot.png', dpi=150)
50 plt.close()
51 print("์ ์ฅ: 01_line_plot.png")
52
53
54# =============================================================================
55# 2. ์ฐ์ ๋
56# =============================================================================
57def scatter_plot():
58 """์ฐ์ ๋ (Scatter Plot)"""
59 print("\n[2] ์ฐ์ ๋ (Scatter Plot)")
60 print("=" * 50)
61
62 np.random.seed(42)
63
64 # ๋ฐ์ดํฐ ์์ฑ
65 n = 100
66 x = np.random.randn(n)
67 y = 2 * x + 1 + np.random.randn(n) * 0.5
68 colors = np.random.rand(n)
69 sizes = np.random.rand(n) * 200
70
71 plt.figure(figsize=(10, 6))
72
73 scatter = plt.scatter(x, y, c=colors, s=sizes, alpha=0.6, cmap='viridis')
74 plt.colorbar(scatter, label='์์ ๊ฐ')
75
76 plt.title('์ฐ์ ๋ ์์ ', fontsize=14)
77 plt.xlabel('X', fontsize=12)
78 plt.ylabel('Y', fontsize=12)
79 plt.grid(True, alpha=0.3)
80
81 plt.tight_layout()
82 plt.savefig('/opt/projects/01_Personal/03_Study/Data_Analysis/examples/02_scatter_plot.png', dpi=150)
83 plt.close()
84 print("์ ์ฅ: 02_scatter_plot.png")
85
86
87# =============================================================================
88# 3. ๋ง๋ ๊ทธ๋ํ
89# =============================================================================
90def bar_plot():
91 """๋ง๋ ๊ทธ๋ํ"""
92 print("\n[3] ๋ง๋ ๊ทธ๋ํ (Bar Plot)")
93 print("=" * 50)
94
95 categories = ['A', 'B', 'C', 'D', 'E']
96 values1 = [23, 45, 56, 78, 32]
97 values2 = [17, 38, 42, 65, 28]
98
99 x = np.arange(len(categories))
100 width = 0.35
101
102 fig, axes = plt.subplots(1, 2, figsize=(14, 5))
103
104 # ๊ทธ๋ฃนํ๋ ๋ง๋ ๊ทธ๋ํ
105 ax = axes[0]
106 ax.bar(x - width/2, values1, width, label='๊ทธ๋ฃน1', color='steelblue')
107 ax.bar(x + width/2, values2, width, label='๊ทธ๋ฃน2', color='coral')
108 ax.set_xlabel('์นดํ
๊ณ ๋ฆฌ')
109 ax.set_ylabel('๊ฐ')
110 ax.set_title('๊ทธ๋ฃนํ๋ ๋ง๋ ๊ทธ๋ํ')
111 ax.set_xticks(x)
112 ax.set_xticklabels(categories)
113 ax.legend()
114
115 # ๋์ ๋ง๋ ๊ทธ๋ํ
116 ax = axes[1]
117 ax.bar(categories, values1, label='๊ทธ๋ฃน1', color='steelblue')
118 ax.bar(categories, values2, bottom=values1, label='๊ทธ๋ฃน2', color='coral')
119 ax.set_xlabel('์นดํ
๊ณ ๋ฆฌ')
120 ax.set_ylabel('๊ฐ')
121 ax.set_title('๋์ ๋ง๋ ๊ทธ๋ํ')
122 ax.legend()
123
124 plt.tight_layout()
125 plt.savefig('/opt/projects/01_Personal/03_Study/Data_Analysis/examples/03_bar_plot.png', dpi=150)
126 plt.close()
127 print("์ ์ฅ: 03_bar_plot.png")
128
129
130# =============================================================================
131# 4. ํ์คํ ๊ทธ๋จ
132# =============================================================================
133def histogram():
134 """ํ์คํ ๊ทธ๋จ"""
135 print("\n[4] ํ์คํ ๊ทธ๋จ (Histogram)")
136 print("=" * 50)
137
138 np.random.seed(42)
139
140 # ์ ๊ท ๋ถํฌ ๋ฐ์ดํฐ
141 data1 = np.random.normal(0, 1, 1000)
142 data2 = np.random.normal(2, 1.5, 1000)
143
144 fig, axes = plt.subplots(1, 2, figsize=(14, 5))
145
146 # ๊ธฐ๋ณธ ํ์คํ ๊ทธ๋จ
147 ax = axes[0]
148 ax.hist(data1, bins=30, alpha=0.7, color='steelblue', edgecolor='white')
149 ax.set_xlabel('๊ฐ')
150 ax.set_ylabel('๋น๋')
151 ax.set_title('๊ธฐ๋ณธ ํ์คํ ๊ทธ๋จ')
152
153 # ๊ฒน์น ํ์คํ ๊ทธ๋จ
154 ax = axes[1]
155 ax.hist(data1, bins=30, alpha=0.5, label='ฮผ=0, ฯ=1', color='blue')
156 ax.hist(data2, bins=30, alpha=0.5, label='ฮผ=2, ฯ=1.5', color='red')
157 ax.set_xlabel('๊ฐ')
158 ax.set_ylabel('๋น๋')
159 ax.set_title('๋ ๋ถํฌ ๋น๊ต')
160 ax.legend()
161
162 plt.tight_layout()
163 plt.savefig('/opt/projects/01_Personal/03_Study/Data_Analysis/examples/04_histogram.png', dpi=150)
164 plt.close()
165 print("์ ์ฅ: 04_histogram.png")
166
167
168# =============================================================================
169# 5. ํ์ด ์ฐจํธ
170# =============================================================================
171def pie_chart():
172 """ํ์ด ์ฐจํธ"""
173 print("\n[5] ํ์ด ์ฐจํธ (Pie Chart)")
174 print("=" * 50)
175
176 labels = ['Python', 'JavaScript', 'Java', 'C++', 'Other']
177 sizes = [35, 25, 20, 10, 10]
178 explode = (0.1, 0, 0, 0, 0) # Python ๊ฐ์กฐ
179 colors = plt.cm.Pastel1(np.linspace(0, 1, len(labels)))
180
181 fig, axes = plt.subplots(1, 2, figsize=(14, 6))
182
183 # ๊ธฐ๋ณธ ํ์ด ์ฐจํธ
184 ax = axes[0]
185 ax.pie(sizes, explode=explode, labels=labels, colors=colors,
186 autopct='%1.1f%%', shadow=True, startangle=90)
187 ax.set_title('ํ๋ก๊ทธ๋๋ฐ ์ธ์ด ์ ์ ์จ')
188
189 # ๋๋ ์ฐจํธ
190 ax = axes[1]
191 wedges, texts, autotexts = ax.pie(sizes, labels=labels, colors=colors,
192 autopct='%1.1f%%', startangle=90,
193 wedgeprops=dict(width=0.5))
194 ax.set_title('๋๋ ์ฐจํธ')
195
196 plt.tight_layout()
197 plt.savefig('/opt/projects/01_Personal/03_Study/Data_Analysis/examples/05_pie_chart.png', dpi=150)
198 plt.close()
199 print("์ ์ฅ: 05_pie_chart.png")
200
201
202# =============================================================================
203# 6. ๋ฐ์ค ํ๋กฏ๊ณผ ๋ฐ์ด์ฌ๋ฆฐ ํ๋กฏ
204# =============================================================================
205def box_violin_plot():
206 """๋ฐ์ค ํ๋กฏ๊ณผ ๋ฐ์ด์ฌ๋ฆฐ ํ๋กฏ"""
207 print("\n[6] ๋ฐ์ค ํ๋กฏ๊ณผ ๋ฐ์ด์ฌ๋ฆฐ ํ๋กฏ")
208 print("=" * 50)
209
210 np.random.seed(42)
211
212 # ๋ฐ์ดํฐ ์์ฑ
213 data = [np.random.normal(0, std, 100) for std in range(1, 5)]
214
215 fig, axes = plt.subplots(1, 2, figsize=(14, 5))
216
217 # ๋ฐ์ค ํ๋กฏ
218 ax = axes[0]
219 bp = ax.boxplot(data, patch_artist=True)
220 colors = ['lightblue', 'lightgreen', 'lightyellow', 'lightpink']
221 for patch, color in zip(bp['boxes'], colors):
222 patch.set_facecolor(color)
223 ax.set_xticklabels(['ฯ=1', 'ฯ=2', 'ฯ=3', 'ฯ=4'])
224 ax.set_xlabel('๊ทธ๋ฃน')
225 ax.set_ylabel('๊ฐ')
226 ax.set_title('๋ฐ์ค ํ๋กฏ')
227
228 # ๋ฐ์ด์ฌ๋ฆฐ ํ๋กฏ
229 ax = axes[1]
230 vp = ax.violinplot(data, showmeans=True, showmedians=True)
231 ax.set_xticks([1, 2, 3, 4])
232 ax.set_xticklabels(['ฯ=1', 'ฯ=2', 'ฯ=3', 'ฯ=4'])
233 ax.set_xlabel('๊ทธ๋ฃน')
234 ax.set_ylabel('๊ฐ')
235 ax.set_title('๋ฐ์ด์ฌ๋ฆฐ ํ๋กฏ')
236
237 plt.tight_layout()
238 plt.savefig('/opt/projects/01_Personal/03_Study/Data_Analysis/examples/06_box_violin.png', dpi=150)
239 plt.close()
240 print("์ ์ฅ: 06_box_violin.png")
241
242
243# =============================================================================
244# 7. ํํธ๋งต
245# =============================================================================
246def heatmap():
247 """ํํธ๋งต"""
248 print("\n[7] ํํธ๋งต (Heatmap)")
249 print("=" * 50)
250
251 np.random.seed(42)
252
253 # ์๊ด ํ๋ ฌ ๋ฐ์ดํฐ
254 data = np.random.randn(5, 5)
255 data = (data + data.T) / 2 # ๋์นญ ํ๋ ฌ
256 np.fill_diagonal(data, 1)
257
258 fig, axes = plt.subplots(1, 2, figsize=(14, 5))
259
260 # Matplotlib ํํธ๋งต
261 ax = axes[0]
262 im = ax.imshow(data, cmap='coolwarm', aspect='auto')
263 ax.set_xticks(range(5))
264 ax.set_yticks(range(5))
265 ax.set_xticklabels(['A', 'B', 'C', 'D', 'E'])
266 ax.set_yticklabels(['A', 'B', 'C', 'D', 'E'])
267 plt.colorbar(im, ax=ax)
268 ax.set_title('Matplotlib ํํธ๋งต')
269
270 # ๊ฐ ํ์
271 for i in range(5):
272 for j in range(5):
273 ax.text(j, i, f'{data[i, j]:.2f}', ha='center', va='center')
274
275 # Seaborn ํํธ๋งต (์๋ ๊ฒฝ์ฐ)
276 ax = axes[1]
277 if HAS_SEABORN:
278 sns.heatmap(data, annot=True, fmt='.2f', cmap='coolwarm', ax=ax,
279 xticklabels=['A', 'B', 'C', 'D', 'E'],
280 yticklabels=['A', 'B', 'C', 'D', 'E'])
281 ax.set_title('Seaborn ํํธ๋งต')
282 else:
283 ax.text(0.5, 0.5, 'Seaborn not available', ha='center', va='center',
284 transform=ax.transAxes)
285 ax.set_title('Seaborn ํํธ๋งต (์์)')
286
287 plt.tight_layout()
288 plt.savefig('/opt/projects/01_Personal/03_Study/Data_Analysis/examples/07_heatmap.png', dpi=150)
289 plt.close()
290 print("์ ์ฅ: 07_heatmap.png")
291
292
293# =============================================================================
294# 8. ์๋ธํ๋กฏ
295# =============================================================================
296def subplots_example():
297 """์๋ธํ๋กฏ ๋ ์ด์์"""
298 print("\n[8] ์๋ธํ๋กฏ (Subplots)")
299 print("=" * 50)
300
301 fig, axes = plt.subplots(2, 3, figsize=(15, 10))
302 fig.suptitle('๋ค์ํ ๊ทธ๋ํ ๋ชจ์', fontsize=16)
303
304 np.random.seed(42)
305
306 # (0, 0) ์ ๊ทธ๋ํ
307 ax = axes[0, 0]
308 x = np.linspace(0, 10, 100)
309 ax.plot(x, np.sin(x), label='sin')
310 ax.plot(x, np.cos(x), label='cos')
311 ax.set_title('์ ๊ทธ๋ํ')
312 ax.legend()
313
314 # (0, 1) ์ฐ์ ๋
315 ax = axes[0, 1]
316 ax.scatter(np.random.randn(50), np.random.randn(50))
317 ax.set_title('์ฐ์ ๋')
318
319 # (0, 2) ๋ง๋ ๊ทธ๋ํ
320 ax = axes[0, 2]
321 ax.bar(['A', 'B', 'C', 'D'], [3, 7, 2, 5])
322 ax.set_title('๋ง๋ ๊ทธ๋ํ')
323
324 # (1, 0) ํ์คํ ๊ทธ๋จ
325 ax = axes[1, 0]
326 ax.hist(np.random.randn(1000), bins=30)
327 ax.set_title('ํ์คํ ๊ทธ๋จ')
328
329 # (1, 1) ํ์ด ์ฐจํธ
330 ax = axes[1, 1]
331 ax.pie([30, 20, 25, 25], labels=['A', 'B', 'C', 'D'], autopct='%1.0f%%')
332 ax.set_title('ํ์ด ์ฐจํธ')
333
334 # (1, 2) ์ด๋ฏธ์ง
335 ax = axes[1, 2]
336 ax.imshow(np.random.rand(10, 10), cmap='viridis')
337 ax.set_title('์ด๋ฏธ์ง')
338
339 plt.tight_layout()
340 plt.savefig('/opt/projects/01_Personal/03_Study/Data_Analysis/examples/08_subplots.png', dpi=150)
341 plt.close()
342 print("์ ์ฅ: 08_subplots.png")
343
344
345# =============================================================================
346# 9. 3D ํ๋กฏ
347# =============================================================================
348def plot_3d():
349 """3D ํ๋กฏ"""
350 print("\n[9] 3D ํ๋กฏ")
351 print("=" * 50)
352
353 from mpl_toolkits.mplot3d import Axes3D
354
355 fig = plt.figure(figsize=(14, 5))
356
357 # 3D ํ๋ฉด
358 ax1 = fig.add_subplot(1, 2, 1, projection='3d')
359 x = np.linspace(-5, 5, 50)
360 y = np.linspace(-5, 5, 50)
361 X, Y = np.meshgrid(x, y)
362 Z = np.sin(np.sqrt(X**2 + Y**2))
363
364 ax1.plot_surface(X, Y, Z, cmap='viridis', alpha=0.8)
365 ax1.set_xlabel('X')
366 ax1.set_ylabel('Y')
367 ax1.set_zlabel('Z')
368 ax1.set_title('3D ํ๋ฉด')
369
370 # 3D ์ฐ์ ๋
371 ax2 = fig.add_subplot(1, 2, 2, projection='3d')
372 n = 100
373 xs = np.random.randn(n)
374 ys = np.random.randn(n)
375 zs = np.random.randn(n)
376 colors = np.random.rand(n)
377
378 ax2.scatter(xs, ys, zs, c=colors, cmap='plasma')
379 ax2.set_xlabel('X')
380 ax2.set_ylabel('Y')
381 ax2.set_zlabel('Z')
382 ax2.set_title('3D ์ฐ์ ๋')
383
384 plt.tight_layout()
385 plt.savefig('/opt/projects/01_Personal/03_Study/Data_Analysis/examples/09_3d_plot.png', dpi=150)
386 plt.close()
387 print("์ ์ฅ: 09_3d_plot.png")
388
389
390# =============================================================================
391# ๋ฉ์ธ
392# =============================================================================
393def main():
394 print("=" * 60)
395 print("๋ฐ์ดํฐ ์๊ฐํ ์์ ")
396 print("=" * 60)
397
398 # ํ๊ธ ํฐํธ ์ค์ (์์คํ
์ ๋ฐ๋ผ ์กฐ์ ํ์)
399 plt.rcParams['font.family'] = 'DejaVu Sans'
400 plt.rcParams['axes.unicode_minus'] = False
401
402 line_plot()
403 scatter_plot()
404 bar_plot()
405 histogram()
406 pie_chart()
407 box_violin_plot()
408 heatmap()
409 subplots_example()
410 plot_3d()
411
412 print("\n" + "=" * 60)
413 print("์๊ฐํ ํต์ฌ ์ ๋ฆฌ")
414 print("=" * 60)
415 print("""
416 Matplotlib ๊ธฐ๋ณธ:
417 - plt.figure(): ์ ๊ทธ๋ฆผ ์์ฑ
418 - plt.subplot(): ์๋ธํ๋กฏ ์์ฑ
419 - plt.plot(), scatter(), bar(), hist(): ๊ทธ๋ํ ์ข
๋ฅ
420 - plt.xlabel(), ylabel(), title(): ๋ผ๋ฒจ๊ณผ ์ ๋ชฉ
421 - plt.legend(), grid(): ๋ฒ๋ก์ ๊ทธ๋ฆฌ๋
422 - plt.savefig(), show(): ์ ์ฅ๊ณผ ํ์
423
424 Seaborn ์ฅ์ :
425 - ๋ ์์ ๊ธฐ๋ณธ ์คํ์ผ
426 - ํต๊ณ์ ์๊ฐํ (regplot, kdeplot)
427 - DataFrame๊ณผ ์ฌ์ด ์ฐ๋
428
429 ๊ทธ๋ํ ์ ํ ๊ฐ์ด๋:
430 - ์ถ์ธ: ์ ๊ทธ๋ํ
431 - ๊ด๊ณ: ์ฐ์ ๋
432 - ๋น๊ต: ๋ง๋ ๊ทธ๋ํ
433 - ๋ถํฌ: ํ์คํ ๊ทธ๋จ, ๋ฐ์คํ๋กฏ
434 - ๋น์จ: ํ์ด ์ฐจํธ
435 - ์๊ด๊ด๊ณ: ํํธ๋งต
436
437 ํ:
438 - ์ ์ ํ ์์ ํ๋ ํธ ์ ํ
439 - ๋ผ๋ฒจ๊ณผ ์ ๋ชฉ ๋ช
ํํ๊ฒ
440 - ๋ฒ๋ก ์์น ์ต์ ํ
441 - DPI ์ค์ ์ผ๋ก ํด์๋ ์กฐ์
442 """)
443
444
445if __name__ == "__main__":
446 main()