주석 연결 스타일

Beginner

This tutorial is from open-source community. Access the source code

소개

이 랩에서는 Python 의 Matplotlib 라이브러리를 사용하여 주석 연결 스타일을 만드는 과정을 안내합니다. 주석은 그래프에서 특정 데이터 포인트를 설명하거나 강조하는 데 도움이 되므로 데이터 시각화에서 중요한 도구입니다. 주석의 연결 스타일은 주석을 데이터 포인트에 연결하는 선의 모양과 스타일을 나타냅니다.

VM 팁

VM 시작이 완료되면 왼쪽 상단을 클릭하여 Notebook 탭으로 전환하여 실습을 위해 Jupyter Notebook에 액세스하십시오.

때로는 Jupyter Notebook 이 로딩을 완료하는 데 몇 초 정도 기다려야 할 수 있습니다. Jupyter Notebook 의 제한 사항으로 인해 작업의 유효성 검사는 자동화할 수 없습니다.

학습 중에 문제가 발생하면 Labby 에게 문의하십시오. 세션 후 피드백을 제공하면 문제를 즉시 해결해 드리겠습니다.

필요한 라이브러리 가져오기

주석을 생성하기 전에 필요한 라이브러리를 가져와야 합니다. 이 경우 Matplotlib 라이브러리를 사용합니다.

import matplotlib.pyplot as plt

주석 연결 스타일을 생성하기 위한 함수 정의

두 개의 매개변수, 즉 축 객체와 연결 스타일을 입력으로 받는 함수를 정의합니다. 이 함수는 두 개의 데이터 포인트를 플롯하고 지정된 연결 스타일로 주석을 생성합니다.

def demo_con_style(ax, connectionstyle):
    x1, y1 = 0.3, 0.2
    x2, y2 = 0.8, 0.6

    ax.plot([x1, x2], [y1, y2], ".")
    ax.annotate("",
                xy=(x1, y1), xycoords='data',
                xytext=(x2, y2), textcoords='data',
                arrowprops=dict(arrowstyle="->", color="0.5",
                                shrinkA=5, shrinkB=5,
                                patchA=None, patchB=None,
                                connectionstyle=connectionstyle,
                                ),
                )

    ax.text(.05, .95, connectionstyle.replace(",", ",\n"),
            transform=ax.transAxes, ha="left", va="top")

주석 연결 스타일 생성

demo_con_style 함수를 사용하여 다양한 주석 연결 스타일을 생성하고 그리드에 플롯합니다.

fig, axs = plt.subplots(3, 5, figsize=(7, 6.3), layout="constrained")
demo_con_style(axs[0, 0], "angle3,angleA=90,angleB=0")
demo_con_style(axs[1, 0], "angle3,angleA=0,angleB=90")
demo_con_style(axs[0, 1], "arc3,rad=0.")
demo_con_style(axs[1, 1], "arc3,rad=0.3")
demo_con_style(axs[2, 1], "arc3,rad=-0.3")
demo_con_style(axs[0, 2], "angle,angleA=-90,angleB=180,rad=0")
demo_con_style(axs[1, 2], "angle,angleA=-90,angleB=180,rad=5")
demo_con_style(axs[2, 2], "angle,angleA=-90,angleB=10,rad=5")
demo_con_style(axs[0, 3], "arc,angleA=-90,angleB=0,armA=30,armB=30,rad=0")
demo_con_style(axs[1, 3], "arc,angleA=-90,angleB=0,armA=30,armB=30,rad=5")
demo_con_style(axs[2, 3], "arc,angleA=-90,angleB=0,armA=0,armB=40,rad=0")
demo_con_style(axs[0, 4], "bar,fraction=0.3")
demo_con_style(axs[1, 4], "bar,fraction=-0.3")
demo_con_style(axs[2, 4], "bar,angle=180,fraction=-0.2")

for ax in axs.flat:
    ax.set(xlim=(0, 1), ylim=(0, 1.25), xticks=[], yticks=[], aspect=1.25)
fig.set_constrained_layout_pads(wspace=0, hspace=0, w_pad=0, h_pad=0)

plt.show()

결과 해석

다양한 연결 스타일을 가진 주석의 결과 그리드가 표시됩니다. 주석은 그래프에서 데이터 포인트를 강조하는 데 도움이 되며, 다양한 스타일은 Matplotlib 의 주석 기능의 다재다능함을 보여줍니다.

요약

이 랩에서는 Python 의 Matplotlib 라이브러리를 사용하여 주석 연결 스타일을 만드는 방법에 대한 개요를 제공했습니다. 주석은 데이터 시각화에서 유용한 도구이며, 그래프에서 특정 데이터 포인트를 설명하거나 강조하는 데 사용할 수 있습니다. 주석의 연결 스타일은 주석을 데이터 포인트에 연결하는 선의 모양과 스타일을 나타냅니다. 이 랩에 설명된 단계를 따르면, 이제 Matplotlib 에서 자신만의 주석 연결 스타일을 만들 수 있습니다.