소개
이 튜토리얼에서는 Python 의 Matplotlib 을 사용하여 화살표 스타일 참조 차트를 만드는 과정을 안내합니다. 이 차트는 ~.Axes.annotate에서 사용할 수 있는 다양한 화살표 스타일을 표시합니다.
VM 팁
VM 시작이 완료되면 왼쪽 상단을 클릭하여 Notebook 탭으로 전환하여 실습을 위해 Jupyter Notebook에 액세스하십시오.
때로는 Jupyter Notebook 이 로딩을 완료하는 데 몇 초 정도 기다려야 할 수 있습니다. Jupyter Notebook 의 제한으로 인해 작업의 유효성 검사를 자동화할 수 없습니다.
학습 중 문제가 발생하면 언제든지 Labby 에게 문의하십시오. 세션 후 피드백을 제공해주시면 문제를 즉시 해결해 드리겠습니다.
필요한 라이브러리 가져오기
화살표 스타일 참조 차트를 만들기 위해 필요한 라이브러리를 가져옵니다.
import inspect
import itertools
import re
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
화살표 스타일 가져오기
mpatches.ArrowStyle.get_styles()를 사용하여 ~.Axes.annotate에서 사용할 수 있는 모든 화살표 스타일을 가져옵니다.
styles = mpatches.ArrowStyle.get_styles()
Figure 설정
plt.figure()와 add_gridspec()을 사용하여 figure 를 설정합니다. figure 는 2 개의 열과 n 개의 행으로 구성된 그리드를 갖습니다. 여기서 n 은 화살표 스타일의 수입니다. 그리드의 각 셀에는 화살표 스타일과 기본 매개변수가 포함됩니다.
ncol = 2
nrow = (len(styles) + 1) // ncol
axs = (plt.figure(figsize=(4 * ncol, 1 + nrow))
.add_gridspec(1 + nrow, ncol,
wspace=.7, left=.1, right=.9, bottom=0, top=1).subplots())
for ax in axs.flat:
ax.set_axis_off()
for ax in axs[0, :]:
ax.text(0, .5, "arrowstyle",
transform=ax.transAxes, size="large", color="tab:blue",
horizontalalignment="center", verticalalignment="center")
ax.text(.35, .5, "default parameters",
transform=ax.transAxes,
horizontalalignment="left", verticalalignment="center")
화살표 스타일 플롯
각 화살표 스타일을 그리드의 셀에 해당 기본 매개변수와 함께 플롯합니다. ax.annotate()를 사용하여 화살표 스타일 이름과 기본 매개변수를 셀에 추가합니다.
for ax, (stylename, stylecls) in zip(axs[1:, :].T.flat, styles.items()):
l, = ax.plot(.25, .5, "ok", transform=ax.transAxes)
ax.annotate(stylename, (.25, .5), (-0.1, .5),
xycoords="axes fraction", textcoords="axes fraction",
size="large", color="tab:blue",
horizontalalignment="center", verticalalignment="center",
arrowprops=dict(
arrowstyle=stylename, connectionstyle="arc3,rad=-0.05",
color="k", shrinkA=5, shrinkB=5, patchB=l,
),
bbox=dict(boxstyle="square", fc="w"))
## wrap at every nth comma (n = 1 or 2, depending on text length)
s = str(inspect.signature(stylecls))[1:-1]
n = 2 if s.count(',') > 3 else 1
ax.text(.35, .5,
re.sub(', ', lambda m, c=itertools.count(1): m.group()
if next(c) % n else '\n', s),
transform=ax.transAxes,
horizontalalignment="left", verticalalignment="center")
차트 표시
plt.show()를 사용하여 화살표 스타일 참조 차트를 표시합니다.
plt.show()
요약
이 튜토리얼에서는 Python 의 Matplotlib 을 사용하여 화살표 스타일 참조 차트를 만드는 방법을 배웠습니다. mpatches.ArrowStyle.get_styles()를 사용하여 ~.Axes.annotate에서 사용할 수 있는 모든 화살표 스타일을 가져오고, plt.figure() 및 add_gridspec()을 사용하여 figure 를 설정하고, 그리드의 각 셀에 각 화살표 스타일을 플롯하고, plt.show()를 사용하여 차트를 표시했습니다.