Retrospectives Badge

1. 실습

여러 시각화 기법과 관련된 다양한 주제를 matplotlib과 함께 다른 라이브러리를 사용하여 극좌표계, Radar chart, Pie chart, Bar chart, subplot 생성 및 배열, 고급 시각화 구현, 색상 팔레트 변경, 다크모드 시각화를 실습했습니다.

1. Matplotlib를 통한 극좌표계에 대한 이해

극좌표계는 데이터를 각도와 반지름으로 표현하는 방식입니다. 주로 원형 데이터 시각화에 사용됩니다.

import matplotlib.pyplot as plt
import numpy as np

# 데이터 생성
labels = ['A', 'B', 'C', 'D', 'E']
values = [3, 4, 2, 5, 4]
angles = np.linspace(0, 2 * np.pi, len(labels), endpoint=False).tolist()
values += values[:1]
angles += angles[:1]

# 플롯 설정
fig, ax = plt.subplots(figsize=(6, 6), subplot_kw=dict(polar=True))
ax.fill(angles, values, color='blue', alpha=0.25)
ax.plot(angles, values, color='blue', linewidth=2)
ax.set_yticklabels([])  # 반지름에 대한 레이블 숨기기
ax.set_xticks(angles[:-1])
ax.set_xticklabels(labels)

plt.title("Polar Coordinate System Example")
plt.show()

2. Matplotlib를 통한 Radar chart에 대한 이해

Radar chart는 여러 차원을 원형으로 나타내는 차트입니다. 주로 성과를 비교하는 데 사용됩니다.

# Radar chart 설정
fig, ax = plt.subplots(figsize=(6, 6), subplot_kw=dict(polar=True))
ax.fill(angles, values, color='green', alpha=0.25)
ax.plot(angles, values, color='green', linewidth=2)
ax.set_yticklabels([])
ax.set_xticks(angles[:-1])
ax.set_xticklabels(labels)

plt.title("Radar Chart Example")
plt.show()

3. Matplotlib를 통한 Pie chart와 Bar chart에 대한 이해

# Pie chart
sizes = [20, 30, 25, 25]
labels = ['A', 'B', 'C', 'D']
colors = ['#ff9999','#66b3ff','#99ff99','#ffcc99']

plt.figure(figsize=(6, 6))
plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=140)
plt.title("Pie Chart Example")
plt.show()

# Bar chart
x = np.arange(len(labels))

plt.figure(figsize=(6, 6))
plt.bar(x, sizes, color=colors)
plt.xticks(x, labels)
plt.title("Bar Chart Example")
plt.show()

4. Matplotlib를 통한 Pie chart의 다양한 변형

# Exploded Pie chart
explode = (0.1, 0, 0, 0)  # 'A' 부분을 분리
plt.figure(figsize=(6, 6))
plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=140, explode=explode)
plt.title("Exploded Pie Chart Example")
plt.show()

5. 다양한 라이브러리를 통한 시각화 구현

missingno - 결측값 시각화

import missingno as msno
import pandas as pd
import numpy as np

# 결측값이 포함된 데이터프레임 생성
data = pd.DataFrame({
    'A': [1, 2, np.nan, 4],
    'B': [5, np.nan, np.nan, 8],
    'C': [9, 10, 11, 12]
})

# 결측값 시각화
msno.matrix(data)
plt.show()

squarify - TreeMap 시각화

import squarify

sizes = [50, 25, 15, 10]
labels = ['Group A', 'Group B', 'Group C', 'Group D']

plt.figure(figsize=(8, 6))
squarify.plot(sizes=sizes, label=labels, alpha=0.7)
plt.title("TreeMap Example")
plt.show()

pywaffle - Waffle Chart 시각화

from pywaffle import Waffle

data = {'Category A': 50, 'Category B': 30, 'Category C': 20}

plt.figure(
    FigureClass=Waffle, 
    rows=5, 
    values=data, 
    colors=["#232066", "#983D3D", "#DCB732"],
    legend={'loc': 'upper left', 'bbox_to_anchor': (1.1, 1)}
)
plt.title("Waffle Chart Example")
plt.show()

matplotlib_venn - Venn Diagram 시각화

from matplotlib_venn import venn2

plt.figure(figsize=(6, 6))
venn2(subsets = (10, 20, 5), set_labels = ('Group A', 'Group B'))
plt.title("Venn Diagram Example")
plt.show()

6. add_subplot을 이용해 subplot 생성하기

fig = plt.figure(figsize=(10, 5))

ax1 = fig.add_subplot(1, 2, 1)
ax1.plot([1, 2, 3], [4, 5, 6])
ax1.set_title("Subplot 1")

ax2 = fig.add_subplot(1, 2, 2)
ax2.plot([1, 2, 3], [6, 5, 4])
ax2.set_title("Subplot 2")

plt.show()

7. Axes로 subplot 배열 구성하기

fig, axes = plt.subplots(2, 2, figsize=(10, 10))

axes[0, 0].plot([1, 2, 3], [4, 5, 6])
axes[0, 0].set_title("Subplot 1")

axes[0, 1].plot([1, 2, 3], [6, 5, 4])
axes[0, 1].set_title("Subplot 2")

axes[1, 0].plot([1, 2, 3], [4, 4, 4])
axes[1, 0].set_title("Subplot 3")

axes[1, 1].plot([1, 2, 3], [6, 6, 6])
axes[1, 1].set_title("Subplot 4")

plt.tight_layout()
plt.show()

8. Grid, Line & Span, Setting 관련 함수들을 통한 고급 시각화 구현

fig, ax = plt.subplots(figsize=(8, 6))
x = np.linspace(0, 10, 100)
y = np.sin(x)

ax.plot(x, y)

# 그리드 추가
ax.grid(True)

# 특정 영역에 대해 Span 추가
ax.axvspan(2, 4, color='yellow', alpha=0.3)

# 특정 라인 추가
ax.axhline(0.5, color='red', linestyle='--')
ax.axvline(5, color='blue', linestyle='--')

plt.title("Advanced Visualization Example")
plt.show()

9. cycler을 사용해 색상 팔레트 변경하기

from cycler import cycler

# 색상 팔레트 설정
plt.rc('axes', prop_cycle=(cycler('color', ['#FF5733', '#33FF57', '#3357FF', '#FF33FF'])))

x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.tan(x)

plt.plot(x, y1)
plt.plot(x, y2)
plt.plot(x, y3)

plt.title("Custom Color Palette Example")
plt.show()

10. 다크모드 시각화 구현하기

plt.style.use('dark_background')

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

plt.plot(x, y, color='cyan')
plt.title("Dark Mode Visualization Example")
plt.show()

2. 오피스아워