Matplotlib で2重積分の説明用の図を描く

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"] = False

2重積分の定義

In [2]:
def f(x, y):
    return (1+0.2*y + 0.3*np.cos(y))*(1+0.1*np.sin(x+0.7*y))
In [3]:
fig = plt.figure(figsize=[8, 6])
fig.tight_layout()
ax = fig.add_subplot(projection='3d')
fig.subplots_adjust(top=1.0, bottom=-.1,left=0)

# z = f(x, y)
x = np.linspace(0, 3, 31)
y = np.linspace(0, 3, 31)
x, y = np.meshgrid(x, y)
ax.plot_surface(x, y, f(x, y), cmap = "Blues_r", alpha = 0.6,
                ec = "lightblue", lw = 0.2, zorder=20)
ax.text(2,2,2, "$z = f(x, y)$", c='blue',
        fontsize="xx-large", ha="center",va="center")
# 領域 D の上
x0 = 1
x1 = 2.5
y0 = 0.5
y1 = 2.5
x = np.linspace(x0, x1, 5)
y = np.linspace(y0, y1, 10)
x, y = np.meshgrid(x, y)
ax.plot_surface(x, y, f(x, y)*1.01, fc = "blue", zorder=50,
                ec = "blue", lw = 1)

# 領域 D
ax.plot_surface(x, y, 0*x, fc="pink", ec="pink", 
                lw = 1, zorder=0)
plt.plot([x0,x1,x1,x0,x0], 
         [y0,y0,y1,y1,y0], 
         [0,0,0,0,0], lw=2, c="k",zorder=10)
ax.text(1.8,1.5,0, "$D$", c='red',
        fontsize="xx-large", ha="center",va="center")

# 垂直な線達
plt.plot([x0,x0], 
         [y0,y0], 
         [0,f(x0,y0)], color='k', lw = 2, zorder=2)
plt.plot([x1,x1], 
         [y0,y0], 
         [0,f(x1,y0)], color='k', lw = 2, zorder=2)
plt.plot([x1,x1], 
         [y1,y1], 
         [0,f(x1,y1)], color='k', lw = 2, zorder=3)
plt.plot([x0,x0], 
         [y1,y1], 
         [0,f(x0,y1)], color='k', lw = 2, zorder=2)
# 体積 V
ax.text(1.8,1.5,0.6, "$V$", c='k',
        fontsize="36", ha="center",va="center")

# x 軸
X = np.linspace(0, 3, 10)
plt.plot(X, [0]*10,  [0]*10, lw = 1, c = "lightgray", zorder=0)
ax.text(3.2, 0, 0, "$x$", 
        fontsize="x-large", ha="center", va="center", c='gray')
# y 軸
Y = np.linspace(0, 3, 10)
plt.plot([0]*10, Y, [0]*10, lw = 1, c = "lightgray", zorder=0)
ax.text(0, 3.1, 0, "$y$", 
        fontsize="x-large", ha="center", va="center", c='gray')
# z 軸
Z = np.linspace(0, 2.2, 10)
plt.plot([0]*10,  [0]*10, Z, lw = 1, c = "lightgray", zorder=0)
ax.text(0, 0, 2.3, "$z$", 
        fontsize="x-large", ha="center", va="center", c='gray')


ax.set_zlim(0, 2.5)
ax.view_init(elev = 10, azim = 20, roll = 0);
ax.axis(False);

累次積分

領域 $D$ が $x = a, x = b, y = y_1(x), y = y_2(x)$ で囲まれる場合

In [4]:
def y2(x):
    return 0.5*x + 0.4*np.cos(x)
def y1(x):
    return 0.1*(x-1)**2 + 0.1
In [5]:
fig = plt.figure(figsize=[6.4, 6.4])
ax = fig.add_subplot()
fig.tight_layout()

# 関数 y2(x)
x = np.linspace(0.1, 2.7)
plt.plot(x, y2(x), lw = 2, c = "blue", alpha=0.2)
a = 0.3
b = 2.3
x = np.linspace(a, b)
plt.plot(x, y2(x), lw = 2, 
         c = "blue", zorder = 10)
ax.text(3, y2(2.7)+0.03, "$y = y_2(x)$", 
        fontsize = "xx-large", c = "blue",
        ha = "center", va = "center")

# 関数 y1(x)
x = np.linspace(0.1, 2.7)
plt.plot(x, y1(x), lw = 2, c = "red", alpha=0.2)
x = np.linspace(a, b)
plt.plot(x, y1(x), lw = 2, c = "red", zorder = 10)
ax.text(3, y1(2.7)+0.03, "$y = y_1(x)$", 
        fontsize="xx-large", c = "red",
        ha = "center", va = "center", zorder = 10)

#x = a
plt.plot([a, a], [y1(a), y2(a)], 
         lw = 2, c = "k")
plt.plot([a, a], [0, y1(a)], 
         lw = 2, c = "k", linestyle="--")
ax.text(a, -0.05, "$a$", 
        fontsize = "xx-large", c = "k",
        ha = "center", va = "center")

# b
plt.plot([b, b], [0, y1(b)], 
         lw = 2, c = "k", linestyle = "--")
plt.plot([b, b], [y1(b), y2(b)], 
         lw = 2, c = "k")
ax.text(b, -0.05, "$b$", 
        fontsize = "xx-large", c = "k",
        ha = "center", va = "center")

# 領域 D
xx1 = np.linspace(a, b)
plt.fill_between(xx1, y2(xx1), y1(xx1), 
                 fc = "yellow", alpha = 0.5, zorder = 0)
ax.text(1.3, 0.4, "$D$", 
        fontsize = "36", c = "k",
        ha = "center", va = "center")

# x 軸
plt.quiver([-0.18], [0], [3.3], [0], 
           width = 0.002, headwidth = 10, headlength = 10,
           angles = 'xy', scale_units = 'xy', scale = 1)
ax.text(3.2, 0, "$x$", 
        fontsize="x-large", 
        ha = "center", va = "center")
# y 軸
plt.quiver([0], [-0.12], [0], [1.25],
           width = 0.002, headwidth = 10, headlength = 10,
           angles = 'xy', scale_units = 'xy', scale = 1)
ax.text(0, 1.2, "$y$", 
        fontsize = "x-large", 
        ha = "center", va = "center")

# 表示範囲
ax.set_xlim(-0.2, 3.3)
ax.set_ylim(-0.2, 1.3);

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

領域 $D$ が $y = c, y = d, x = x_1(y), x = x_2(y)$ で囲まれる場合

In [6]:
def x2(y):
    return 0.5*y + 0.4*np.cos(y)
def x1(y):
    return 0.1*(y-1)**2 + 0.1
In [7]:
fig = plt.figure(figsize=[6.4, 6.4])
ax = fig.add_subplot()
fig.tight_layout()

# 関数 x2(y)
y = np.linspace(0.1, 2.7)
plt.plot(x2(y), y, lw = 2, c = "blue", alpha=0.2)
c = 0.3
d = 2.3
y = np.linspace(a, b)
plt.plot(x2(y), y, lw = 2, 
         c = "blue", zorder = 10)
ax.text(x2(2.7)+0.03, 2.8, "$x = x_2(y)$", 
        fontsize = "xx-large", c = "blue",
        ha = "center", va = "center")

# 関数 x1(y)
y = np.linspace(0.1, 2.7)
plt.plot(x1(y), y, lw = 2, c = "red", alpha=0.2)
y = np.linspace(c, d)
plt.plot(x1(y), y, lw = 2, c = "red", zorder = 10)
ax.text(x1(2.7)+0.03, 2.8, "$x = x_1(y)$", 
        fontsize="xx-large", c = "red",
        ha = "center", va = "center", zorder = 10)

# y = c
plt.plot([x1(c), x2(c)], 
         [c, c], 
         lw = 2, c = "k")
plt.plot([0, x1(c)], 
         [c, c], 
         lw = 2, c = "k", linestyle="--")
ax.text(-0.05, c, "$c$", 
        fontsize = "xx-large", c = "k",
        ha = "center", va = "center")

# y = d
plt.plot([x1(d), x2(d)], 
         [d, d], 
         lw = 2, c = "k")
plt.plot([0, x1(d)], 
         [d, d], 
         lw = 2, c = "k", linestyle = "--")
ax.text(-0.05, b, "$d$", 
        fontsize = "xx-large", c = "k",
        ha = "center", va = "center")

# 領域 D
yy1 = np.linspace(c, d)
leftx = x1(yy1)
yy2 = np.linspace(d, c)
rightx = x2(yy2)
edgex = list(leftx) + list(rightx)
edgey = list(yy1) + list(yy2)
plt.fill(edgex, edgey, 
         fc = "yellow", alpha = 0.5, zorder = 0)
ax.text(0.4, 1.3, "$D$", 
        fontsize = "36", c = "k",
        ha = "center", va = "center")

# x 軸
plt.quiver(
    [-0.1], [0], [1.25], [0], 
           width = 0.002, headwidth = 10, headlength = 10,
           angles = 'xy', scale_units = 'xy', scale = 1)
ax.text(1.2, 0, "$x$", 
        fontsize="x-large", 
        ha = "center", va = "center")
# y 軸
plt.quiver([0], [-0.12], [0], [3.2],
           width = 0.002, headwidth = 10, headlength = 10,
           angles = 'xy', scale_units = 'xy', scale = 1)
ax.text(0, 3.2, "$y$", 
        fontsize = "x-large", 
        ha = "center", va = "center")

# 表示範囲
ax.set_ylim(-0.2, 3.3)
ax.set_xlim(-0.2, 1.3);

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

円の面積

In [8]:
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, fc = "yellow", alpha = 0.5)
ax.add_patch(en1)
ax.text(0, 0.12, r"$D: \, x^2 + y^2 \leq 1$", 
        fontsize="36", ha="center", va="center")

# 上円弧 
arcAP1 = patches.Arc(
        xy=(0, 0), width=2, height=2, 
        theta1=0, theta2=180, ec="blue", lw=2)
ax.add_patch(arcAP1)
ax.text(0.1, 1.05, r"$y = \sqrt{1-x^2}$", 
        fontsize="xx-large", c='blue')
# 下円弧 
arcAP1 = patches.Arc(
        xy=(0, 0), width=2, height=2, 
        theta1=180, theta2=360, edgecolor="red", linewidth=2)
ax.add_patch(arcAP1)
ax.text(0.15, -1.07, r"$y = -\sqrt{1-x^2}$", 
        fontsize="xx-large", c='red')

#
plt.scatter([-1], [0], c="k")
ax.text(-1.12, -0.08, "$-1$", fontsize="large")
plt.scatter([1], [0], c="k")
ax.text(1.03, -0.08, "$1$", fontsize="large")

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

# x軸 y軸は dashed に
ax.axhline(0, c='lightgray', ls = ':', lw=1)
ax.axvline(0, c='lightgray', ls = ':', lw=1);
ax.text(1.25,0.03, "$x$", fontsize="x-large")
ax.text(-0.05,1.25, "$y$", fontsize="x-large")
# 目盛設定
ax.axis(False);

極座標の微小面積要素

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

# r dr d\theta
x1=np.cos(np.radians(5))
x2=1.2*np.cos(np.radians(5))
y1=np.sin(np.radians(5))
y2=1.2*np.sin(np.radians(5))

plt.plot([0,x1],
         [0,-y1], c="blue", alpha=0.2, lw=2)
plt.plot([0,x1],
         [0,y1], c="blue", alpha=0.2, lw=2)

plt.fill([x1,x2,x2,x1],
         [-y1,-y2,y2,y1], 
         fc="yellow", alpha = 0.5)
plt.plot([x1,x2,x2,x1],
         [-y1,-y2,y2,y1], c="blue", lw=2)
# r
ax.text(0.5, -0.07, r"$r$", 
        fontsize="xx-large", ha="center", va="center")
# d\theta
arc1 = patches.Arc(
        xy=(0, 0), width=0.6, height=0.6, 
        theta1=-5, theta2=5, ec="blue", alpha=0.2, lw=1)
ax.add_patch(arc1)
arc2 = patches.Arc(
        xy=(0, 0), width=0.62, height=0.62, 
        theta1=-5, theta2=5, ec="blue", alpha=0.2, lw=1)
ax.add_patch(arc2)
ax.text(0.34, 0, r"$d\theta$", 
        fontsize="x-large", ha="center", va="center")
# r d\theta
plt.plot([x1, x1],
         [-y1,y1], c="red", lw=2, zorder=5)
ax.text(1.008, 0, r"$r\,d\theta$", c="red",
        fontsize="x-large", va="center")
# dr
plt.plot([x1, x2],
         [-y1,-y2], c="green", lw=2, zorder=5)
ax.text(1.1, -0.07, r"$dr$", c="green",
        fontsize="x-large", ha="center", va="center")
# P
plt.scatter([x1], [-y1], c="k",zorder=20)
ax.text(1, -0.13, r"$P(r, \theta)$", 
        fontsize="large", ha="center", va="center")
# Q
plt.scatter([x2], [y2], c="k")
ax.text(1.25, 0.14, r"$Q(r\!+\!dr, \theta\!+\!d\theta)$", 
        fontsize="large", ha="center", va="center")

# dx dy
X1=1
X2=1.2
Y1=0.4
Y2=0.6
plt.fill([X1, X2, X2, X1],
         [Y1, Y1, Y2, Y2], 
         fc="yellow", alpha = 0.5)
plt.plot([X1, X2, X2, X1],
         [Y1, Y1, Y2, Y2], c="blue", lw=2)
# P
plt.scatter([X1], [Y1], c="k",zorder=20)
ax.text(1, 0.37, r"$P(x, y)$", 
        fontsize="large", ha="center", va="center")
# Q
plt.scatter([X2], [Y2], c="k",zorder=20)
ax.text(1.25, 0.64, r"$Q(x\!+\!dx, y\!+\!dy)$", 
        fontsize="large", ha="center", va="center")
# dx
plt.plot([X1,X2],
         [Y1,Y1], c="green",lw=2,zorder=5)
ax.text(1.1, 0.43, r"$dx$", c="green",
        fontsize="x-large", ha="center", va="center")
# dy
plt.plot([X1,X1],
         [Y1,Y2], c="red",lw=2,zorder=5)
ax.text(1.008, 0.5, r"$dy$", c="red",
        fontsize="x-large", va="center")

# 表示範囲
ax.set_xlim(0, 1.5)
ax.set_ylim(-0.2, 0.8)
# 目盛設定
ax.axis(False);

平行四辺形の面積を外積で

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

x1 = 0
x2 = 1.5
x3 = x2 + np.cos(np.radians(60))
x4 = x1 + np.cos(np.radians(60))
y1 = 0
y2 = 0
y3 = np.sin(np.radians(60))
y4 = np.sin(np.radians(60))

plt.fill([x1, x2, x3, x4], [y1, y2, y3, y4], 
         fc = "yellow", alpha = 0.5)

# P
plt.scatter([x1], [y1], c="k", zorder=20)
ax.text(x1-0.1, y1-0.02, r"$P$", 
        fontsize="xx-large", ha="center", va="center")
# Q
plt.scatter([x3], [y3], c="k",zorder=20)
ax.text(x3+0.1, y3+0.02, r"$Q$", 
        fontsize="xx-large", ha="center", va="center")

# \theta
arc1 = patches.Arc(
        xy=(x1, y1), width=0.4, height=0.4, 
        theta1=0, theta2=60, ec="green", linewidth=1)
ax.add_patch(arc1)
arc2 = patches.Arc(
        xy=(x1, y1), width=0.44, height=0.44, 
        theta1=0, theta2=60, ec="green", linewidth=1)
ax.add_patch(arc2)
ax.text(0.25, 0.16, r"$\theta$", c="green",
        fontsize="26", ha="center", va="center")
# S
ax.text(1, 0.4, r"$S$", c="k",
        fontsize="36", ha="center", va="center")

# vec a
plt.quiver([x1], [y1], [x2], [0], color="blue", lw = 2,
          angles='xy', scale_units='xy', scale=1
          )
plt.quiver([x4], [y4], [x2], [0], color="blue", lw = 2,
          angles='xy', scale_units='xy', scale=1
          )
ax.text(0.7, -0.15, r"$\vec{a}$", c="blue",
        fontsize="36", ha="center", va="center")
# 新弘大 JupyterHub では \boldsymbol{} が使える
# ax.text(0.7, -0.15, r"$\boldsymbol{a}$", c="blue",
#         fontsize="36", ha="center", va="center")

# vec b
plt.quiver([x1], [y1], [x4], [y4], color="red", lw = 2,
          angles='xy', scale_units='xy', scale=1
          )
plt.quiver([x2], [y2], [x3-x2], [y3-y2], color="red", lw = 2,
          angles='xy', scale_units='xy', scale=1
          )
ax.text(0.15, 0.5, r"$\vec{b}$", c="red",
        fontsize="36", ha="center", va="center")
# 新弘大 JupyterHub では \boldsymbol{} が使える
# ax.text(0.15, 0.5, r"$\boldsymbol{b}$", c="red",
#         fontsize="36", ha="center", va="center")

# 表示範囲
ax.set_xlim(-0.5, 2.5)
ax.set_ylim(-0.4, 1.1)
# 目盛設定
ax.axis(False);

楕円の面積

In [11]:
def f(x):
    return b*(np.sqrt(1-x**2/a**2))
In [12]:
plt.rcParams["text.usetex"] = True
In [13]:
a = 2
b = 1
alim = a+0.5
blim = b+0.5

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

# 楕円
e1 = patches.Ellipse((0, 0), 2*a, 2*b, 
                     # 塗りつぶしの色,  線の色
                     facecolor="yellow", alpha=0.5, 
                     linewidth=2, fill=True)
ax.add_patch(e1)
ax.text(0.02, 0.25, 
        r"$D: \, \frac{x^2}{a^2} + \frac{y^2}{b^2} \leq 1$", 
        fontsize="28", ha="center", va="center")

x = np.linspace(-a, a, 500)
# 上
plt.plot(x, f(x), lw =2, c="blue")
ax.text(0.3, 1.1, r"$y = b\,\sqrt{1 -\frac{x^2}{a^2}}$", 
        fontsize="xx-large", c="blue")
# 下
plt.plot(x,-f(x), lw =2, c="red")
ax.text(0.3, -1.2, r"$y = -b\,\sqrt{1 -\frac{x^2}{a^2}}$", 
        fontsize="xx-large", c="red")
# x = -a
plt.scatter([-a], [0], c="k", zorder=20)
ax.text(-a-0.25, -0.1, "$-a$", fontsize="large")
# x = a
plt.scatter([a], [0], c="k", zorder=20)
ax.text(a+0.05, -0.1, "$a$", fontsize="large")

#

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

# x軸 y軸は dashed に
ax.axhline(0, c='lightgray', ls = ':', lw=1)
ax.axvline(0, c='lightgray', ls = ':', lw=1);
ax.text(2.3,0.03, "$x$", fontsize="x-large")
ax.text(-0.1,1.4, "$y$", fontsize="x-large")

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

ガウス積分

In [14]:
plt.rcParams["text.usetex"] = False
In [15]:
fig = plt.figure(figsize=[6.4, 3])
ax = fig.add_subplot()
fig.tight_layout()

x = np.linspace(-3, 3, 300)
plt.plot(x, np.exp(-x**2), lw=2, c="blue")
plt.fill(x, np.exp(-x**2), 0, fc="yellow", alpha=0.5)
ax.text(0.8, 0.6, r"$y = e^{-x^2}$", 
        fontsize="xx-large",c="blue")
# 表示範囲
ax.set_xlim(-3, 3)
ax.set_ylim(-0.02, 1.1)

# x軸 y軸は dashed に
ax.axhline(0, c='gray', ls = ':', lw=1)
ax.axvline(0, c='gray', ls = ':', lw=1);
ax.text(2.8, 0.03, "$x$", fontsize="x-large")
ax.text(0.03, 1.05, "$y$", fontsize="x-large")

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