zenn.skin 무료버전 배포중!
자세히보기

Python/Syntax

[matplotlib] 텍스트 추가하기, annotate

koosco! 2022. 9. 2. 05:13

matplotlib의 annotate는 지정된 xy위치에 text를 삽입해주는 함수입니다.

파라미터 설명
text : str 추가할 문자열
xy : (float, float) 추가할 (x, y) 위치
xytext : (float, float) xy위치에서 조정할 텍스트 위치
textcoords : str or Artist or Transform or (float, float) - default: value of xycoords
xytext가 제공되는 좌표계
'offset points': xy값으로부터의 offset(point)
'offset pixels': xy값으로부터의 offset(pixel)

 

annotate와 산점도를 사용하면 다음과 같이 정보를 표현할 수 있습니다.

import matplotlib.pyplot as plt

friends = [70, 65, 72, 63, 71, 64, 60, 64, 67]
minutes = [175, 170, 205, 120, 220, 130, 105, 145, 190]
labels = [chr(97 + i) for i in range(len(friends))]

plt.scatter(friends, minutes)
for label, friend_count, minute_count in zip(labels, friends, minutes):
    plt.annotate(label,
            xy=(friend_count, minute_count),
            xytext=(5, -5),
            textcoords='offset points')
plt.show()


참고

밑바닥부터 시작하는 데이터과학2판, 조엘 그루스

'Python/Syntax'의 다른글

  • 현재글 [matplotlib] 텍스트 추가하기, annotate

관련글