Matplotlib 수학 렌더링 엔진

Beginner

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

소개

이 랩에서는 Matplotlib 의 수학 렌더링 엔진의 선택된 기능을 보여주는 그림을 만드는 방법을 안내합니다. 이 그림은 아래첨자, 위첨자, 분수, 이항식, 스택 숫자, 근호, 글꼴, 악센트, 그리스 문자, 히브리 문자, 구분 기호, 함수 및 기호의 예시를 통해 수학적 표현식을 작성하는 방법을 보여줍니다.

VM 팁

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

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

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

라이브러리 임포트

이 단계에서는 이 랩에 필요한 라이브러리를 임포트합니다.

import matplotlib.pyplot as plt

데모 예제 설정

이 단계에서는 그림에서 보여줄 수학 표현식의 데모 예제를 설정합니다.

mathtext_demos = {
    "Header demo":
        r"$W^{3\beta}_{\delta_1 \rho_1 \sigma_2} = "
        r"U^{3\beta}_{\delta_1 \rho_1} + \frac{1}{8 \pi 2} "
        r"\int^{\alpha_2}_{\alpha_2} d \alpha^\prime_2 \left[\frac{ "
        r"U^{2\beta}_{\delta_1 \rho_1} - \alpha^\prime_2U^{1\beta}_"
        r"{\rho_1 \sigma_2} }{U^{0\beta}_{\rho_1 \sigma_2}}\right]$",

    "Subscripts and superscripts":
        r"$\alpha_i > \beta_i,\ "
        r"\alpha_{i+1}^j = {\rm sin}(2\pi f_j t_i) e^{-5 t_i/\tau},\ "
        r"\ldots$",

    "Fractions, binomials and stacked numbers":
        r"$\frac{3}{4},\ \binom{3}{4},\ \genfrac{}{}{0}{}{3}{4},\ "
        r"\left(\frac{5 - \frac{1}{x}}{4}\right),\ \ldots$",

    "Radicals":
        r"$\sqrt{2},\ \sqrt[3]{x},\ \ldots$",

    "Fonts":
        r"$\mathrm{Roman}\ , \ \mathit{Italic}\ , \ \mathtt{Typewriter} \ "
        r"\mathrm{or}\ \mathcal{CALLIGRAPHY}$",

    "Accents":
        r"$\acute a,\ \bar a,\ \breve a,\ \dot a,\ \ddot a, \ \grave a, \ "
        r"\hat a,\ \tilde a,\ \vec a,\ \widehat{xyz},\ \widetilde{xyz},\ "
        r"\ldots$",

    "Greek, Hebrew":
        r"$\alpha,\ \beta,\ \chi,\ \delta,\ \lambda,\ \mu,\ "
        r"\Delta,\ \Gamma,\ \Omega,\ \Phi,\ \Pi,\ \Upsilon,\ \nabla,\ "
        r"\aleph,\ \beth,\ \daleth,\ \gimel,\ \ldots$",

    "Delimiters, functions and Symbols":
        r"$\coprod,\ \int,\ \oint,\ \prod,\ \sum,\ "
        r"\log,\ \sin,\ \approx,\ \oplus,\ \star,\ \varpropto,\ "
        r"\infty,\ \partial,\ \Re,\ \leftrightsquigarrow, \ \ldots$",
}

그림 및 축 생성

이 단계에서는 수학 표현식 예제를 위한 그림과 축을 생성합니다.

## Creating figure and axis.
fig = plt.figure(figsize=(7, 7))
ax = fig.add_axes([0.01, 0.01, 0.98, 0.90],
                  facecolor="white", frameon=True)
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_title("Matplotlib's math rendering engine",
             color=mpl_grey_rgb, fontsize=14, weight='bold')
ax.set_xticks([])
ax.set_yticks([])

줄 간격 정의

이 단계에서는 축 좌표에서 줄 사이의 간격을 정의합니다.

n_lines = len(mathtext_demos)
line_axesfrac = 1 / n_lines

헤더 데모 공식 플롯

이 단계에서는 헤더 데모 공식을 플롯합니다.

full_demo = mathtext_demos['Header demo']
ax.annotate(full_demo,
            xy=(0.5, 1. - 0.59 * line_axesfrac),
            color='tab:orange', ha='center', fontsize=20)

기능 데모 공식 플롯

이 단계에서는 기능 데모 공식을 플롯합니다.

for i_line, (title, demo) in enumerate(mathtext_demos.items()):
    if i_line == 0:
        continue

    baseline = 1 - i_line * line_axesfrac
    baseline_next = baseline - line_axesfrac
    fill_color = ['white', 'tab:blue'][i_line % 2]
    ax.axhspan(baseline, baseline_next, color=fill_color, alpha=0.2)
    ax.annotate(f'{title}:',
                xy=(0.06, baseline - 0.3 * line_axesfrac),
                color=mpl_grey_rgb, weight='bold')
    ax.annotate(demo,
                xy=(0.04, baseline - 0.75 * line_axesfrac),
                color=mpl_grey_rgb, fontsize=16)

그림 표시

이 단계에서는 그림을 표시합니다.

plt.show()

요약

이 랩에서는 Matplotlib 의 수학 렌더링 엔진의 선택된 기능을 보여주는 그림을 만드는 방법을 시연했습니다. 이 그림은 아래첨자, 위첨자, 분수, 이항식, 스택 숫자, 근호, 글꼴, 악센트, 그리스 문자, 히브리 문자, 구분 기호, 함수 및 기호의 예시를 통해 수학적 표현식을 작성하는 방법을 보여주었습니다.