맷플롯립 기능 명단
1. 기본 그래프 생성
import matplotlib.pyplot as plt
import numpy as np
<!-- -->
# 선 그래프
plt.plot([1, 2, 3, 4], [1, 4, 2, 3])
<!-- -->
# 산점도
plt.scatter([1, 2, 3, 4], [1, 4, 2, 3])
<!-- -->
# 막대 그래프
plt.bar(['A', 'B', 'C'], [1, 2, 3])
<!-- -->
# 히스토그램
plt.hist([1, 1, 2, 3, 3, 3, 4])
<!-- -->
# 그래프 표시
plt.show()
2. 그래프 커스터마이징
<!-- -->
# 제목과 축 레이블
plt.title('그래프 제목')
plt.xlabel('x축 레이블')
plt.ylabel('y축 레이블')
<!-- -->
# 범례
plt.plot([1, 2, 3], label='선 1')
plt.plot([2, 3, 4], label='선 2')
plt.legend()
<!-- -->
# 색상과 스타일
plt.plot([1, 2, 3], color='red', linestyle='--', marker='o')
plt.scatter([1, 2, 3], [1, 2, 3], c='blue', s=100, alpha=0.5)
<!-- -->
# 축 범위 설정
plt.xlim(0, 10)
plt.ylim(-5, 5)
3. 서브플롯
<!-- -->
# 기본 서브플롯
plt.subplot(2, 2, 1) # 2x2 그리드의 첫 번째
plt.plot([1, 2, 3])
plt.subplot(2, 2, 2) # 2x2 그리드의 두 번째
plt.scatter([1, 2, 3], [1, 2, 3])
<!-- -->
# figure와 axes 사용
fig, axes = plt.subplots(2, 2)
axes[0, 0].plot([1, 2, 3])
axes[0, 1].scatter([1, 2, 3], [1, 2, 3])
4. 고급 그래프 유형
<!-- -->
# 파이 차트
plt.pie([30, 20, 10], labels=['A', 'B', 'C'])
<!-- -->
# 박스플롯
plt.boxplot([1, 2, 3], [4, 5, 6](1-2-3-4-5-6.html))
<!-- -->
# 등고선 그래프
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X**2 + Y**2))
plt.contour(X, Y, Z)
<!-- -->
# 3D 그래프
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z)
5. 스타일링과 테마
<!-- -->
# 스타일 설정
plt.style.use('seaborn')
plt.style.use('dark_background')
plt.style.use('ggplot')
<!-- -->
# 그림 크기 설정
plt.figure(figsize=(10, 6))
<!-- -->
# 격자 표시
plt.grid(True)
<!-- -->
# 틱 설정
plt.xticks([0, 1, 2], ['A', 'B', 'C'])
plt.yticks(rotation=45)
6. 텍스트와 주석
<!-- -->
# 텍스트 추가
plt.text(x=1, y=2, s='텍스트')
<!-- -->
# 화살표 주석
plt.annotate('최대값', xy=(2, 3), xytext=(3, 4),
arrowprops=dict(facecolor='black', shrink=0.05))
<!-- -->
# 수식 표현
plt.title(r'$y=x^2$') # LaTeX 형식 수식
7. 이미지 처리
<!-- -->
# 이미지 표시
img = plt.imread('image.png')
plt.imshow(img)
<!-- -->
# 컬러맵
plt.imshow(Z, cmap='viridis')
plt.colorbar()
<!-- -->
# 이미지 저장
plt.savefig('graph.png', dpi=300, bbox_inches='tight')
8. 그래프 레이아웃
<!-- -->
# 여백 조정
plt.tight_layout()
<!-- -->
# 그래프 간격 조정
plt.subplots_adjust(wspace=0.3, hspace=0.3)
<!-- -->
# 그래프 겹치기
plt.fill_between(x, y1, y2, alpha=0.3)
9. 인터랙티브 기능
<!-- -->
# 대화형 모드 켜기
plt.ion()
<!-- -->
# 실시간 업데이트
for i in range(100):
plt.clf() # 이전 그래프 지우기
plt.plot(np.random.rand(10))
plt.pause(0.1)
<!-- -->
# 대화형 모드 끄기
plt.ioff()
10. 다중 축과 트윈 축
<!-- -->
# 두 개의 y축
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(x, y1, 'g-')
ax2.plot(x, y2, 'b-')
<!-- -->
# 다중 축 설정
ax1.set_ylabel('첫 번째 축', color='g')
ax2.set_ylabel('두 번째 축', color='b')
모든 그래프는 마지막에 plt.show()를 호출하여 표시합니다.