1""" 2================================== 3Integral as the area under a curve 4================================== 5 6Although this is a simple example, it demonstrates some important tweaks: 7 8 * A simple line plot with custom color and line width. 9 * A shaded region created using a Polygon patch. 10 * A text label with mathtext rendering. 11 * figtext calls to label the x- and y-axes. 12 * Use of axis spines to hide the top and right spines. 13 * Custom tick placement and labels. 14""" 15import numpy as np 16import matplotlib.pyplot as plt 17from matplotlib.patches import Polygon 18 19 20def func(x): 21 return (x - 3) * (x - 5) * (x - 7) + 85 22 23 24a, b = 2, 9 # integral limits 25x = np.linspace(0, 10) 26y = func(x) 27 28fig, ax = plt.subplots() 29plt.plot(x, y, 'r', linewidth=2) 30plt.ylim(ymin=0) 31 32# Make the shaded region 33ix = np.linspace(a, b) 34iy = func(ix) 35verts = [(a, 0)] + list(zip(ix, iy)) + [(b, 0)] 36poly = Polygon(verts, facecolor='0.9', edgecolor='0.5') 37ax.add_patch(poly) 38 39plt.text(0.5 * (a + b), 30, r"$\int_a^b f(x)\mathrm{d}x$", 40 horizontalalignment='center', fontsize=20) 41 42plt.figtext(0.9, 0.05, '$x$') 43plt.figtext(0.1, 0.9, '$y$') 44 45ax.spines['right'].set_visible(False) 46ax.spines['top'].set_visible(False) 47ax.xaxis.set_ticks_position('bottom') 48 49ax.set_xticks((a, b)) 50ax.set_xticklabels(('$a$', '$b$')) 51ax.set_yticks([]) 52 53plt.show() 54