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'
In [2]:
fig = plt.figure(figsize=[6.4, 5.68])
ax = fig.add_subplot(aspect='equal')
fig.tight_layout()

# 扇形 OAB
th = 40
wedge1 = patches.Wedge(center=(0, 0), r=1,
             theta1=0, theta2=th, fill=False,
             edgecolor="red", linewidth=2, zorder = 1)
ax.add_patch(wedge1)

# 二等辺三角形 OAB
xA = 1
yA = 0
xB = np.cos(np.radians(th))
yB = np.sin(np.radians(th))
plt.plot([0, xA, xB, 0], 
         [0, yA, yB, 0], c="darkgreen", lw=1.5, zorder = 2)

# 点 B から点 P への垂線
xP = xB
yP = 0
plt.plot([xB, xP], 
         [yB, yP], ls = ':',lw=0.8, c="gray", zorder = 2)

# 直角三角形 OAC
xC = xA
yC = np.tan(np.radians(th))
plt.plot([0, xA, xC, 0], 
         [0, yA, yC, 0], c="blue", linewidth=2.5, zorder = 0)
# 直角部分
h = 0.05
plt.plot([xP-h, xP-h, xP], 
         [yP, yP+h, yP+h], c="gray", lw=1, zorder = 0)
plt.plot([xA-h, xA-h, xA], 
         [yA, yA+h, yA+h], c="gray", lw=1, zorder = 0)

# 点 O
xO = 0
yO = 0
ax.text(xO-0.07, yO-0.05, r"$O$", fontsize="xx-large", c='k')

# 点 P
ax.text(xP-0.02, yP-0.07, r"$P$", fontsize="xx-large", c='k')
# 点 A
ax.text(xA-0.02, yA-0.07, r"$A$", fontsize="xx-large", c='k')
# 点 B
ax.text(xB-0.07, yB+0.02, r"$B$", fontsize="xx-large", c='k')
# 点 C
ax.text(xC-0.02, yC+0.02, r"$C$", fontsize="xx-large", c='k')

# 角度 x 部分
arc1 = patches.Arc(
        xy=(0, 0), width=0.4, height=0.4, 
        theta1=0, theta2=th, edgecolor="k", linewidth=1)
arc2 = patches.Arc(
        xy=(0, 0), width=0.44, height=0.44, 
        theta1=0, theta2=th, edgecolor="k", linewidth=1)
ax.add_patch(arc1)
ax.add_patch(arc2)
ax.text(0.23, 0.08, r"$x$", fontsize="xx-large", c='k')

# 表示範囲
ax.set_xlim(-0.1, 1.05)
ax.set_ylim(-0.1, 0.92)

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

In [3]:
fig = plt.figure(figsize=[6.4, 5.68])
ax = fig.add_subplot(aspect='equal')
fig.tight_layout()

# 扇形 OAB
th = 40
wedge1 = patches.Wedge(center=(0, 0), r=1,
             theta1=0, theta2=th, fill=False,
             edgecolor="red", linewidth=2, zorder = 1)
wedge2 = patches.Wedge(center=(0, 0), r=1,
             theta1=0, theta2=th, facecolor="red", alpha=0.1,
             zorder = 0)
ax.add_patch(wedge1)
ax.add_patch(wedge2)

# 二等辺三角形 OAB
xA = 1
yA = 0
xB = np.cos(np.radians(th))
yB = np.sin(np.radians(th))
plt.plot([0, xA, xB, 0], 
         [0, yA, yB, 0], c="darkgreen", lw=1.5, zorder = 2)
# 塗りつぶし
plt.fill([0, xA, xB, 0], 
         [0, yA, yB, 0], c="green", alpha=0.1, zorder = 0)

# 点 B から点 P への垂線
xP = xB
yP = 0
plt.plot([xB, xP], 
         [yB, yP], ls = ':',lw=0.8, c="gray", zorder = 2)

# 直角三角形 OAC
xC = xA
yC = np.tan(np.radians(th))
plt.plot([0, xA, xC, 0], 
         [0, yA, yC, 0], c="blue", linewidth=2.5, zorder = 0)
# 塗りつぶし
plt.fill([0, xA, xC, 0], 
         [0, yA, yC, 0], c="blue", alpha=0.1, zorder = 0)
# 直角部分
h = 0.05
plt.plot([xP-h, xP-h, xP], 
         [yP, yP+h, yP+h], c="gray", lw=1, zorder = 0)
plt.plot([xA-h, xA-h, xA], 
         [yA, yA+h, yA+h], c="gray", lw=1, zorder = 0)

# 点 O
xO = 0
yO = 0
ax.text(xO-0.07, yO-0.05, r"$O$", fontsize="xx-large", c='k')

# 点 P
ax.text(xP-0.02, yP-0.07, r"$P$", fontsize="xx-large", c='k')
# 点 A
ax.text(xA-0.02, yA-0.07, r"$A$", fontsize="xx-large", c='k')
# 点 B
ax.text(xB-0.07, yB+0.02, r"$B$", fontsize="xx-large", c='k')
# 点 C
ax.text(xC-0.02, yC+0.02, r"$C$", fontsize="xx-large", c='k')

# 角度 x 部分
arc1 = patches.Arc(
        xy=(0, 0), width=0.4, height=0.4, 
        theta1=0, theta2=th, edgecolor="k", linewidth=1)
arc2 = patches.Arc(
        xy=(0, 0), width=0.44, height=0.44, 
        theta1=0, theta2=th, edgecolor="k", linewidth=1)
ax.add_patch(arc1)
ax.add_patch(arc2)
ax.text(0.23, 0.08, r"$x$", fontsize="xx-large", c='k')


# 表示範囲
ax.set_xlim(-0.1, 1.05)
ax.set_ylim(-0.1, 0.92)

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

In [4]:
fig = plt.figure(figsize=[6.4, 5.68])
ax = fig.add_subplot(aspect='equal')
fig.tight_layout()

# 二等辺三角形 OAB
xA = 1
yA = 0
xB = np.cos(np.radians(th))
yB = np.sin(np.radians(th))
plt.plot([0, xA, xB, 0], 
         [0, yA, yB, 0], c="darkgreen", linewidth=2)
# 塗りつぶし
plt.fill([0, xA, xB, 0], 
         [0, yA, yB, 0], c="green", alpha=0.1, zorder = 0)

# 点 B から点 P への垂線
xP = xB
yP = 0
plt.plot([xB, xP], 
         [yB, yP], ls = ':',lw=0.8, c="gray", zorder = 1)

# 直角部分
h = 0.05
plt.plot([xP-h, xP-h, xP], 
         [yP, yP+h, yP+h], c="gray", lw=1, zorder = 0)

# 点 O
xO = 0
yO = 0
ax.text(xO-0.07, yO-0.05, r"$O$", fontsize="xx-large", c='k')

# 点 P
ax.text(xP-0.02, yP-0.07, r"$P$", fontsize="xx-large", c='k')
# 点 A
ax.text(xA-0.02, yA-0.07, r"$A$", fontsize="xx-large", c='k')
# 点 B
ax.text(xB-0.02, yB+0.02, r"$B$", fontsize="xx-large", c='k')
# 面積 S1
ax.text(0.55, 0.2, r"$S_1$", fontsize="26", c='darkgreen')


# 角度 x 部分
arc1 = patches.Arc(
        xy=(0, 0), width=0.4, height=0.4, 
        theta1=0, theta2=th, edgecolor="k", linewidth=1)
arc2 = patches.Arc(
        xy=(0, 0), width=0.44, height=0.44, 
        theta1=0, theta2=th, edgecolor="k", linewidth=1)
ax.add_patch(arc1)
ax.add_patch(arc2)
ax.text(0.23, 0.08, r"$x$", fontsize="xx-large", c='k')


# 表示範囲
ax.set_xlim(-0.1, 1.05)
ax.set_ylim(-0.1, 0.92)

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

In [5]:
fig = plt.figure(figsize=[6.4, 5.68])
ax = fig.add_subplot(aspect='equal')
fig.tight_layout()

# 扇形 OAB
th = 40
wedge1 = patches.Wedge(center=(0, 0), r=1,
             theta1=0, theta2=th, fill=False,
             edgecolor="red", linewidth=2, zorder = 1)
wedge2 = patches.Wedge(center=(0, 0), r=1,
             theta1=0, theta2=th, facecolor="red", alpha=0.1,
             zorder = 0)
ax.add_patch(wedge1)
ax.add_patch(wedge2)

# 点 O
xO = 0
yO = 0
ax.text(xO-0.07, yO-0.05, r"$O$", fontsize="xx-large", c='k')

# 点 A
ax.text(xA-0.02, yA-0.07, r"$A$", fontsize="xx-large", c='k')
# 点 B
ax.text(xB-0.07, yB+0.02, r"$B$", fontsize="xx-large", c='k')
# 面積 S
ax.text(0.55, 0.18, r"$S$", fontsize="34", c='red')

# 角度 x 部分
arc1 = patches.Arc(
        xy=(0, 0), width=0.4, height=0.4, 
        theta1=0, theta2=th, edgecolor="k", linewidth=1)
arc2 = patches.Arc(
        xy=(0, 0), width=0.44, height=0.44, 
        theta1=0, theta2=th, edgecolor="k", linewidth=1)
ax.add_patch(arc1)
ax.add_patch(arc2)
ax.text(0.23, 0.08, r"$x$", fontsize="xx-large", c='k')


# 表示範囲
ax.set_xlim(-0.1, 1.05)
ax.set_ylim(-0.1, 0.92)

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

In [6]:
fig = plt.figure(figsize=[6.4, 5.68])
ax = fig.add_subplot(aspect='equal')
fig.tight_layout()

# 直角三角形 OAC
xC = xA
yC = np.tan(np.radians(th))
plt.plot([0, xA, xC, 0], 
         [0, yA, yC, 0], c="blue", linewidth=2.5)
# 塗りつぶし
plt.fill([0, xA, xC, 0], 
         [0, yA, yC, 0], c="blue", alpha=0.1, zorder = 0)

# 直角部分
h = 0.05
plt.plot([xA-h, xA-h, xA], 
         [yA, yA+h, yA+h], c="gray", lw=1, zorder = 0)

# 点 O
xO = 0
yO = 0
ax.text(xO-0.07, yO-0.05, r"$O$", fontsize="xx-large", c='k')

# 点 A
ax.text(xA-0.02, yA-0.07, r"$A$", fontsize="xx-large", c='k')
# 点 C
ax.text(xC-0.02, yC+0.02, r"$C$", fontsize="xx-large", c='k')
# 面積 S2
ax.text(0.55, 0.2, r"$S_2$", fontsize="36", c='blue')

# 角度 x 部分
arc1 = patches.Arc(
        xy=(0, 0), width=0.4, height=0.4, 
        theta1=0, theta2=th, edgecolor="k", linewidth=1)
arc2 = patches.Arc(
        xy=(0, 0), width=0.44, height=0.44, 
        theta1=0, theta2=th, edgecolor="k", linewidth=1)
ax.add_patch(arc1)
ax.add_patch(arc2)
ax.text(0.23, 0.08, r"$x$", fontsize="xx-large", c='k')


# 表示範囲
ax.set_xlim(-0.1, 1.05)
ax.set_ylim(-0.1, 0.92)

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

In [7]:
fig = plt.figure(figsize=[6.4, 1.8])
ax = fig.add_subplot(aspect='equal')
fig.tight_layout()

# 二等辺三角形 OAB
xA = 1
yA = 0
xB = np.cos(np.radians(th))
yB = np.sin(np.radians(th))
plt.plot([0, xA, xB, 0], 
         [0, yA, yB, 0], c="darkgreen", linewidth=2)
# 塗りつぶし
plt.fill([0, xA, xB, 0], 
         [0, yA, yB, 0], c="green", alpha=0.1, zorder = 0)
# 点 B から点 P への垂線
xP = xB
yP = 0
plt.plot([xB, xP], 
         [yB, yP], ls = ':',lw=0.8, c="gray", zorder = 1)
# 直角部分
h = 0.05
plt.plot([xP-h, xP-h, xP], 
         [yP, yP+h, yP+h], c="gray", lw=1, zorder = 0)
# 面積 S1
ax.text(0.55, 0.2, r"$S_1$", fontsize="24", c='darkgreen')
# 角度 x 部分
arc1 = patches.Arc(
        xy=(0, 0), width=0.4, height=0.4, 
        theta1=0, theta2=th, edgecolor="k", linewidth=1)
arc2 = patches.Arc(
        xy=(0, 0), width=0.44, height=0.44, 
        theta1=0, theta2=th, edgecolor="k", linewidth=1)
ax.add_patch(arc1)
ax.add_patch(arc2)
ax.text(0.23, 0.05, r"$x$", fontsize="x-large", c='k')

# 扇形 OAB
th = 40
wedge1 = patches.Wedge(center=(1.2, 0), r=1,
             theta1=0, theta2=th, fill=False,
             edgecolor="red", linewidth=2, zorder = 1)
wedge2 = patches.Wedge(center=(1.2, 0), r=1,
             theta1=0, theta2=th, facecolor="red", alpha=0.1,
             zorder = 0)
ax.add_patch(wedge1)
ax.add_patch(wedge2)
# 角度 x 部分
arc1 = patches.Arc(
        xy=(1.2, 0), width=0.4, height=0.4, 
        theta1=0, theta2=th, edgecolor="k", linewidth=1)
arc2 = patches.Arc(
        xy=(1.2, 0), width=0.44, height=0.44, 
        theta1=0, theta2=th, edgecolor="k", linewidth=1)
ax.add_patch(arc1)
ax.add_patch(arc2)
ax.text(1.43, 0.05, r"$x$", fontsize="x-large", c='k')
ax.text(1.75, 0.18, r"$S$", fontsize="28", c='red')

# 直角三角形 OAC
plt.plot([2.4, xA+2.4, xC+2.4, 2.4], 
         [0, yA, yC, 0], c="blue", linewidth=2.5)
# 塗りつぶし
plt.fill([2.4, xA+2.4, xC+2.4, 2.4], 
         [0, yA, yC, 0], c="blue", alpha=0.1, zorder = 0)
ax.text(2.95, 0.2, r"$S_2$", fontsize="32", c='blue')
# 角度 x 部分
arc1 = patches.Arc(
        xy=(2.4, 0), width=0.4, height=0.4, 
        theta1=0, theta2=th, edgecolor="k", linewidth=1)
arc2 = patches.Arc(
        xy=(2.4, 0), width=0.44, height=0.44, 
        theta1=0, theta2=th, edgecolor="k", linewidth=1)
ax.add_patch(arc1)
ax.add_patch(arc2)
ax.text(2.63, 0.05, r"$x$", fontsize="x-large", c='k')
# 直角部分
h = 0.05
plt.plot([xA+2.4-h, xA+2.4-h, xA+2.4], 
         [yA, yA+h, yA+h], c="gray", lw=1, zorder = 0)

# 不等号
ax.text(1.05, 0.3, r"$<$", fontsize="xx-large", c='k')
ax.text(2.25, 0.3, r"$<$", fontsize="xx-large", c='k')

# 表示範囲
ax.set_xlim(-0.05, 3.45)
ax.set_ylim(-0.05, 0.95)

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