matplotlib學習之函數積分圖 ...
# coding:utf-8 import numpy as np from matplotlib import pyplot as plt from matplotlib.patches import Polygon ''' 求函數積分 ''' def func(x): return -(x - 2) * (x - 8) + 40 print(plt.style.available) plt.style.use("ggplot") # 繪製曲線 x = np.linspace(0, 10) y = func(x) fig, ax = plt.subplots() plt.plot(x, y, linewidth=2) # 設置坐標軸 a = 2 b = 9 ax.set_xticks([a, b]) # 設置橫坐標2,9 ax.set_yticks([]) # 設置縱坐標為空 ax.set_xticklabels(['$a$', '$b$']) # 設置橫坐標顯示為a,b plt.figtext(0.91, 0.09, '$x$') # 設置坐標軸字母 plt.figtext(0.11, 0.88, '$y$') # 確定多邊形 ix = np.linspace(a, b) iy = func(ix) ixy = zip(ix, iy) verts = [(a, 0)] + list(ixy) + [(b, 0)] poly = Polygon(verts, facecolor='0.9', edgecolor='0.5') ax.add_patch(poly) # 添加數學公式 x_math = (a + b) * 0.5 y_math = 35 plt.text(x_math, y_math, r'$\int_a^b(-(x - 2) * (x - 8) + 40)$', fontsize=13, horizontalalignment='center') plt.title('functional integration') plt.show()