### Sine and Cosine Graph using Python

### Introduction to Sine/Cosine Graph

The sine and cosine graphs are almost identical, except the cosine curve (*y* = *cos*(*x*)) starts at amplitude *y* = 1, when angle *x* = 0° (whereas the sine curve (*y* = *sin*(*x*)) starts at amplitude *y* = 0). We say the cosine curve is a sine curve which is shifted to the left by 90°. The graph of sine and cosine is like a wave that forever oscillates between -1 and 1, in a shape that repeats itself every 360° (2π) units.

### Program Implementation

In this tutorial, I have used Python 3.5.2 (64-bit) software, and 7 modules: MatPlotLib 2.0.2, PyParsing 2.2.0, Python-DateUtil 2.6.1, PyTZ 2017.2, SetupTools 36.2.0, Cycler 0.10.0, and NumPy-MKL 1.13.1 implemented in Windows 10 Enterprise operating system. The 7 modules are chosen based on the compatibility of Python and OS version and bit.

#### Source Code

```
import numpy as np
import matplotlib.pyplot as plt
X = np.linspace(-np.pi, np.pi, 256, endpoint = True)
C, S = np.cos(X), np.sin(X)
plt.plot(X, C, color = "blue", linewidth = 1,
linestyle = "-", label = r'$y = cos(x)$')
plt.plot(X, S, color = "red", linewidth = 1,
linestyle = "-", label = r'$y = sin(x)$')
plt.legend(loc = 'upper left')
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')
ax.spines['left'].set_position(('data', 0))
ax.spines['bottom'].set_position(('data', 0))
plt.xlim(X.min() * 1.1, X.max() * 1.1)
plt.xticks([-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi],
[r'$-180^\circ$', r'$-90^\circ$', r'$0$', r'$+90^\circ$', r'$+180^\circ$'])
plt.ylim(C.min() * 1.1, C.max() * 1.1)
plt.yticks([-1, 1], [r'$-1$', r'$+1$'])
plt.show()
```

useful

ReplyDelete