@sines-2.py

from __future__ import print_function
from os import path
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocatorFormatStrFormatter

ImgsPath = r'C:\CJS\prj\Python\ws\charts\imgs'
fn = 'sines-2.png'
tex = r'$\frac{x^1}{1!} - \frac{x^3}{3!} + \frac{x^5}{5!} - \frac{x^7}{7!} + ...$'

Factorial = lambda n: n if n<=2 else (n * Factorial(n-1))
Taylor = lambda x,n: ((x**n) / Factorial(n))

x0x1 = -3.00, +3.00
y0y1 = -2.50, +2.50

xminxmax = (x0 * np.pi),  (x1 * np.pi)

xt = np.linspace(x0x17)
xx = np.linspace(xminxmax201)

dx1 = [+Taylor(x1for x in xx]
dx2 = [-Taylor(x3for x in xx]
dx3 = [+Taylor(x5for x in xx]
dx4 = [-Taylor(x7for x in xx]
dx5 = [+Taylor(x9for x in xx]
dx6 = [-Taylor(x,11for x in xx]
dx7 = [+Taylor(x,13for x in xx]
dx8 = [-Taylor(x,15for x in xx]
dx9 = [+Taylor(x,17for x in xx]
dx0 = [sum(tfor t in zip(dx1,dx2,dx3,dx4,dx5,dx6,dx7,dx8,dx9)]
sx0 = [np.sin(xfor x in xx]

figax = plt.subplots()
fig.suptitle('Taylor Series for Sine (1,3,5,..17)')
ax.set_xlim(xminxmax)
ax.set_ylim(y0y1)
ax.minorticks_on()
ax.set_xticks([x*np.pi for x in xt])
ax.set_xticklabels(['%+d $pi$' % int(xfor x in xt])
ax.xaxis.set_minor_locator(MultipleLocator(np.pi/4))
ax.yaxis.set_major_locator(MultipleLocator(1.00))
ax.yaxis.set_minor_locator(MultipleLocator(0.25))
ax.yaxis.set_major_formatter(FormatStrFormatter('%+.2f'))
ax.tick_params(which='major'axis='both'direction='out'zorder=0)
ax.tick_params(which='minor'axis='both'direction='out'zorder=0)
ax.grid(which='minor'axis='both'color='#efefff'lw=0.6,ls='-',zorder=0alpha=1.0)
ax.grid(which='major'axis='both'color='#7f7fbf'lw=1.2,ls='-',zorder=2alpha=1.0)

ax.axhline(0.0color='k'lw=1.8zorder=5)
ax.axvline(0.0color='k'lw=1.8zorder=5)
ax.plot(xx,dx1color='#000000'lw=0.80zorder=4alpha=1.00)
ax.plot(xx,dx2color='#1f1f1f'lw=0.80zorder=4alpha=1.00)
ax.plot(xx,dx3color='#3f3f3f'lw=0.80zorder=4alpha=1.00)
ax.plot(xx,dx4color='#5f5f5f'lw=0.80zorder=4alpha=1.00)
ax.plot(xx,dx5color='#7f7f7f'lw=0.80zorder=4alpha=1.00)
ax.plot(xx,dx6color='#9f9f9f'lw=0.80zorder=4alpha=1.00)
ax.plot(xx,dx7color='#afafaf'lw=0.80zorder=4alpha=1.00)
ax.plot(xx,dx8color='#cfcfcf'lw=0.80zorder=4alpha=1.00)
ax.plot(xx,dx9color='#dfdfdf'lw=0.80zorder=4alpha=1.00)

p1 = ax.plot(xx,dx0color='#ff0000'lw=1.60zorder=8alpha=1.00)
p2 = ax.plot(xx,sx0color='#0000ff'lw=1.20zorder=9alpha=0.50)
ax.legend([p1[0],p2[0]],[tex,'sin(x)'], loc='upper left'shadow=True)

fig.set_figwidth(8.0)
fig.set_figheight(4.8)
fig.subplots_adjust(left=0.08bottom=0.10right=0.97top=0.90)
fig.savefig(path.join(ImgsPath,fn))
#plt.show()