Matplotlib で円と直角三角形で三角関数の定義の図を描く

三角関数の定義の図を Matplotlib だけで描く。以下のページなどで使うので。

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'
# 本格的に full latex したいときには True に。ただし時間がかかる...
plt.rcParams["text.usetex"] = False
In [2]:
fig = plt.figure(figsize=[6.4, 6.4])
ax = fig.add_subplot(aspect='equal')
fig.tight_layout()

# 原点を中心とした半径 1 の円
en1 = patches.Circle(
        xy=(0, 0), 
        radius = 1, 
        fill=False, edgecolor="purple", linewidth=1)
ax.add_patch(en1)

# 円上の点 P の座標
x1 = np.cos(np.radians(60))
y1 = np.sin(np.radians(60))
# 点 P
plt.scatter([x1], [y1], zorder=10, c="purple")
ax.text(x1+0.02, y1+0.02, r"$P\,(x, y)$", 
        fontsize="xx-large", c='purple')

# 直角三角形の3辺
plt.plot([0, x1], 
         [0, y1], c="k", linewidth=2)
plt.plot([0, x1], 
         [0, 0], c="blue", linewidth=2)
plt.plot([x1, x1],
         [0, y1], c="red", linewidth=2)
ax.text(-0.08, -0.08, r"$O$", fontsize="xx-large", c='k')
ax.text(0.2, 0.48, r"$r$", fontsize="xx-large", c='k')
ax.text(0.24, -0.08, r"$x$", fontsize="xx-large", c='blue')
ax.text(0.53, 0.38, r"$y$", fontsize="xx-large", c='red')

# 点 P から y 軸への平行な線
# plt.plot([0, x1],
#          [y1, y1], ls = ':',lw=0.8, c="gray")

# 角度 θ 部分
arc1 = patches.Arc(
        xy=(0, 0), width=0.18, height=0.18, 
        theta1=0, theta2=60, edgecolor="green", linewidth=1)
arc2 = patches.Arc(
        xy=(0, 0), width=0.2, height=0.2, 
        theta1=0, theta2=60, edgecolor="green", linewidth=1)
ax.add_patch(arc1)
ax.add_patch(arc2)
ax.text(0.1, 0.05, r"$\theta$", fontsize="xx-large", c='green')

# 直角部分
plt.plot([0.45,0.45,0.5],
         [0,0.05,0.05], c='k', zorder=0)

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

# 目盛設定
ax.axis(False);

# x軸 y軸は dashed に
ax.axhline(0, c='k', ls = ':', lw=1)
ax.axvline(0, c='k', ls = ':', lw=1);
$$
\cos{\color{green}{\theta}} = \frac{\color{blue}{x}}{r}, \quad
\sin{\color{green}{\theta}} = \frac{\color{red}{y}}{r}, \quad
\tan{\color{green}{\theta}} = \frac{\color{red}{y}}{\color{blue}{x}} = \frac{\sin{\color{green}{\theta}}}{\cos{\color{green}{\theta}}}
$$
In [3]:
plt.rcParams["text.usetex"] = True
In [4]:
fig = plt.figure(figsize=[6.4, 4])
ax = fig.add_subplot(aspect='equal')
fig.tight_layout()

# 円上の点 P の座標
y1 = np.cos(np.radians(60))
x1 = np.sin(np.radians(60))

# 直角三角形の3辺
plt.plot([0, x1], 
         [y1, 0], c="k", linewidth=2)
plt.plot([0, 0], 
         [y1, 0], c="blue", linewidth=2)
plt.plot([0, x1],
         [0, 0], c="red", linewidth=2)
ax.text(0.42, 0.3, r"$r$", fontsize="xx-large", c='k')
ax.text(-0.05, 0.25, r"$x$", fontsize="xx-large", c='blue')
ax.text(0.39, -0.05, r"$y$", fontsize="xx-large", c='red')

# 角度 θ 部分
arc1 = patches.Arc(
        xy=(0, y1), width=0.16, height=0.16, 
        theta1=270, theta2=330, edgecolor="green", linewidth=1)
arc2 = patches.Arc(
        xy=(0, y1), width=0.18, height=0.18, 
        theta1=270, theta2=330, edgecolor="green", linewidth=1)
ax.add_patch(arc1)
ax.add_patch(arc2)
ax.text(0.05, 0.38, r"$\theta$", fontsize="xx-large", c='green')

# 角度 π/2 - θ 部分
arc1 = patches.Arc(
        xy=(x1, 0), width=0.25, height=0.25, 
        theta1=150, theta2=180, edgecolor="purple", linewidth=1)
arc2 = patches.Arc(
        xy=(x1, 0), width=0.27, height=0.27, 
        theta1=150, theta2=180, edgecolor="purple", linewidth=1)
ax.add_patch(arc1)
ax.add_patch(arc2)
# plt.rcParams["text.usetex"] = True が必要
ax.text(0.6, 0.04, r"$\displaystyle\frac{\pi}{2} -\theta$", 
        fontsize="xx-large", c='purple')

# 直角部分
plt.plot([0, 0.045, 0.045], 
         [0.045,0.045,0],
         c='k', zorder=0)

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

# 目盛設定
ax.axis(False);
$$\cos\left( {\color{purple}{\frac{\pi}{2}  -\theta}}\right) = \frac{\color{red}{y}}{r} = \sin {\color{green}{\theta}}, \quad \sin\left( {\color{purple}{\frac{\pi}{2}  -\theta}}\right)  = \frac{\color{blue}{x}}{r} = \cos {\color{green}{\theta}}$$