Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Matplotlib에서 하나의 텍스트로 여러 지점에 주석을 추가하는 방법은 무엇입니까?

<시간/>

Matplotlib에 여러 지점에 대해 주석이 달린 텍스트를 추가하려면 다음 단계를 수행할 수 있습니다. -

  • 그림 크기를 설정하고 서브플롯 사이 및 주변 여백을 조정합니다.
  • numpy를 사용하여 x 및 y 데이터 포인트를 생성합니다.
  • 각 산점에 대한 레이블을 설정하려면 레이블 목록을 만드십시오.
  • scatter()를 사용하여 xpoints, ypoints 플롯 방법. 색상은 xpoint를 사용하세요.
  • 압축 레이블, xpoint 및 ypoint를 반복합니다.
  • for 루프에서 굵은 LaTeX 표현과 함께 annotate() 메서드를 사용합니다.
  • 그림을 표시하려면 show()를 사용하세요. 방법.

예시

import numpy as np
from matplotlib import pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

xpoints = np.linspace(1, 10, 10)
ypoints = np.random.rand(10)

labels = ["%.2f" % i for i in xpoints]

plt.scatter(xpoints, ypoints, c=xpoints)

for label, x, y in zip(labels, xpoints, ypoints):
   plt.annotate(
      f"$\\bf{label}$",
      xy=(x, y), xytext=(-20, 20),
      textcoords='offset points', ha='center', va='bottom',
      arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0'))

plt.show()

출력

Matplotlib에서 하나의 텍스트로 여러 지점에 주석을 추가하는 방법은 무엇입니까? Matplotlib에서 하나의 텍스트로 여러 지점에 주석을 추가하는 방법은 무엇입니까?