Matplotlib で三角関数の加法定理の証明用の図を描く

三角関数の加法定理の証明用の図を Matplotlib だけで描く。以下のページで使っているので。

しかし,plt.rcParams["text.usetex"] = True してみたけど,\boldsymbol{a} がなぜか使えないなぁ。しかたがないので \vec{a} で。

In [1]:
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np

# グラフを SVG で Notebook にインライン表示
%config InlineBackend.figure_formats = ['svg']

plt.rcParams['mathtext.fontset'] = 'cm'
plt.rcParams["text.usetex"] = True
In [2]:
fig = plt.figure(figsize=[6.4, 4.8])
ax = fig.add_subplot(aspect='equal')
fig.tight_layout()

# 円弧
th1 = -20
th2 = 40
arc1 = patches.Arc(
        xy=(0, 0), width=2, height=2, 
        theta1=th1, theta2=th2, edgecolor="purple", linewidth=1)
ax.add_patch(arc1)

# ベクトル OA
xA = np.cos(np.radians(30))
yA = np.sin(np.radians(30))
plt.quiver([0], [0], [xA], [yA], color="blue", lw = 3,
          angles='xy', scale_units='xy', scale=1
          )
# ベクトル OB
xB = np.cos(np.radians(-15))
yB = np.sin(np.radians(-15))
plt.quiver([0], [0], [xB], [yB], color="red", lw = 3,
          angles='xy', scale_units='xy', scale=1
          )

# 点 O
xO = 0
yO = 0
ax.text(xO-0.07, yO-0.07, r"$O$", fontsize="xx-large", c='k')
# 点 A
ax.text(xA+0.01, yA+0.01, r"$A$", fontsize="xx-large", c='k')
# 点 B
ax.text(xB+0.01, yB-0.05, r"$B$", fontsize="xx-large", c='k')

# 角度 θ 部分
arc1 = patches.Arc(
        xy=(0, 0), width=0.8, height=0.8, 
        theta1=-15, theta2=30, edgecolor="k", linewidth=1)
arc2 = patches.Arc(
        xy=(0, 0), width=0.82, height=0.82, 
        theta1=-15, theta2=30, edgecolor="k", linewidth=1)
ax.add_patch(arc1)
ax.add_patch(arc2)
ax.text(0.43, 0.03, r"$\theta = \alpha + \beta$", fontsize="xx-large", c='k')

# 角度 α 部分
arca = patches.Arc(
        xy=(0, 0), width=0.45, height=0.45, 
        theta1=0, theta2=30, edgecolor="blue", linewidth=2)
ax.add_patch(arca)
ax.text(0.24, 0.04, r"$\alpha$", fontsize="xx-large", c='blue')

# 角度 β 部分
arcb = patches.Arc(
        xy=(0, 0), width=0.45, height=0.45, 
        theta1=-15, theta2=0, edgecolor="red", linewidth=2)
ax.add_patch(arcb)
ax.text(0.25, -0.05, r"$\beta$", fontsize="xx-large", c='red')

# ベクトルのラベル
ax.text(0.27, 0.3, r"$\overrightarrow{OA} = \vec{a}$", fontsize="xx-large", c='blue')
ax.text(0.4, -0.23, r"$\overrightarrow{OB} = \vec{b}$", fontsize="xx-large", c='red')

# ベクトルの成分
plt.plot([xA, 0], [yA, yA], ls = ':',lw=0.8, c="blue", alpha=0.3)
ax.text(-0.06, yA-0.01, r"$a_y$", fontsize="x-large", c='blue')
plt.plot([xA, xA], [yA, 0], ls = ':',lw=0.8, c="blue", alpha=0.3)
ax.text(xA-0.01, -0.04, r"$a_x$", fontsize="x-large", c='blue')
plt.plot([xB, 0], [yB, yB], ls = ':',lw=0.8, c="red", alpha=0.3)
ax.text(-0.06, yB-0.01, r"$b_y$", fontsize="x-large", c='red')
plt.plot([xB, xB], [yB, 0], ls = ':',lw=0.8, c="red", alpha=0.3)
ax.text(xB-0.02, +0.02, r"$b_x$", fontsize="x-large", c='red')

# 表示範囲
ax.set_xlim(-0.1, 1.1)
ax.set_ylim(-0.4, 0.7)

# 目盛設定
ax.axis(False);
# x軸 y軸は dashed に
ax.axhline(0, c='gray', ls = ':', lw=1)
ax.axvline(0, c='gray', ls = ':', lw=1);

plt.savefig('kahoufig1.svg');

 

追記:Matplotlib のバージョンの関係なのか,別の JupyterHub では以下のようにちゃんと \boldsymbol{a} で太字のベクトルが表示される。ただし今度は,plt.rcParams["text.usetex"] = False なので,\overrightarrow{OA} の矢印が短い。