Zum Hauptinhalt springen

Datenvisualisierung ๐Ÿ“Š

Datenvisualisierung ist der Prozess der Darstellung komplexer Daten in leicht verstรคndlichen Grafiken. Matplotlib์€ ํŒŒ์ด์ฌ์—์„œ ๊ฐ€์žฅ ๋„๋ฆฌ ์‚ฌ์šฉ๋˜๋Š” ์‹œ๊ฐํ™” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค.

Was ist Matplotlib?โ€‹

Matplotlib ist eine Bibliothek zum Zeichnen von 2D-Diagrammen und Grafiken in Python. MATLAB๊ณผ ์œ ์‚ฌํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

Hauptmerkmaleโ€‹

  • ๋‹ค์–‘ํ•œ ์ฐจํŠธ: ์„ , ๋ง‰๋Œ€, ์‚ฐ์ ๋„, ํžˆ์Šคํ† ๊ทธ๋žจ ๋“ฑ
  • ์ปค์Šคํ„ฐ๋งˆ์ด์ง•: ์„ธ๋ฐ€ํ•œ ์Šคํƒ€์ผ ์กฐ์ • ๊ฐ€๋Šฅ
  • Pandas ํ†ตํ•ฉ: DataFrame๊ณผ ์‰ฝ๊ฒŒ ์—ฐ๋™
  • ์ถœํŒ ํ’ˆ์งˆ: ๋…ผ๋ฌธ/๋ณด๊ณ ์„œ์šฉ ๊ทธ๋ž˜ํ”„ ์ƒ์„ฑ

Installationโ€‹

pip install matplotlib
import matplotlib.pyplot as plt
import numpy as np

# ํ•œ๊ธ€ ํฐํŠธ ์„ค์ • (macOS)
plt.rcParams['font.family'] = 'AppleGothic'
# Windows: 'Malgun Gothic'
# Linux: 'NanumGothic'

# ๋งˆ์ด๋„ˆ์Šค ๊ธฐํ˜ธ ๊นจ์ง ๋ฐฉ์ง€
plt.rcParams['axes.unicode_minus'] = False

print(f"Matplotlib ๋ฒ„์ „: {plt.__version__}")

Grundlegende Diagrammeโ€‹

Liniendiagramm (Line Plot)โ€‹

import matplotlib.pyplot as plt
import numpy as np

# ๋ฐ์ดํ„ฐ ์ค€๋น„
x = np.linspace(0, 10, 100)
y = np.sin(x)

# ๊ทธ๋ž˜ํ”„ ๊ทธ๋ฆฌ๊ธฐ
plt.plot(x, y)
plt.title('์‚ฌ์ธ ํ•จ์ˆ˜')
plt.xlabel('x')
plt.ylabel('sin(x)')
plt.grid(True)
plt.show()

# ์—ฌ๋Ÿฌ ์„  ๊ทธ๋ฆฌ๊ธฐ
plt.figure(figsize=(10, 6))
plt.plot(x, np.sin(x), label='sin(x)')
plt.plot(x, np.cos(x), label='cos(x)')
plt.title('์‚ผ๊ฐํ•จ์ˆ˜')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.grid(True)
plt.show()

Stiloptionenโ€‹

x = np.linspace(0, 10, 20)
y = x ** 2

# ์„  ์Šคํƒ€์ผ
plt.plot(x, y, 'r--', linewidth=2) # ๋นจ๊ฐ„ ์ ์„ 
plt.plot(x, y, 'bo-', markersize=8) # ํŒŒ๋ž€ ๋™๊ทธ๋ผ๋ฏธ์™€ ์„ 
plt.plot(x, y, 'g^:', alpha=0.5) # ์ดˆ๋ก ์‚ผ๊ฐํ˜•๊ณผ ์ ์„  (๋ฐ˜ํˆฌ๋ช…)

# ๋” ๋ช…์‹œ์ ์œผ๋กœ
plt.plot(x, y,
color='blue', # ์ƒ‰์ƒ
linestyle='--', # ์„  ์Šคํƒ€์ผ
linewidth=2, # ์„  ๋‘๊ป˜
marker='o', # ๋งˆ์ปค ๋ชจ์–‘
markersize=8, # ๋งˆ์ปค ํฌ๊ธฐ
markerfacecolor='red', # ๋งˆ์ปค ๋‚ด๋ถ€ ์ƒ‰
markeredgecolor='black',# ๋งˆ์ปค ํ…Œ๋‘๋ฆฌ ์ƒ‰
alpha=0.7, # ํˆฌ๋ช…๋„
label='๋ฐ์ดํ„ฐ' # ๋ฒ”๋ก€
)

Balkendiagramm (Bar Chart)โ€‹

Vertikales Balkendiagrammโ€‹

import matplotlib.pyplot as plt

# ๋ฐ์ดํ„ฐ
categories = ['A', 'B', 'C', 'D', 'E']
values = [23, 45, 56, 78, 32]

# ๋ง‰๋Œ€ ๊ทธ๋ž˜ํ”„
plt.figure(figsize=(10, 6))
plt.bar(categories, values, color='skyblue', edgecolor='black')
plt.title('์ œํ’ˆ๋ณ„ ํŒ๋งค๋Ÿ‰')
plt.xlabel('์ œํ’ˆ')
plt.ylabel('ํŒ๋งค๋Ÿ‰')
plt.grid(axis='y', alpha=0.3)
plt.show()

# ์ƒ‰์ƒ ๋‹ค๋ฅด๊ฒŒ
colors = ['red', 'green', 'blue', 'orange', 'purple']
plt.bar(categories, values, color=colors)
plt.show()

Horizontales Balkendiagrammโ€‹

# ์ˆ˜ํ‰ ๋ง‰๋Œ€
plt.figure(figsize=(10, 6))
plt.barh(categories, values, color='lightcoral')
plt.title('์ œํ’ˆ๋ณ„ ํŒ๋งค๋Ÿ‰')
plt.xlabel('ํŒ๋งค๋Ÿ‰')
plt.ylabel('์ œํ’ˆ')
plt.show()

Gruppiertes Balkendiagrammโ€‹

import numpy as np

# ๋ฐ์ดํ„ฐ
categories = ['A', 'B', 'C', 'D']
values1 = [23, 45, 56, 78]
values2 = [34, 55, 48, 70]

x = np.arange(len(categories))
width = 0.35

# ๊ทธ๋ฃน ๋ง‰๋Œ€
fig, ax = plt.subplots(figsize=(10, 6))
ax.bar(x - width/2, values1, width, label='2023๋…„', color='skyblue')
ax.bar(x + width/2, values2, width, label='2024๋…„', color='lightcoral')

ax.set_xlabel('์ œํ’ˆ')
ax.set_ylabel('ํŒ๋งค๋Ÿ‰')
ax.set_title('์—ฐ๋„๋ณ„ ์ œํ’ˆ ํŒ๋งค ๋น„๊ต')
ax.set_xticks(x)
ax.set_xticklabels(categories)
ax.legend()
ax.grid(axis='y', alpha=0.3)

plt.tight_layout()
plt.show()

Gestapeltes Balkendiagrammโ€‹

# ๋ˆ„์  ๋ง‰๋Œ€
categories = ['Q1', 'Q2', 'Q3', 'Q4']
product_a = [20, 35, 30, 35]
product_b = [25, 32, 34, 20]
product_c = [15, 20, 25, 30]

plt.figure(figsize=(10, 6))
plt.bar(categories, product_a, label='์ œํ’ˆ A')
plt.bar(categories, product_b, bottom=product_a, label='์ œํ’ˆ B')
plt.bar(categories, product_c, bottom=np.array(product_a)+np.array(product_b), label='์ œํ’ˆ C')

plt.xlabel('๋ถ„๊ธฐ')
plt.ylabel('ํŒ๋งค๋Ÿ‰')
plt.title('๋ถ„๊ธฐ๋ณ„ ์ œํ’ˆ ํŒ๋งค')
plt.legend()
plt.show()

Streudiagramm (Scatter Plot)โ€‹

import matplotlib.pyplot as plt
import numpy as np

# ๋ฐ์ดํ„ฐ
np.random.seed(42)
x = np.random.rand(100) * 100
y = np.random.rand(100) * 100
sizes = np.random.rand(100) * 100
colors = np.random.rand(100)

# ๊ธฐ๋ณธ ์‚ฐ์ ๋„
plt.figure(figsize=(10, 6))
plt.scatter(x, y)
plt.title('๊ธฐ๋ณธ ์‚ฐ์ ๋„')
plt.xlabel('X')
plt.ylabel('Y')
plt.grid(True, alpha=0.3)
plt.show()

# ํฌ๊ธฐ์™€ ์ƒ‰์ƒ ์ ์šฉ
plt.figure(figsize=(10, 6))
scatter = plt.scatter(x, y, s=sizes, c=colors, cmap='viridis', alpha=0.6)
plt.colorbar(scatter, label='๊ฐ’')
plt.title('ํฌ๊ธฐ์™€ ์ƒ‰์ƒ์„ ๊ฐ€์ง„ ์‚ฐ์ ๋„')
plt.xlabel('X')
plt.ylabel('Y')
plt.grid(True, alpha=0.3)
plt.show()

Korrelationsvisualisierungโ€‹

# ํ‚ค์™€ ๋ชธ๋ฌด๊ฒŒ ์ƒ๊ด€๊ด€๊ณ„
height = np.random.normal(170, 10, 100)
weight = height * 0.8 + np.random.normal(0, 5, 100)

plt.figure(figsize=(10, 6))
plt.scatter(height, weight, alpha=0.5)
plt.title('ํ‚ค์™€ ๋ชธ๋ฌด๊ฒŒ ์ƒ๊ด€๊ด€๊ณ„')
plt.xlabel('ํ‚ค (cm)')
plt.ylabel('๋ชธ๋ฌด๊ฒŒ (kg)')

# ์ถ”์„ธ์„  ์ถ”๊ฐ€
z = np.polyfit(height, weight, 1)
p = np.poly1d(z)
plt.plot(height, p(height), "r--", alpha=0.8, label='์ถ”์„ธ์„ ')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()

Histogrammโ€‹

import matplotlib.pyplot as plt
import numpy as np

# ์ •๊ทœ๋ถ„ํฌ ๋ฐ์ดํ„ฐ
data = np.random.normal(100, 15, 1000)

# ํžˆ์Šคํ† ๊ทธ๋žจ
plt.figure(figsize=(10, 6))
plt.hist(data, bins=30, color='skyblue', edgecolor='black', alpha=0.7)
plt.title('์‹œํ—˜ ์ ์ˆ˜ ๋ถ„ํฌ')
plt.xlabel('์ ์ˆ˜')
plt.ylabel('๋นˆ๋„')
plt.axvline(data.mean(), color='red', linestyle='--', linewidth=2, label=f'ํ‰๊ท : {data.mean():.1f}')
plt.legend()
plt.grid(axis='y', alpha=0.3)
plt.show()

# ์—ฌ๋Ÿฌ ํžˆ์Šคํ† ๊ทธ๋žจ ๋น„๊ต
data1 = np.random.normal(100, 15, 1000)
data2 = np.random.normal(110, 10, 1000)

plt.figure(figsize=(10, 6))
plt.hist(data1, bins=30, alpha=0.5, label='A๋ฐ˜', color='blue')
plt.hist(data2, bins=30, alpha=0.5, label='B๋ฐ˜', color='red')
plt.title('๋ฐ˜๋ณ„ ์‹œํ—˜ ์ ์ˆ˜ ๋ถ„ํฌ')
plt.xlabel('์ ์ˆ˜')
plt.ylabel('๋นˆ๋„')
plt.legend()
plt.grid(axis='y', alpha=0.3)
plt.show()

Kreisdiagramm (Pie Chart)โ€‹

# ๋ฐ์ดํ„ฐ
labels = ['Python', 'Java', 'JavaScript', 'C++', 'Others']
sizes = [35, 25, 20, 10, 10]
colors = ['#ff9999', '#66b3ff', '#99ff99', '#ffcc99', '#ff99cc']
explode = (0.1, 0, 0, 0, 0) # Python ๊ฐ•์กฐ

# ํŒŒ์ด ์ฐจํŠธ
plt.figure(figsize=(10, 8))
plt.pie(sizes, explode=explode, labels=labels, colors=colors,
autopct='%1.1f%%', shadow=True, startangle=90)
plt.title('ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด ์‚ฌ์šฉ ๋น„์œจ')
plt.axis('equal')
plt.show()

# ๋„๋„› ์ฐจํŠธ
plt.figure(figsize=(10, 8))
plt.pie(sizes, labels=labels, colors=colors,
autopct='%1.1f%%', startangle=90,
wedgeprops={'width': 0.5}) # ๋„๋„› ๋ชจ์–‘
plt.title('ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด ์‚ฌ์šฉ ๋น„์œจ (๋„๋„›)')
plt.axis('equal')
plt.show()

Boxplotโ€‹

import numpy as np

# ๋ฐ์ดํ„ฐ
np.random.seed(42)
data1 = np.random.normal(100, 10, 200)
data2 = np.random.normal(90, 15, 200)
data3 = np.random.normal(110, 5, 200)

# ๋ฐ•์Šค ํ”Œ๋กฏ
plt.figure(figsize=(10, 6))
plt.boxplot([data1, data2, data3],
labels=['A๋ฐ˜', 'B๋ฐ˜', 'C๋ฐ˜'],
patch_artist=True,
boxprops=dict(facecolor='lightblue', alpha=0.5),
medianprops=dict(color='red', linewidth=2))
plt.title('๋ฐ˜๋ณ„ ์‹œํ—˜ ์ ์ˆ˜ ๋ถ„ํฌ')
plt.ylabel('์ ์ˆ˜')
plt.grid(axis='y', alpha=0.3)
plt.show()

Unterdiagramme (Subplots)โ€‹

Grundlegende Unterdiagrammeโ€‹

# 2x2 ์„œ๋ธŒํ”Œ๋กฏ
fig, axes = plt.subplots(2, 2, figsize=(12, 10))

# ์™ผ์ชฝ ์œ„: ์„  ๊ทธ๋ž˜ํ”„
x = np.linspace(0, 10, 100)
axes[0, 0].plot(x, np.sin(x))
axes[0, 0].set_title('์„  ๊ทธ๋ž˜ํ”„')
axes[0, 0].grid(True)

# ์˜ค๋ฅธ์ชฝ ์œ„: ๋ง‰๋Œ€ ๊ทธ๋ž˜ํ”„
categories = ['A', 'B', 'C', 'D']
values = [23, 45, 56, 78]
axes[0, 1].bar(categories, values, color='skyblue')
axes[0, 1].set_title('๋ง‰๋Œ€ ๊ทธ๋ž˜ํ”„')

# ์™ผ์ชฝ ์•„๋ž˜: ์‚ฐ์ ๋„
x = np.random.rand(50)
y = np.random.rand(50)
axes[1, 0].scatter(x, y, alpha=0.5)
axes[1, 0].set_title('์‚ฐ์ ๋„')

# ์˜ค๋ฅธ์ชฝ ์•„๋ž˜: ํžˆ์Šคํ† ๊ทธ๋žจ
data = np.random.normal(0, 1, 1000)
axes[1, 1].hist(data, bins=30, color='lightcoral')
axes[1, 1].set_title('ํžˆ์Šคํ† ๊ทธ๋žจ')

plt.tight_layout()
plt.show()

Verschiedene Layoutsโ€‹

# ๋ถˆ๊ท ๋“ฑํ•œ ์„œ๋ธŒํ”Œ๋กฏ
fig = plt.figure(figsize=(12, 8))

# 2x2 ๊ทธ๋ฆฌ๋“œ์—์„œ ๋‹ค์–‘ํ•œ ํฌ๊ธฐ
ax1 = plt.subplot(2, 2, 1)
ax1.plot([1, 2, 3], [1, 4, 9])
ax1.set_title('Plot 1')

ax2 = plt.subplot(2, 2, 2)
ax2.bar(['A', 'B', 'C'], [10, 20, 15])
ax2.set_title('Plot 2')

ax3 = plt.subplot(2, 1, 2) # ์•„๋ž˜์ชฝ ์ „์ฒด
ax3.plot(np.random.rand(100))
ax3.set_title('Plot 3 (Wide)')

plt.tight_layout()
plt.show()

Stylingโ€‹

Grundstileโ€‹

# ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์Šคํƒ€์ผ ํ™•์ธ
print(plt.style.available)

# ์Šคํƒ€์ผ ์ ์šฉ
plt.style.use('seaborn-v0_8-darkgrid')
# ๋˜๋Š” 'ggplot', 'fivethirtyeight', 'bmh' ๋“ฑ

x = np.linspace(0, 10, 100)
y = np.sin(x)

plt.figure(figsize=(10, 6))
plt.plot(x, y, linewidth=2)
plt.title('์Šคํƒ€์ผ์ด ์ ์šฉ๋œ ๊ทธ๋ž˜ํ”„')
plt.show()

# ์Šคํƒ€์ผ ์ดˆ๊ธฐํ™”
plt.style.use('default')

Farbauswahlโ€‹

# ๋‹ค์–‘ํ•œ ์ƒ‰์ƒ ์ง€์ • ๋ฐฉ๋ฒ•
plt.plot(x, y, color='red') # ์ด๋ฆ„
plt.plot(x, y, color='r') # ์•ฝ์ž
plt.plot(x, y, color='#FF5733') # Hex ์ฝ”๋“œ
plt.plot(x, y, color=(0.1, 0.2, 0.5)) # RGB ํŠœํ”Œ
plt.plot(x, y, color=(0.1, 0.2, 0.5, 0.7)) # RGBA (ํˆฌ๋ช…๋„ ํฌํ•จ)

# ์ปฌ๋Ÿฌ๋งต ์‚ฌ์šฉ
colors = plt.cm.viridis(np.linspace(0, 1, 10))
for i in range(10):
plt.plot(x, y + i, color=colors[i])

Schriftarten und Textโ€‹

plt.figure(figsize=(10, 6))

# ํƒ€์ดํ‹€
plt.title('์ œ๋ชฉ', fontsize=20, fontweight='bold', color='navy')

# ์ถ• ๋ ˆ์ด๋ธ”
plt.xlabel('X์ถ•', fontsize=14, style='italic')
plt.ylabel('Y์ถ•', fontsize=14, style='italic')

# ํ…์ŠคํŠธ ์ถ”๊ฐ€
plt.text(5, 0.5, '์ฃผ์„ ํ…์ŠคํŠธ', fontsize=12,
bbox=dict(boxstyle='round', facecolor='wheat', alpha=0.5))

# ํ™”์‚ดํ‘œ์™€ ์ฃผ์„
plt.annotate('์ค‘์š” ์ง€์ ', xy=(7, 0.7), xytext=(8, 0.9),
arrowprops=dict(arrowstyle='->', color='red', lw=2),
fontsize=12)

plt.plot(x, y)
plt.show()

Praktische Beispieleโ€‹

์˜ˆ์ œ 1: ์›”๋ณ„ ๋งค์ถœ ๋Œ€์‹œ๋ณด๋“œโ€‹

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

# ๋ฐ์ดํ„ฐ ์ƒ์„ฑ
months = ['1์›”', '2์›”', '3์›”', '4์›”', '5์›”', '6์›”']
sales_2023 = [120, 135, 148, 162, 155, 178]
sales_2024 = [145, 158, 170, 185, 192, 205]
target = [150] * 6

# ๋Œ€์‹œ๋ณด๋“œ ์ƒ์„ฑ
fig = plt.figure(figsize=(16, 10))
fig.suptitle('2024๋…„ ์ƒ๋ฐ˜๊ธฐ ๋งค์ถœ ๋ถ„์„ ๋Œ€์‹œ๋ณด๋“œ', fontsize=20, fontweight='bold')

# 1. ์›”๋ณ„ ๋งค์ถœ ๋น„๊ต (์„  ๊ทธ๋ž˜ํ”„)
ax1 = plt.subplot(2, 3, 1)
ax1.plot(months, sales_2023, marker='o', linewidth=2, label='2023๋…„', color='skyblue')
ax1.plot(months, sales_2024, marker='s', linewidth=2, label='2024๋…„', color='coral')
ax1.plot(months, target, linestyle='--', linewidth=2, label='๋ชฉํ‘œ', color='green')
ax1.set_title('์›”๋ณ„ ๋งค์ถœ ์ถ”์ด', fontsize=14, fontweight='bold')
ax1.set_ylabel('๋งค์ถœ (๋ฐฑ๋งŒ์›)')
ax1.legend()
ax1.grid(True, alpha=0.3)

# 2. ์—ฐ๋„๋ณ„ ์ด ๋งค์ถœ (๋ง‰๋Œ€ ๊ทธ๋ž˜ํ”„)
ax2 = plt.subplot(2, 3, 2)
total_sales = [sum(sales_2023), sum(sales_2024)]
colors = ['skyblue', 'coral']
bars = ax2.bar(['2023๋…„', '2024๋…„'], total_sales, color=colors, edgecolor='black', linewidth=2)
ax2.set_title('์—ฐ๋„๋ณ„ ์ด ๋งค์ถœ', fontsize=14, fontweight='bold')
ax2.set_ylabel('๋งค์ถœ (๋ฐฑ๋งŒ์›)')
for bar in bars:
height = bar.get_height()
ax2.text(bar.get_x() + bar.get_width()/2., height,
f'{int(height)}M', ha='center', va='bottom', fontsize=12, fontweight='bold')
ax2.grid(axis='y', alpha=0.3)

# 3. ์ฆ๊ฐ์œจ (๋ง‰๋Œ€ ๊ทธ๋ž˜ํ”„)
ax3 = plt.subplot(2, 3, 3)
growth = [(s24 - s23) / s23 * 100 for s23, s24 in zip(sales_2023, sales_2024)]
colors_growth = ['green' if g > 0 else 'red' for g in growth]
ax3.bar(months, growth, color=colors_growth, alpha=0.7, edgecolor='black')
ax3.axhline(y=0, color='black', linestyle='-', linewidth=0.5)
ax3.set_title('์ „๋…„ ๋Œ€๋น„ ์ฆ๊ฐ์œจ', fontsize=14, fontweight='bold')
ax3.set_ylabel('์ฆ๊ฐ์œจ (%)')
ax3.grid(axis='y', alpha=0.3)

# 4. ๋ชฉํ‘œ ๋‹ฌ์„ฑ๋ฅ  (์ˆ˜ํ‰ ๋ง‰๋Œ€)
ax4 = plt.subplot(2, 3, 4)
achievement = [s / t * 100 for s, t in zip(sales_2024, target)]
colors_achievement = ['green' if a >= 100 else 'orange' for a in achievement]
ax4.barh(months, achievement, color=colors_achievement, alpha=0.7, edgecolor='black')
ax4.axvline(x=100, color='red', linestyle='--', linewidth=2, label='๋ชฉํ‘œ์„ ')
ax4.set_title('์›”๋ณ„ ๋ชฉํ‘œ ๋‹ฌ์„ฑ๋ฅ ', fontsize=14, fontweight='bold')
ax4.set_xlabel('๋‹ฌ์„ฑ๋ฅ  (%)')
ax4.legend()
ax4.grid(axis='x', alpha=0.3)

# 5. ๋งค์ถœ ๋ถ„ํฌ (ํžˆ์Šคํ† ๊ทธ๋žจ)
ax5 = plt.subplot(2, 3, 5)
all_sales = sales_2023 + sales_2024
ax5.hist(all_sales, bins=10, color='mediumpurple', edgecolor='black', alpha=0.7)
ax5.axvline(np.mean(all_sales), color='red', linestyle='--',
linewidth=2, label=f'ํ‰๊ท : {np.mean(all_sales):.1f}M')
ax5.set_title('๋งค์ถœ ๋ถ„ํฌ', fontsize=14, fontweight='bold')
ax5.set_xlabel('๋งค์ถœ (๋ฐฑ๋งŒ์›)')
ax5.set_ylabel('๋นˆ๋„')
ax5.legend()
ax5.grid(axis='y', alpha=0.3)

# 6. ํ†ต๊ณ„ ์š”์•ฝ (ํ…์ŠคํŠธ)
ax6 = plt.subplot(2, 3, 6)
ax6.axis('off')

stats_text = f"""
๐Ÿ“Š ์ฃผ์š” ์ง€ํ‘œ ์š”์•ฝ

2024๋…„ ์ด ๋งค์ถœ: {sum(sales_2024):,}๋ฐฑ๋งŒ์›
2023๋…„ ์ด ๋งค์ถœ: {sum(sales_2023):,}๋ฐฑ๋งŒ์›
์ฆ๊ฐ: +{sum(sales_2024) - sum(sales_2023):,}๋ฐฑ๋งŒ์›
์ฆ๊ฐ์œจ: +{(sum(sales_2024) - sum(sales_2023)) / sum(sales_2023) * 100:.1f}%

์›”ํ‰๊ท  ๋งค์ถœ: {np.mean(sales_2024):.1f}๋ฐฑ๋งŒ์›
์ตœ๊ณ  ๋งค์ถœ: {max(sales_2024)}๋ฐฑ๋งŒ์› ({months[sales_2024.index(max(sales_2024))]})
์ตœ์ € ๋งค์ถœ: {min(sales_2024)}๋ฐฑ๋งŒ์› ({months[sales_2024.index(min(sales_2024))]})

๋ชฉํ‘œ ๋‹ฌ์„ฑ ์›”์ˆ˜: {sum(1 for s in sales_2024 if s >= 150)}/6๊ฐœ์›”
ํ‰๊ท  ๋‹ฌ์„ฑ๋ฅ : {np.mean(achievement):.1f}%
"""

ax6.text(0.1, 0.5, stats_text, fontsize=12, verticalalignment='center',
fontfamily='monospace',
bbox=dict(boxstyle='round', facecolor='wheat', alpha=0.3))

plt.tight_layout()
plt.show()

์˜ˆ์ œ 2: ์ œํ’ˆ ํŒ๋งค ๋ถ„์„โ€‹

import matplotlib.pyplot as plt
import pandas as pd

# ์ œํ’ˆ ํŒ๋งค ๋ฐ์ดํ„ฐ
data = {
'์ œํ’ˆ': ['๋…ธํŠธ๋ถ', '๋งˆ์šฐ์Šค', 'ํ‚ค๋ณด๋“œ', '๋ชจ๋‹ˆํ„ฐ', 'ํ—ค๋“œ์…‹'],
'ํŒ๋งค๋Ÿ‰': [450, 1200, 800, 350, 600],
'๋งค์ถœ': [450000, 36000, 80000, 350000, 180000],
'์žฌ๊ณ ': [120, 450, 280, 90, 200]
}
df = pd.DataFrame(data)

# ๋Œ€์‹œ๋ณด๋“œ
fig, axes = plt.subplots(2, 2, figsize=(14, 10))
fig.suptitle('์ œํ’ˆ ํŒ๋งค ํ˜„ํ™ฉ', fontsize=18, fontweight='bold')

# 1. ์ œํ’ˆ๋ณ„ ํŒ๋งค๋Ÿ‰
axes[0, 0].bar(df['์ œํ’ˆ'], df['ํŒ๋งค๋Ÿ‰'], color='steelblue', edgecolor='black')
axes[0, 0].set_title('์ œํ’ˆ๋ณ„ ํŒ๋งค๋Ÿ‰', fontsize=14, fontweight='bold')
axes[0, 0].set_ylabel('ํŒ๋งค๋Ÿ‰')
axes[0, 0].tick_params(axis='x', rotation=45)
axes[0, 0].grid(axis='y', alpha=0.3)

# 2. ์ œํ’ˆ๋ณ„ ๋งค์ถœ
colors = plt.cm.Set3(range(len(df)))
axes[0, 1].barh(df['์ œํ’ˆ'], df['๋งค์ถœ'], color=colors, edgecolor='black')
axes[0, 1].set_title('์ œํ’ˆ๋ณ„ ๋งค์ถœ', fontsize=14, fontweight='bold')
axes[0, 1].set_xlabel('๋งค์ถœ (์›)')
for i, v in enumerate(df['๋งค์ถœ']):
axes[0, 1].text(v, i, f' {v:,}', va='center', fontsize=10)
axes[0, 1].grid(axis='x', alpha=0.3)

# 3. ๋งค์ถœ ๋น„์œจ (ํŒŒ์ด ์ฐจํŠธ)
explode = [0.05] * len(df)
explode[df['๋งค์ถœ'].idxmax()] = 0.15 # ์ตœ๋Œ€ ๋งค์ถœ ์ œํ’ˆ ๊ฐ•์กฐ
axes[1, 0].pie(df['๋งค์ถœ'], labels=df['์ œํ’ˆ'], autopct='%1.1f%%',
explode=explode, shadow=True, startangle=90)
axes[1, 0].set_title('๋งค์ถœ ๋น„์œจ', fontsize=14, fontweight='bold')

# 4. ํŒ๋งค๋Ÿ‰ vs ์žฌ๊ณ  (์‚ฐ์ ๋„)
axes[1, 1].scatter(df['ํŒ๋งค๋Ÿ‰'], df['์žฌ๊ณ '], s=df['๋งค์ถœ']/500,
c=range(len(df)), cmap='viridis', alpha=0.6, edgecolors='black', linewidth=2)
for i, txt in enumerate(df['์ œํ’ˆ']):
axes[1, 1].annotate(txt, (df['ํŒ๋งค๋Ÿ‰'][i], df['์žฌ๊ณ '][i]),
xytext=(5, 5), textcoords='offset points', fontsize=10)
axes[1, 1].set_title('ํŒ๋งค๋Ÿ‰ vs ์žฌ๊ณ  (ํฌ๊ธฐ=๋งค์ถœ)', fontsize=14, fontweight='bold')
axes[1, 1].set_xlabel('ํŒ๋งค๋Ÿ‰')
axes[1, 1].set_ylabel('์žฌ๊ณ ')
axes[1, 1].grid(True, alpha=0.3)

plt.tight_layout()
plt.show()

์˜ˆ์ œ 3: ์‹œ๊ณ„์—ด ๋ฐ์ดํ„ฐ ์‹œ๊ฐํ™”โ€‹

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

# ์ผ์ผ ๋ฐฉ๋ฌธ์ž ๋ฐ์ดํ„ฐ ์ƒ์„ฑ
dates = pd.date_range('2024-01-01', '2024-06-30', freq='D')
np.random.seed(42)
base_visitors = 1000
trend = np.linspace(0, 500, len(dates))
seasonality = 200 * np.sin(2 * np.pi * np.arange(len(dates)) / 7) # ์ฃผ๊ฐ„ ํŒจํ„ด
noise = np.random.normal(0, 50, len(dates))
visitors = base_visitors + trend + seasonality + noise

df = pd.DataFrame({
'date': dates,
'visitors': visitors.astype(int)
})
df['month'] = df['date'].dt.month
df['weekday'] = df['date'].dt.day_name()

# ์‹œ๊ฐํ™”
fig = plt.figure(figsize=(16, 12))
fig.suptitle('์›น์‚ฌ์ดํŠธ ๋ฐฉ๋ฌธ์ž ๋ถ„์„ (2024๋…„ ์ƒ๋ฐ˜๊ธฐ)', fontsize=20, fontweight='bold')

# 1. ์ „์ฒด ์ถ”์ด
ax1 = plt.subplot(3, 2, 1)
ax1.plot(df['date'], df['visitors'], linewidth=1, alpha=0.7, label='์ผ์ผ ๋ฐฉ๋ฌธ์ž')
# ์ด๋™ ํ‰๊ท 
ma7 = df['visitors'].rolling(window=7).mean()
ma30 = df['visitors'].rolling(window=30).mean()
ax1.plot(df['date'], ma7, linewidth=2, label='7์ผ ์ด๋™ํ‰๊ท ', color='orange')
ax1.plot(df['date'], ma30, linewidth=2, label='30์ผ ์ด๋™ํ‰๊ท ', color='red')
ax1.set_title('์ผ์ผ ๋ฐฉ๋ฌธ์ž ์ถ”์ด', fontsize=14, fontweight='bold')
ax1.set_ylabel('๋ฐฉ๋ฌธ์ž ์ˆ˜')
ax1.legend()
ax1.grid(True, alpha=0.3)

# 2. ์›”๋ณ„ ํ‰๊ท 
ax2 = plt.subplot(3, 2, 2)
monthly_avg = df.groupby('month')['visitors'].mean()
month_names = ['1์›”', '2์›”', '3์›”', '4์›”', '5์›”', '6์›”']
bars = ax2.bar(month_names, monthly_avg, color='skyblue', edgecolor='black')
ax2.set_title('์›”๋ณ„ ํ‰๊ท  ๋ฐฉ๋ฌธ์ž', fontsize=14, fontweight='bold')
ax2.set_ylabel('ํ‰๊ท  ๋ฐฉ๋ฌธ์ž ์ˆ˜')
for bar in bars:
height = bar.get_height()
ax2.text(bar.get_x() + bar.get_width()/2., height,
f'{int(height)}', ha='center', va='bottom', fontsize=10)
ax2.grid(axis='y', alpha=0.3)

# 3. ์š”์ผ๋ณ„ ํŒจํ„ด
ax3 = plt.subplot(3, 2, 3)
weekday_order = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
weekday_labels = ['์›”', 'ํ™”', '์ˆ˜', '๋ชฉ', '๊ธˆ', 'ํ† ', '์ผ']
weekday_avg = df.groupby('weekday')['visitors'].mean().reindex(weekday_order)
colors = ['#FF6B6B' if day in ['Saturday', 'Sunday'] else '#4ECDC4' for day in weekday_order]
ax3.bar(weekday_labels, weekday_avg, color=colors, edgecolor='black')
ax3.set_title('์š”์ผ๋ณ„ ํ‰๊ท  ๋ฐฉ๋ฌธ์ž', fontsize=14, fontweight='bold')
ax3.set_ylabel('ํ‰๊ท  ๋ฐฉ๋ฌธ์ž ์ˆ˜')
ax3.grid(axis='y', alpha=0.3)

# 4. ๋ฐฉ๋ฌธ์ž ๋ถ„ํฌ
ax4 = plt.subplot(3, 2, 4)
ax4.hist(df['visitors'], bins=30, color='mediumpurple', edgecolor='black', alpha=0.7)
ax4.axvline(df['visitors'].mean(), color='red', linestyle='--',
linewidth=2, label=f"ํ‰๊ท : {df['visitors'].mean():.0f}")
ax4.axvline(df['visitors'].median(), color='green', linestyle='--',
linewidth=2, label=f"์ค‘์•™๊ฐ’: {df['visitors'].median():.0f}")
ax4.set_title('๋ฐฉ๋ฌธ์ž ๋ถ„ํฌ', fontsize=14, fontweight='bold')
ax4.set_xlabel('๋ฐฉ๋ฌธ์ž ์ˆ˜')
ax4.set_ylabel('์ผ์ˆ˜')
ax4.legend()
ax4.grid(axis='y', alpha=0.3)

# 5. ์›”๋ณ„ ๋ฐ•์Šคํ”Œ๋กฏ
ax5 = plt.subplot(3, 2, 5)
monthly_data = [df[df['month'] == m]['visitors'].values for m in range(1, 7)]
bp = ax5.boxplot(monthly_data, labels=month_names, patch_artist=True)
for patch in bp['boxes']:
patch.set_facecolor('lightblue')
ax5.set_title('์›”๋ณ„ ๋ฐฉ๋ฌธ์ž ๋ถ„ํฌ', fontsize=14, fontweight='bold')
ax5.set_ylabel('๋ฐฉ๋ฌธ์ž ์ˆ˜')
ax5.grid(axis='y', alpha=0.3)

# 6. ํ†ต๊ณ„ ์š”์•ฝ
ax6 = plt.subplot(3, 2, 6)
ax6.axis('off')

stats = f"""
๐Ÿ“ˆ ๋ฐฉ๋ฌธ์ž ํ†ต๊ณ„ ์š”์•ฝ

์ด ๋ถ„์„ ๊ธฐ๊ฐ„: {len(df)}์ผ
์ด ๋ฐฉ๋ฌธ์ž: {df['visitors'].sum():,}๋ช…

ํ‰๊ท  ๋ฐฉ๋ฌธ์ž: {df['visitors'].mean():.0f}๋ช…
์ค‘์•™๊ฐ’: {df['visitors'].median():.0f}๋ช…
ํ‘œ์ค€ํŽธ์ฐจ: {df['visitors'].std():.0f}

์ตœ๋Œ€ ๋ฐฉ๋ฌธ: {df['visitors'].max()}๋ช…
({df[df['visitors'] == df['visitors'].max()]['date'].dt.date.values[0]})
์ตœ์†Œ ๋ฐฉ๋ฌธ: {df['visitors'].min()}๋ช…
({df[df['visitors'] == df['visitors'].min()]['date'].dt.date.values[0]})

๊ฐ€์žฅ ๋งŽ์€ ์š”์ผ: {weekday_labels[weekday_avg.argmax()]}์š”์ผ
๊ฐ€์žฅ ์ ์€ ์š”์ผ: {weekday_labels[weekday_avg.argmin()]}์š”์ผ

์ฆ๊ฐ€ ์ถ”์„ธ: +{(df['visitors'].iloc[-30:].mean() - df['visitors'].iloc[:30].mean()):.0f}๋ช…
"""

ax6.text(0.1, 0.5, stats, fontsize=11, verticalalignment='center',
fontfamily='monospace',
bbox=dict(boxstyle='round', facecolor='lightyellow', alpha=0.5))

plt.tight_layout()
plt.show()

Speichern und Exportierenโ€‹

import matplotlib.pyplot as plt

# ๊ทธ๋ž˜ํ”„ ์ƒ์„ฑ
fig, ax = plt.subplots(figsize=(10, 6))
ax.plot([1, 2, 3, 4], [1, 4, 2, 3])
ax.set_title('์ƒ˜ํ”Œ ๊ทธ๋ž˜ํ”„')

# ์ด๋ฏธ์ง€๋กœ ์ €์žฅ
plt.savefig('graph.png', dpi=300, bbox_inches='tight') # PNG
plt.savefig('graph.pdf', bbox_inches='tight') # PDF
plt.savefig('graph.svg', bbox_inches='tight') # SVG

# ํˆฌ๋ช… ๋ฐฐ๊ฒฝ
plt.savefig('graph.png', dpi=300, bbox_inches='tight', transparent=True)

plt.close() # ๋ฉ”๋ชจ๋ฆฌ ์ •๋ฆฌ

Hรคufig gestellte Fragenโ€‹

ํ•œ๊ธ€์ด ๊นจ์ ธ์š”!โ€‹

import matplotlib.pyplot as plt

# macOS
plt.rcParams['font.family'] = 'AppleGothic'

# Windows
plt.rcParams['font.family'] = 'Malgun Gothic'

# Linux
plt.rcParams['font.family'] = 'NanumGothic'

# ๋งˆ์ด๋„ˆ์Šค ๊ธฐํ˜ธ ๊นจ์ง ๋ฐฉ์ง€
plt.rcParams['axes.unicode_minus'] = False

๊ทธ๋ž˜ํ”„๊ฐ€ ๊ฒน์ณ์š”!โ€‹

# tight_layout ์‚ฌ์šฉ
plt.tight_layout()

# ๋˜๋Š” ์—ฌ๋ฐฑ ์กฐ์ •
plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1,
wspace=0.3, hspace=0.3)

๊ณ ํ•ด์ƒ๋„ ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค๊ณ  ์‹ถ์–ด์š”!โ€‹

# DPI(์ธ์น˜๋‹น ๋„ํŠธ ์ˆ˜) ๋†’์ด๊ธฐ
fig = plt.figure(figsize=(10, 6), dpi=300)

# ๋˜๋Š” ์ €์žฅ ์‹œ
plt.savefig('graph.png', dpi=300)

Pandas DataFrame์„ ๋ฐ”๋กœ ๊ทธ๋ฆด ์ˆ˜ ์žˆ๋‚˜์š”?โ€‹

import pandas as pd

df = pd.DataFrame({
'A': [1, 2, 3, 4],
'B': [4, 3, 2, 1]
})

# Pandas์˜ plot ๋ฉ”์„œ๋“œ
df.plot(kind='line')
df.plot(kind='bar')
df.plot(kind='scatter', x='A', y='B')
plt.show()

Nรคchste Schritteโ€‹

Matplotlib ๊ธฐ์ดˆ๋ฅผ ์ตํ˜”๋‹ค๋ฉด:

  1. Seaborn: ๋” ์•„๋ฆ„๋‹ค์šด ํ†ต๊ณ„ ๊ทธ๋ž˜ํ”„
  2. Plotly: ์ธํ„ฐ๋ž™ํ‹ฐ๋ธŒ ์‹œ๊ฐํ™”
  3. ๋Œ€์‹œ๋ณด๋“œ: Streamlit, Dash๋กœ ์›น ๋Œ€์‹œ๋ณด๋“œ
  4. ๊ณ ๊ธ‰ ์‹œ๊ฐํ™”: 3D ๊ทธ๋ž˜ํ”„, ์• ๋‹ˆ๋ฉ”์ด์…˜

Referenzenโ€‹