03_visualization.py

Download
python 447 lines 13.2 KB
  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()