본문 바로가기

코딩

[Python] Matplotlib 라이브러리

 

내용 코드
import import matplotlib.pyplot as plt
선 그래프 생성
*선 마커, 선 모양, 선 색 설정 가능
plt.plot(x, y)
그래프 제목 삽입 plt.title("제목", fontsize=수)
x축 레이블 삽입 plt.xlabel("레이블", fontsize=수)
y축 레이블 삽입 plt.ylabel("레이블", fontsize=수)
여러 선 그래프 생성 a = [(g1, 'g1'), (g2, 'g2'), (g3, 'g3')]

for m, n in a:
    plt.plot(x, m, label=n)

plt.legend()
그린 그래프를 화면에 표시 plt.show()
x축 눈금 설정 plt.xticks([2018, 2019, 2020, 2021])
격자 추가 plt.grid(True)
DataFrame 특정 col 값으로 선 그리기
*row명(인덱스명)이 x로, 값이 y로
plt.plot(df["col"])

 

내용 코드
figure 생성 fig1 = plt.figure(아이디, figsize=(가로, 세로))
서브플롯 제목 삽입 ax1.set_title("제목")

 

figure1 = plt.figure(1, figsize=(5,4))
plt.plot(x1, y1)

figure2 = plt.figure(2, figsize=(4,3))
plt.plot(x1, y2)
plt.title("second figure")

plt.figure(1)
plt.title("first figure")

plt.show()

 

#방법1
fig, ax = plt.subplots(1, 2, constrained_layout=True)

ax[0].plot(x, y1)
ax[1].plot(x, y2)

plt.show()

#방법2
fig, (ax1, ax2) = plt.subplots(1, 2, constrained_layout=True)

ax1.plot(x, y1)
ax2.plot(x, y2)

plt.show()