積分作為曲線下的面積#

雖然這是一個簡單的範例,但它示範了一些重要的調整

  • 具有自訂顏色和線寬的簡單折線圖。

  • 使用多邊形修補程式建立的陰影區域。

  • 具有數學文字渲染的文字標籤。

  • figtext 呼叫以標記 x 軸和 y 軸。

  • 使用軸線隱藏頂部和右側軸線。

  • 自訂刻度位置和標籤。

integral
import matplotlib.pyplot as plt
import numpy as np

from matplotlib.patches import Polygon


def func(x):
    return (x - 3) * (x - 5) * (x - 7) + 85


a, b = 2, 9  # integral limits
x = np.linspace(0, 10)
y = func(x)

fig, ax = plt.subplots()
ax.plot(x, y, 'r', linewidth=2)
ax.set_ylim(bottom=0)

# Make the shaded region
ix = np.linspace(a, b)
iy = func(ix)
verts = [(a, 0), *zip(ix, iy), (b, 0)]
poly = Polygon(verts, facecolor='0.9', edgecolor='0.5')
ax.add_patch(poly)

ax.text(0.5 * (a + b), 30, r"$\int_a^b f(x)\mathrm{d}x$",
        horizontalalignment='center', fontsize=20)

fig.text(0.9, 0.05, '$x$')
fig.text(0.1, 0.9, '$y$')

ax.spines[['top', 'right']].set_visible(False)
ax.set_xticks([a, b], labels=['$a$', '$b$'])
ax.set_yticks([])

plt.show()

Sphinx-Gallery 產生的圖庫