Demo entry 1462817

sin

   

Submitted by adrian on Mar 04, 2015 at 19:13
Language: Python. Code size: 1.0 kB.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon

a, b = 0, 2 * (np.pi)
x = np.linspace(0, 4 * np.pi, 1000)
y = abs(np.sin(x))

fig, ax = plt.subplots()
plt.plot(x, y, 'r')
plt.xlim(xmax = 4 * np.pi)
plt.ylim(ymax = 2)

plt.xticks([0, np.pi, 2*np.pi, 3*np.pi, 4*np.pi],
           ['$0$', '$\pi$', '$2\pi$', '$3\pi$', '4$\pi$'], fontsize = 15)
plt.yticks([0, 1], ['$0$', '$\hat{u}$'], fontsize = 15)

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

plt.annotate('$\int_{0}^{\pi} |sin(x)| \mathrm{d}x$',
             xy=(0.5 * (a + (b/2)), 0.5), xytext = ( 2 * np.pi, 1.5),
             arrowprops = dict(arrowstyle = '->'),fontsize = 20)
plt.annotate('', xy=(0.5 * ((b/2) + b), 0.5), xytext = (2 * np.pi, 1.5),
             arrowprops = dict(arrowstyle = '->'), fontsize = 20)

plt.show()

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).