sine-waves.html
001| """Sine Waves."""
002|
003| from sys import argv
004| from os import path
005| from cmath import exp, pi, phase
006| import matplotlib.pyplot as plt
007| from matplotlib.ticker import NullLocator, MultipleLocator, NullFormatter, FormatStrFormatter
008| from goodies import divisions
009|
010| BasePath = r'C:\CJS\prj\Python\charts'
011| ImgsPath = path.join(BasePath,'img')
012| Filename = 'sine-waves.png'
013| print()
014|
015| Series = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
016| Odds = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29]
017| Evens = [2,4,6,8,10,12,14,16,18,20,22,24,26,28,30]
018| Primes = [1,2,3,5,7,11,13,17,19,23]
019| Fibona = [1,2,3,5,8,13,21,34,55,89]
020| Doubles = [1,2,4,8,16,32,64,128,256,512,1024]
021|
022| Freqs = [10.0, 10.1, 10.3, 10.5, 10.7, 10.9, 11.1]
023| xmin, xmax = -1.0, +1.0
024| ymin, ymax = -1.1, +1.1
025| xticks = (0.5, 0.125)
026| yticks = (1.0, 0.25)
027|
028| i2pi = complex(0,1) * complex(2.0*pi,0)
029| EXP = lambda x,k:exp(i2pi*complex(x*k,0))
030|
031| xs = list(divisions(xmin, xmax, 800))
032|
033| data = []
034| for ix,k in enumerate(Freqs):
035| ds = [EXP(x,k) for x in xs]
036| data.append(ds)
037|
038| ys0 = [sum(t)/float(len(data)) for t in zip(*data)]
039| ys1 = [c.real for c in ys0]
040| ys2 = [c.imag for c in ys0]
041| ys3 = [abs(c) for c in ys0]
042| ys4 = [phase(c)/pi for c in ys0]
043|
044| fig, ax = plt.subplots()
045| fig.suptitle('Sine Waves (exp-wheels)', fontsize=14, fontweight='bold')
046|
047| ax.set_xlabel('2pi*x', fontsize=14)
048| ax.set_xlim(xmin, xmax)
049| ax.xaxis.set_major_locator(MultipleLocator(xticks[0]))
050| ax.xaxis.set_minor_locator(MultipleLocator(xticks[1]))
051| ax.xaxis.set_major_formatter(FormatStrFormatter('%+.1f'))
052|
053| for tick in ax.xaxis.get_major_ticks():
054| tick.label1.set_rotation('vertical')
055| tick.label1.set_size(9)
056|
057| ax.set_ylabel('f(x)', fontsize=14)
058| ax.set_ylim(ymin, ymax)
059| ax.yaxis.set_major_locator(MultipleLocator(yticks[0]))
060| ax.yaxis.set_minor_locator(MultipleLocator(yticks[1]))
061| ax.yaxis.set_major_formatter(FormatStrFormatter('%.0f'))
062|
063| ax.grid(which='major', axis='both', color='#7f7f7f', lw=1.2,ls='-',zorder=1, alpha=1.0)
064| ax.grid(which='minor', axis='both', color='#bfbfbf', lw=0.6,ls='-',zorder=0, alpha=1.0)
065| ax.tick_params(which='major', axis='both', direction='out', color='#000000')
066| ax.tick_params(which='minor', axis='both', direction='out', color='#000000')
067|
068| ax.plot(xs,ys3, label='ampl', c='#7f007f', zorder=13, alpha=0.80, lw=2.0)
069| ax.plot(xs,ys1, label='real', c='#0000ff', zorder=12, alpha=0.80, lw=2.0)
070| ax.plot(xs,ys2, label='imag', c='#ff0000', zorder=11, alpha=0.80, lw=1.0)
071|
072| ax.legend(loc=(-0.075, -0.33), ncol=4, facecolor='#ffffff', framealpha=1.0)
073|
074| fig.set_figwidth(8.0)
075| fig.set_figheight(3.0)
076| fig.subplots_adjust(left=0.07, right=0.98, bottom=0.23, top=0.90)
077|
078| fn = path.join(ImgsPath, Filename)
079| fig.savefig(fn)
080| print('wrote: %s' % fn)
081| print()
082|
083| print()
084|
085| '''eof'''
086|