코딩/시각화

[matplotlib.pyplot] suptitle

CDeo 2024. 11. 1. 22:13

`matplotlib.pyplot`에서 가끔 부제목이 필요할 때가 있다. 특별히 `subtitle`이 구현되어있지는 않지만, `suptitle`은 구현되어있다.

matplotlib.pyplot.suptitle(t, **kwargs)

취향차이겠지만, 저는 중앙에 부제목이 오는것이 좋아 `x`파라미터는 건들지 않습니다. 그 외에 자주 수정하는 파라미터는 `y`, `fontsize`정도 입니다. 아래는 기본적인 경우입니다.

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [10, 20, 15, 25, 30]

plt.plot(x, y)

plt.title("Title")
plt.suptitle("Subtitle")

plt.show()

 

디폴트로 들어간 `y`를 사용하면 결과물이 제 예상과 크게 다름으로, `y`파라미터를 main `title`과 스케일에 맞추어 수정합니다. 이 부분이 시간이 제일 많이 걸립니다.

plt.title("Title", y = 1.05)
plt.suptitle("Subtitle", y = 0.92)

`fontsize`는 `'medium'`으로 바꾸어 부제목 처럼 본제목보다 작게 바꾸어 줍니다.

plt.suptitle("Subtitle", y = 0.92, fontsize='medium')

출처

https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.suptitle.html

 

matplotlib.pyplot.suptitle — Matplotlib 3.9.2 documentation

The horizontal alignment of the text relative to (x, y).

matplotlib.org