Matplotlib の数式レンダリングエンジン

Beginner

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

はじめに

この実験では、Matplotlib の数式レンダリングエンジンの選択された機能を示す図を作成する方法を案内します。この図では、添え字、上付き文字、分数、二項係数、積み重ねられた数字、ルート、フォント、アクセント付き文字、ギリシャ文字、ヘブライ文字、区切り記号、関数、記号の例を使って、数学的な式を書く方法を示します。

VM のヒント

VM の起動が完了したら、左上隅をクリックしてノートブックタブに切り替え、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 の数式レンダリングエンジンの選択された機能を示す図を作成する方法を示しました。この図は、添え字、上付き文字、分数、二項式、積み重ねられた数字、根号、フォント、アクセント、ギリシャ文字、ヘブライ文字、区切り記号、関数、記号の例を使って数学的式を書く方法を示しています。