Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit edce62b

Browse files
committed
Cleanup integral_demo
1 parent 800deb4 commit edce62b

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

examples/showcase/integral_demo.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,51 @@
11
"""
22
Plot demonstrating the integral as the area under a curve.
3+
4+
Although this is a simple example, it demonstrates some important tweaks:
5+
6+
* A simple line plot with custom color and line width.
7+
* A shaded region created using a Polygon patch.
8+
* A text label with mathtext rendering.
9+
* figtext calls to label the x- and y-axes.
10+
* Use of axis spines to hide the top and right spines.
11+
* Custom tick placement and labels.
312
"""
413
import numpy as np
514
import matplotlib.pyplot as plt
615
from matplotlib.patches import Polygon
716

17+
818
def func(x):
9-
return (x-3)*(x-5)*(x-7)+85
19+
return (x - 3) * (x - 5) * (x - 7) + 85
1020

11-
fig, ax = plt.subplots()
1221

13-
a, b = 2, 9 # integral area
22+
a, b = 2, 9 # integral limits
1423
x = np.linspace(0, 10)
1524
y = func(x)
25+
26+
fig, ax = plt.subplots()
1627
plt.plot(x, y, 'r', linewidth=2)
28+
plt.ylim(ymin=0)
1729

18-
# make the shaded region
30+
# Make the shaded region
1931
ix = np.linspace(a, b)
2032
iy = func(ix)
21-
verts = [(a,0)] + list(zip(ix,iy)) + [(b,0)]
33+
verts = [(a, 0)] + list(zip(ix, iy)) + [(b, 0)]
2234
poly = Polygon(verts, facecolor='0.9', edgecolor='0.5')
2335
ax.add_patch(poly)
2436

25-
plt.text(0.5 * (a + b), 30,
26-
r"$\int_a^b f(x)\mathrm{d}x$", horizontalalignment='center',
27-
fontsize=20)
37+
plt.text(0.5 * (a + b), 30, r"$\int_a^b f(x)\mathrm{d}x$",
38+
horizontalalignment='center', fontsize=20)
2839

29-
plt.axis([0,10, 0, 180])
3040
plt.figtext(0.9, 0.05, '$x$')
3141
plt.figtext(0.1, 0.9, '$y$')
3242

3343
ax.spines['right'].set_visible(False)
3444
ax.spines['top'].set_visible(False)
35-
ax.set_xticks((a,b))
36-
ax.set_xticklabels(('$a$','$b$'))
45+
ax.xaxis.set_ticks_position('bottom')
46+
47+
ax.set_xticks((a, b))
48+
ax.set_xticklabels(('$a$', '$b$'))
3749
ax.set_yticks([])
3850

3951
plt.show()

0 commit comments

Comments
 (0)