linear-cos-cos2.html


001| """Linear vs cos vs cos^2 Chart."""
002| ####################################################################################################
003| from sys import argv
004| from os import path
005| from math import pi, cos
006| import matplotlib.pyplot as plt
007| from matplotlib.ticker import MultipleLocator, NullFormatter, FormatStrFormatter
008| ####################################################################################################
009| BasePath = r'C:\CJS\prj\Python\blog'
010| ImgsPath = path.join(BasePath,'img')
011| Filename = 'linear-cos-cos2.png'
012| 
013| 
014| ##================================================================================================##
015| def make_chart (xs, ys1, ys2, ys3):
016|     fig, ax = plt.subplots()
017|     fig.suptitle(r'Linear vs $\cos$ vs $\cos^2$')
018|     fig.set_figwidth(5.0)
019|     fig.set_figheight(5.0)
020|     fig.subplots_adjust(left=0.14, right=0.98, bottom=0.11, top=0.93)
021| 
022|     ax.set_xlabel('Angle (degrees)')
023|     ax.set_xlim(0, 90)
024|     ax.xaxis.set_major_locator(MultipleLocator(10))
025|     ax.xaxis.set_minor_locator(MultipleLocator(5))
026|     ax.xaxis.set_major_formatter(NullFormatter())
027|     ax.xaxis.set_minor_formatter(FormatStrFormatter('%d'))
028| 
029|     for tick in ax.xaxis.get_minor_ticks():
030|         tick.label1.set_rotation('vertical')
031|         tick.label1.set_size(9)
032| 
033|     ax.set_ylabel('Probability')
034|     ax.set_ylim(0, 100)
035|     ax.yaxis.set_major_locator(MultipleLocator(10))
036|     ax.yaxis.set_minor_locator(MultipleLocator(5))
037|     ax.yaxis.set_major_formatter(FormatStrFormatter('%3d%%'))
038| 
039|     ax.grid(which='major', axis='both', color='#7f7f7f', lw=1.2,ls='-',zorder=1, alpha=1.0)
040|     ax.grid(which='minor', axis='both', color='#bfbfbf', lw=0.6,ls='-',zorder=0, alpha=1.0)
041|     ax.tick_params(which='major', axis='both', direction='out', color='#000000')
042|     ax.tick_params(which='minor', axis='both', direction='out', color='#000000')
043| 
044|     ##>> plot something
045|     ax.plot(xs,ys1, c='#000000', lw=1.5, label='linear'     )
046|     ax.plot(xs,ys2, c='#0000ff', lw=1.5, label=r'$\cos(\theta)$')
047|     ax.plot(xs,ys3, c='#ff0000', lw=2.0, label=r'$\cos^2(\theta)$')
048|     ax.legend()
049| 
050|     fn = path.join(ImgsPath, Filename)
051|     fig.savefig(fn)
052|     print('wrote: %s' % fn)
053|     print()
054| 
055| ##================================================================================================##
056| def main ():
057|     steps = 100
058|     xs = [n/steps for n in range(steps+1)]
059|     cs = [cos((pi/2.0)*x) for x in xs]
060|     ys1 = [100.0*(1.0-x) for x in xs]
061|     ys2 = [100.0*y for y in cs]
062|     ys3 = [100.0*pow(y,2) for y in cs]
063|     xs = [(90.0*x) for x in xs]
064| 
065|     make_chart(xs, ys1, ys2, ys3)
066| 
067| ####################################################################################################
068| if __name__ == '__main__':
069|     print(argv[0])
070|     print()
071|     main()
072|     print()
073| ####################################################################################################
074| '''eof'''
075|