randfreqs.html


001| """Random Frequencies."""
002| ####################################################################################################
003| from sys import argv
004| from os import path
005| from random import randint, choice, random, triangular, gauss
006| import numpy as np
007| import matplotlib.pyplot as plt
008| from matplotlib.ticker import MultipleLocator, FormatStrFormatter, NullFormatter
009| ####################################################################################################
010| BasePath = r'C:\CJS\prj\Python\blog\proj'
011| ImgsPath = path.join(BasePath,'img')
012| Filename = 'random.png'
013| 
014| ##------------------------------------------------------------------------------------------------##
015| yscale = 1000
016| samples = 10**6
017| bins = 3000
018| 
019| Clip = lambda lo,n,hi: max(lo,min(hi,n))
020| RndUp = lambda n,e: ((1+int(n/e))*e)
021| 
022| ##================================================================================================##
023| def make_chart (data, ymin, ymax):
024| 
025|     fig, ax = plt.subplots()
026|     ax.set_title('Random Functions Frequency Spectrum')
027|     ax.set_xlabel('Value Range (%d bins)' % bins)
028|     ax.set_ylabel('Frequency (%.0E samples)' % samples)
029| 
030|     ax.set_xlim(0.0, 1.0)
031|     ax.xaxis.set_major_locator(MultipleLocator(0.10))
032|     ax.xaxis.set_minor_locator(MultipleLocator(0.02))
033|     ax.xaxis.set_major_formatter(FormatStrFormatter('%.1f'))
034|     for tick in ax.xaxis.get_major_ticks():
035|         tick.label1.set_rotation('vertical')
036|         tick.label1.set_size(9)
037| 
038|     ax.set_ylim(ymin, ymax)
039|     ax.yaxis.set_major_locator(MultipleLocator(yscale))
040|     ax.yaxis.set_minor_locator(MultipleLocator(yscale/10))
041|     ax.yaxis.set_major_formatter(NullFormatter())
042| 
043|     ax.grid(which='major', axis='both', color='#7f7f7f', lw=1.2,ls='-',zorder=1, alpha=1.0)
044|     ax.grid(which='minor', axis='both', color='#bfbfbf', lw=0.6,ls='-',zorder=0, alpha=1.0)
045|     ax.tick_params(which='major', axis='both', direction='out', color='#000000')
046|     ax.tick_params(which='minor', axis='both', direction='out', color='#000000')
047| 
048|     xs, ys0,ys1,ys2,ys3,ys4 = data
049|     ax.plot(xs,ys0, label='random', color='#008000', lw=1.0, ms=0, alpha=1.0, zorder=10)
050|     ax.plot(xs,ys1, label='tri-0.5', color='#ff0000', lw=1.0, ms=0, alpha=1.0, zorder=11)
051|     ax.plot(xs,ys2, label='tri-0.2', color='#c04000', lw=1.0, ms=0, alpha=1.0, zorder=12)
052|     ax.plot(xs,ys3, label='gauss-0.10', color='#0000ff', lw=1.0, ms=0, alpha=1.0, zorder=13)
053|     ax.plot(xs,ys4, label='gauss-0.05', color='#000080', lw=1.0, ms=0, alpha=1.0, zorder=14)
054|     ax.legend(bbox_to_anchor=(0.86,0.79), loc='center', shadow=True)
055| 
056|     fig.set_figwidth(4.40)
057|     fig.set_figheight(3.2)
058|     fig.subplots_adjust(left=0.08, right=0.98, bottom=0.17, top=0.92)
059| 
060|     fn = path.join(ImgsPath, Filename)
061|     fig.savefig(fn)
062|     print('wrote: %s' % fn)
063|     print()
064| 
065| ##================================================================================================##
066| def main ():
067|     xs = [float(n)/float(bins) for n in range(bins)]
068|     ys0 = [0]*bins
069|     ys1 = [0]*bins
070|     ys2 = [0]*bins
071|     ys3 = [0]*bins
072|     ys4 = [0]*bins
073| 
074|     for _ in range(samples):
075|         n0 = int(random() * bins)
076|         n1 = int(triangular(0.0, 1.0, 0.5) * bins)
077|         n2 = int(triangular(0.0, 1.0, 0.2) * bins)
078|         n3 = int(gauss(0.5, 0.100) * bins)
079|         n4 = int(gauss(0.5, 0.050) * bins)
080|         ys0[n0] += 1
081|         ys1[n1] += 1
082|         ys2[n2] += 1
083|         ys3[Clip(0,n3,bins-1)] += 1
084|         ys4[Clip(0,n4,bins-1)] += 1
085| 
086|     data = (xs, ys0, ys1, ys2, ys3, ys4)
087|     ytop = max(ys1+ys2+ys3+ys4)
088|     ymin,ymax = 0, RndUp(ytop, yscale//10)
089| 
090|     make_chart(data, ymin, ymax)
091| 
092| ####################################################################################################
093| if __name__ == '__main__':
094|     print(argv[0])
095|     print()
096|     main()
097|     print()
098| ####################################################################################################
099| '''eof'''
100|