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'
# plt.rcParams["text.usetex"] = False

定積分と面積

In [2]:
def f(x):
    return 0.5*x + 0.4*np.cos(x)
In [3]:
fig = plt.figure(figsize=[6.4, 4.8])
ax = fig.add_subplot()
fig.tight_layout()

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

#x = a
plt.plot([a, a], 
         [0, f(a)], lw = 2, c = "blue")
ax.text(a, -0.05, "$a$", fontsize="x-large", c = "blue",
        ha="center", va="center")

# x
x1 = 2
plt.plot([x1, x1], 
         [0, f(x1)], lw = 2, c = "blue")
ax.text(x1, -0.05, "$x$", fontsize="x-large", c = "blue",
        ha="center", va="center")
ax.text(1.87, 0.5, "$f(x)$", fontsize="x-large", c = "blue",
        ha="center", va="center")

# x + Δx
x2 = 2.3
plt.plot([x2, x2], 
         [0, f(x2)], lw = 2, c = "red")
ax.text(x2+0.07, -0.05, r"$x\!\!+\!\!\Delta x$", 
        fontsize="x-large", c = "red",
        ha="center", va="center")
plt.plot([x1, x2], 
         [f(x1), f(x1)], lw = 1.5, c = "blue", 
         linestyle="--",zorder=0)
plt.plot([x1, x1, x2], 
         [f(x1), f(x2), f(x2)], lw = 1.5, c = "red", 
         linestyle="--",zorder=0)
ax.text(2.33, 0.5, r"$f(x\!\!+\!\!\Delta x)$", 
        fontsize="x-large", c = "red",
        va="center")

ax.text(2.15, 0.05, r"$\Delta x$", 
        fontsize="x-large", c = "k",
        ha="center", va="center")
plt.plot([x1, x2], [0, 0], lw = 2, c = "k")

# 面積 S(x)
xx1 = np.linspace(a, x1)
plt.fill_between(xx1, f(xx1), 0, 
                 fc = "yellow", alpha = 0.3, zorder = 0)
ax.text(1.25, 0.4, r"$S(x)$", fontsize="xx-large", c = "k",
        ha="center", va="center")

# 面積 ΔS
xx2 = np.linspace(x1, x2)
plt.fill_between(xx2, f(xx2), 0, 
                 fc="gray", alpha = 0.3, zorder = 0)
ax.text(2.15, 0.4, r"$\Delta\! S$", 
        fontsize="xx-large", c = "k",
        ha="center", va="center")

# x 軸
plt.quiver([-0.18],[0],[3.3],[0], 
           width=0.002, headwidth=10, headlength=10,
           color="black", 
           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,
           color="black", 
           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);

$y=f(x)$ と $x$ 軸で囲まれた部分の面積

In [4]:
fig = plt.figure(figsize=[6.4, 4.8])
ax = fig.add_subplot()
fig.tight_layout()

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

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

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

# 面積 S
xx1 = np.linspace(a, b)
plt.fill_between(xx1, f(xx1), 0, 
                 fc = "yellow", alpha = 0.3, zorder = 0)
ax.text(1.3, 0.35, r"$S$", 
        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);

$y=f(x)$ と $y=g(x)$ で囲まれた部分の面積

In [5]:
def g(x):
    return 0.1*(x-1)**2 + 0.1
In [6]:
fig = plt.figure(figsize=[6.4, 4.8])
ax = fig.add_subplot()
fig.tight_layout()

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

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

#x = a
plt.plot([a, a], [g(a), f(a)], 
         lw = 2, c = "k")
plt.plot([a, a], [0, g(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, g(b)], 
         lw = 2, c = "k", linestyle = "--")
plt.plot([b, b], [g(b), f(b)], 
         lw = 2, c = "k")
ax.text(b, -0.05, "$b$", 
        fontsize = "xx-large", c = "k",
        ha = "center", va = "center")

# 面積 S
xx1 = np.linspace(a, b)
plt.fill_between(xx1, f(xx1), g(xx1), 
                 fc = "yellow", alpha = 0.3, zorder = 0)
ax.text(1.3, 0.4, "$S$", 
        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);

曲線の長さ

In [7]:
fig = plt.figure(figsize=[6.4, 4.8])
ax = fig.add_subplot()
fig.tight_layout()

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

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

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

# dx
X1 = 0.5
X2 = 0.7
plt.plot([X1, X2], [f(X1), f(X1)], lw = 2, c = "gray")
ax.text(0.6, 0.55, "$dx$",
        fontsize = "xx-large", 
        ha = "center", va = "center")
# dy
plt.plot([X2, X2], [f(X1), f(X2)], lw = 2, c = "gray")
ax.text(0.75, 0.63, "$dy$",
        fontsize="xx-large", 
        va="center")
# dl
xdx = np.linspace(X1, X2)
plt.plot(xdx, f(xdx), lw = 2, c = "red", zorder = 20)
ax.text(0.5, 0.7, "$d\ell$",
        fontsize = "xx-large", c = "red", 
        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);

回転体の表面積

In [8]:
fig = plt.figure(figsize=[6.4, 4.8])
ax = fig.add_subplot()
fig.tight_layout()

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

# 関数 y = -f(x)
x = np.linspace(0.1, 2.7)
plt.plot(x, -f(x), lw = 2, c = "blue", alpha=0.2)
x = np.linspace(a, b)
plt.plot(x, -f(x), lw = 2, c = "blue", zorder = 10)

# x = a の上底面
plt.plot([a, a], [0, f(a)], 
         lw = 2, c = "k", ls = "--")
ax.text(a, -0.06, "$a$", 
        fontsize = "xx-large", c = "k",
        ha = "center", va = "center")
ea = patches.Ellipse((a, 0), 2*f(a)*0.3, 2*f(a), 
                     ec = "blue",
                     lw = 2, fill = False)
ax.add_patch(ea)

# x = b の下底面
plt.plot([b, b], [0, f(b)], 
         lw = 2, c = "k", ls = "--")
ax.text(b, -0.08, "$b$", 
        fontsize = "xx-large", c = "k",
        ha = "center", va = "center")
arcbf = patches.Arc(
        xy = (b, 0), width = 2*f(b)*0.3, height = 2*f(b), 
        theta1 = -90, theta2 = 90, ec = "blue", lw = 2)
ax.add_patch(arcbf)
arcbb = patches.Arc(
        xy = (b, 0), width = 2*f(b)*0.3, height = 2*f(b), 
        theta1 = 90, theta2 = 270, ec="blue", lw = 1, ls="--")
ax.add_patch(arcbb)

# 輪切り部分
X1 = 0.7
X2 = 0.8
# dl
xdx = np.linspace(X1, X2)
plt.plot(xdx, f(xdx), lw = 2, c = "red", zorder = 20)
ax.text(0.65, 0.75, "$d\ell$",
        fontsize = "xx-large", c = "red", 
        va="center")
arcX1f = patches.Arc(
        xy = (X1, 0), width = 2*f(X1)*0.3, height = 2*f(X1), 
        theta1 = -90, theta2 = 90, ec = "red", lw = 2)
ax.add_patch(arcX1f)
arcX1b = patches.Arc(
        xy = (X1, 0), width = 2*f(X1)*0.3, height = 2*f(X1), 
        theta1 = 90, theta2 = 270, ec = "red", lw = 1, ls = "--")
ax.add_patch(arcX1b)
arcX2f = patches.Arc(
        xy = (X2, 0), width = 2*f(X2)*0.3, height = 2*f(X2), 
        theta1 = -90, theta2 = 90, ec = "red", lw = 2)
ax.add_patch(arcX2f)
arcX2b = patches.Arc(
        xy = (X2, 0), width = 2*f(X2)*0.3, height = 2*f(X2), 
        theta1 = 90, theta2 = 270, ec = "red", lw = 1, ls = "--")
ax.add_patch(arcX2b)

# 塗りつぶし
th = np.linspace(-90, 90)
edgex1 = X1 + f(X1)*0.3*np.cos(np.radians(th))
edgey1 = f(X1)*np.sin(np.radians(th))
th = np.linspace(90,-90)
edgex2 = X2 + f(X2)*0.3*np.cos(np.radians(th))
edgey2 = f(X2)*np.sin(np.radians(th))

edgex = np.append(edgex1, edgex2)
edgey = np.append(edgey1, edgey2)
plt.fill(edgex, edgey, facecolor="red", alpha=0.1)

th = np.linspace(90, 270)
edgex1 = X1 + f(X1)*0.3*np.cos(np.radians(th))
edgey1 = f(X1)*np.sin(np.radians(th))
th = np.linspace(270,90)
edgex2 = X2 + f(X2)*0.3*np.cos(np.radians(th))
edgey2 = f(X2)*np.sin(np.radians(th))

edgex = np.append(edgex1, edgex2)
edgey = np.append(edgey1, edgey2)
plt.fill(edgex, edgey, facecolor="red", alpha=0.4)

plt.plot([X1, X1], [0, f(X1)], lw = 1, ls = ":", c = "k")
ax.text(X1, -0.05, "$x$", 
        fontsize = "x-large", 
        ha = "center", va = "center")
plt.plot([0, X1], [f(X1), f(X1)], lw = 1, ls = ":", c = "k")
ax.text(-0.05, f(X1), "$y$", 
        fontsize = "x-large", 
        ha = "center", va = "center")

ax.text(1, 0.25, "$L = 2 \pi y$", 
        c="red", fontsize="x-large")

# 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], [-1.3], [0], [2.43],
           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(-1.3, 1.3);

# 目盛設定
ax.axis(False);
In [9]:
fig = plt.figure(figsize=[8, 6])
fig.tight_layout()
ax = fig.add_subplot(projection='3d')
ax.set_box_aspect((8, 5, 5))
fig.subplots_adjust(top=1.0, bottom=-.1)

a = 0.3
b = 2.3
X1 = 0.7
X2 = 0.8

# 「上」側面
xi = np.arange(a, X1+0.01, 0.1)
th = np.linspace(0, 2*np.pi, 120)
xi, th = np.meshgrid(xi, th)
x = xi
y = f(xi)*np.cos(th)
z = f(xi)*np.sin(th)
ax.plot_surface(x, y, z, cmap="Blues_r", alpha=0.8,
                edgecolor="blue", lw=0.2)
# 輪切り部分
xi = np.arange(X1, X2+0.01, 0.1)
th = np.linspace(0, 2*np.pi, 120)
xi, th = np.meshgrid(xi, th)
x = xi
y = f(xi)*np.cos(th)
z = f(xi)*np.sin(th)
ax.plot_surface(x, y, z, cmap="Reds_r",
                edgecolor="red", lw=0.2)
# 「下」側面
xi = np.arange(X2, b+0.01, 0.1)
th = np.linspace(0, 2*np.pi, 120)
xi, th = np.meshgrid(xi, th)
x = xi
y = f(xi)*np.cos(th)
z = f(xi)*np.sin(th)
ax.plot_surface(x, y, z, cmap="Blues_r", 
                edgecolor="blue", lw=0.2)

# 長軸
X = np.linspace(0, 0.44, 10)
ax.plot(X, [0]*10, [0]*10, lw=2, c="k", zorder=5)
X = np.linspace(1, 2.8, 10)
ax.plot(X, [0]*10, [0]*10, lw=2, c="k", zorder=0)

ax.set_xlim(0, 2.8)
ax.view_init(elev=0, azim=-110, roll=0);
ax.axis(False);

回転体の体積

In [10]:
# 塗りつぶし用の plt.fill() に渡すため
# 中心 (X0, 0) の楕円の x, y 座標を角度(°)を媒介変数として
def ex(X0, deg):
    return X0 + 0.3*f(X0)*np.cos(np.radians(deg))
def ey(X0, deg):
    return f(X0)*np.sin(np.radians(deg))
In [11]:
fig = plt.figure(figsize=[6.4, 4.8])
ax = fig.add_subplot()
fig.tight_layout()

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

# 関数 y = -f(x)
x = np.linspace(0.1, 2.7)
plt.plot(x, -f(x), lw = 2, c = "blue", alpha=0.2)
x = np.linspace(a, b)
plt.plot(x, -f(x), lw = 2, c = "blue", zorder = 10)

# x = a の上底面
plt.plot([a, a], [0, f(a)], 
         lw = 2, c = "k", ls = "--")
ax.text(a, -0.06, "$a$", 
        fontsize = "xx-large", 
        ha = "center", va = "center")

ea = patches.Ellipse((a, 0), 2*f(a)*0.3, 2*f(a), 
                     ec = "blue", fc = "yellow",  
                     lw = 2, fill=True)
ax.add_patch(ea)

# x = b の下底面
plt.plot([b, b], [0, f(b)], 
         lw = 2, c = "k", ls = "--")
ax.text(b, -0.08, "$b$", 
        fontsize = "xx-large", c = "k",
        ha = "center", va = "center")
arcbf = patches.Arc(
        xy = (b, 0), width = 2*f(b)*0.3, height = 2*f(b), 
        theta1 = -90, theta2 = 90, ec = "blue", lw = 2, zorder = 10)
ax.add_patch(arcbf)
arcbb = patches.Arc(
        xy = (b, 0), width = 2*f(b)*0.3, height = 2*f(b), 
        theta1 = 90, theta2 = 270, ec = "blue", 
        lw = 1, ls = "--", alpha = 0.5)
ax.add_patch(arcbb)
eb = patches.Ellipse((b, 0), 2*f(b)*0.3, 2*f(b), 
                     fc = "yellow",  
                     lw = 2, fill=True, zorder=1)
ax.add_patch(eb)

# 輪切り部分
X1 = 0.7
X2 = 0.8
## edge 部分
arcX1f = patches.Arc(
        xy = (X1, 0), width = 2*f(X1)*0.3, height = 2*f(X1), 
        theta1 = -90, theta2 = 90, ec = "red", lw = 2)
ax.add_patch(arcX1f)
arcX1b = patches.Arc(
        xy = (X1, 0), width = 2*f(X1)*0.3, height = 2*f(X1), 
        theta1 = 90, theta2 = 270, ec = "red", lw = 1, ls = "--")
ax.add_patch(arcX1b)
arcX2f = patches.Arc(
        xy = (X2, 0), width = 2*f(X2)*0.3, height = 2*f(X2), 
        theta1 = -90, theta2 = 90, ec = "red", lw = 2)
ax.add_patch(arcX2f)
arcX2b = patches.Arc(
        xy = (X2, 0), width = 2*f(X2)*0.3, height = 2*f(X2), 
        theta1 = 90, theta2 = 270, ec = "red", lw = 1, ls = "--")
ax.add_patch(arcX2b)
## 塗りつぶし部分
upx = np.linspace(X1, X2)
rightx = ex(X2, np.linspace(90, -90))
downx = np.linspace(X2, X1)
leftx = ex(X1, np.linspace(-90, 90))

upy = f(upx)
righty = ey(X2, np.linspace(90, -90))
downy = -f(downx)
lefty = ey(X1, np.linspace(-90, 90))

X = list(upx)+list(rightx)+list(downx)+list(leftx)
Y = list(upy)+list(righty)+list(downy)+list(lefty)
plt.fill(X, Y, fc = "red", alpha = 0.2)

e = patches.Ellipse((X1, 0), 2*f(X1)*0.3, 2*f(X1), 
                     fc = "red", alpha = 0.2,
                     lw = 2, fill=True, zorder=1)
ax.add_patch(e)

# 上側面の塗りつぶし
upx = np.linspace(a, X1)
rightx = ex(X1, np.linspace(90, -90))
downx = np.linspace(X1, a)
leftx = ex(a, np.linspace(-90, 90))

upy = f(upx)
righty = ey(X1, np.linspace(90, -90))
downy = -f(downx)
lefty = ey(a, np.linspace(-90, 90))

X = list(upx)+list(rightx)+list(downx)+list(leftx)
Y = list(upy)+list(righty)+list(downy)+list(lefty)
plt.fill(X, Y, fc = "yellow", alpha = 0.3, zorder=0)

# 下側面の塗りつぶし
upx = np.linspace(X2, b)
rightx = ex(b, np.linspace(90, -90))
downx = np.linspace(b, X2)
leftx = ex(X2, np.linspace(-90, 90))

upy = f(upx)
righty = ey(b, np.linspace(90, -90))
downy = -f(downx)
lefty = ey(X2, np.linspace(-90, 90))

X = list(upx)+list(rightx)+list(downx)+list(leftx)
Y = list(upy)+list(righty)+list(downy)+list(lefty)
plt.fill(X, Y, fc = "yellow", alpha = 0.3, zorder=0)

# その他の注釈
plt.plot([X1, X1], [0, f(X1)], lw = 1, ls = ":", c="k")
ax.text(X1, -0.05, "$x$", 
        fontsize = "x-large", 
        ha = "center", va = "center")
plt.plot([0, X1], [f(X1), f(X1)], lw = 1, ls = ":", c = "k")
ax.text(-0.05, f(X1), "$y$", 
        fontsize = "x-large", 
        ha = "center", va = "center")
plt.plot([X2, X2], [0, f(X2)], lw = 1, ls = ":", c = "k")
ax.text(0.75, 0.75, "$d\!x$", 
        fontsize = "large", 
        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], [-1.3], [0], [2.43],
           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(-1.3, 1.3);

# 目盛設定
ax.axis(False);
In [12]:
fig = plt.figure(figsize=[8, 6])
fig.tight_layout()
ax = fig.add_subplot(projection='3d')
ax.set_box_aspect((8, 5, 5))
fig.subplots_adjust(top=1.0, bottom=-.1)

a = 0.3
b = 2.3
X1 = 0.7
X2 = 0.8

# 「上」側面
xi = np.arange(a, X1+0.01, 0.1)
th = np.linspace(0, 2*np.pi, 120)
xi, th = np.meshgrid(xi, th)
x = xi
y = f(xi)*np.cos(th)
z = f(xi)*np.sin(th)
ax.plot_surface(x, y, z, cmap = "Blues_r", alpha = 0.7,
                ec = "blue", lw = 0.2)
# 輪切り部分
xi = np.arange(X1, X2+0.01, 0.1)
th = np.linspace(0, 2*np.pi, 120)
xi, th = np.meshgrid(xi, th)
x = xi
y = f(xi)*np.cos(th)
z = f(xi)*np.sin(th)
ax.plot_surface(x, y, z, cmap = "Reds_r",
                ec = "red", lw = 0.2)
# 輪切り底面
ri = np.linspace(0, f(X1), 5)
th = np.linspace(0, 2*np.pi, 120)
xi, th = np.meshgrid(ri, th)
x = X1
y = ri*np.cos(th)
z = ri*np.sin(th)
ax.plot_surface(x, y, z, fc = "red", 
                ec = "red", lw = 0.2)
# 「下」側面
xi = np.arange(X2, b+0.01, 0.1)
th = np.linspace(0, 2*np.pi, 120)
xi, th = np.meshgrid(xi, th)
x = xi
y = f(xi)*np.cos(th)
z = f(xi)*np.sin(th)
ax.plot_surface(x, y, z, cmap = "Blues_r", 
                ec = "blue", lw = 0.2)
# 「上」底面
ri = np.linspace(0, f(a), 5)
th = np.linspace(0, 2*np.pi, 120)
ri, th = np.meshgrid(ri, th)
x = a
y = ri*np.cos(th)
z = ri*np.sin(th)
ax.plot_surface(x, y, z, 
                fc = "lightblue", ec = "lightblue", alpha=0.9,
                lw=0.2, zorder=10)

# 長軸
X = np.linspace(0, a, 10)
ax.plot(X, [0]*10, [0]*10, lw = 2, c = "k", zorder=15)
X = np.linspace(1, 2.8, 10)
ax.plot(X, [0]*10, [0]*10, lw = 2, c = "k", zorder=0)

ax.set_xlim(0, 2.8)
ax.view_init(elev = 0, azim = -110, roll = 0);
ax.axis(False);