muons.html
001| """Muons. """
002|
003| from sys import argv
004| from os import path
005| from math import sqrt
006| import matplotlib.pyplot as plt
007| from matplotlib.ticker import NullLocator, MultipleLocator, NullFormatter, FormatStrFormatter
008|
009| BasePath = r'C:\CJS\prj\Python\blog'
010| ImgsPath = path.join(BasePath,'img')
011| Filename = 'muons.png'
012|
013|
014| Cms = 299792458
015| Cks = float(Cms)/1.0E3
016| Cus = float(Cms)/1.0E9
017|
018| gamma = lambda v: 1.0 / sqrt(1.0 - ((v*v)/1.0))
019|
020| km_at = lambda v: ((v * Cus) * (2.2 * gamma(v)))
021|
022| ss = ['Avg Dist...']
023| ss.append('@ 0.0c = %.4f km' % km_at(0.0))
024| ss.append('@ 0.1c = %.4f km' % km_at(0.1))
025| ss.append('@ 0.5c = %.3f km' % km_at(0.5))
026| ss.append('@ 0.9c = %.3f km' % km_at(0.9))
027| ss.append('@ 0.99c = %.2f km' % km_at(0.99))
028| ss.append('@ 0.999c = %.2f km' % km_at(0.999))
029| ss.append('@ 0.9999c = %.1f km' % km_at(0.9999))
030| ss.append('@ 0.99999c = %.1f km' % km_at(0.99999))
031| ss.append('@ 0.999999c = %.1f km' % km_at(0.999999))
032|
033| kmtext = '\n'.join(ss)
034|
035| bbprops = dict(facecolor='#ffffbf', capstyle='round', alpha=1.0)
036| kmprops = dict(horizontalalignment='left', verticalalignment='top', bbox=bbprops)
037|
038| nx = 500
039|
040| spread = lambda x: (float(x) / float(nx))
041| distro = lambda x: ((900.0 + (100.0*spread(x))) / 1000.0)
042|
043| b1 = 'half-life [from 1.5ms]'
044| b2 = 'avg life [from 2.2ms]'
045| b3 = 'avg distance (in km)'
046| b4 = '50 km contraction'
047|
048|
049| def make_chart (xs, ys1, ys2, ys3, ys4):
050| fig, ax = plt.subplots()
051| ax.set_title('Relativistic Muons!', fontsize=16, fontweight='bold')
052| ax.set_xlabel('Fraction of C', fontsize=12, fontweight='bold')
053| ax.set_ylabel('Microseconds / Kilometres', fontsize=12, fontweight='bold')
054|
055| ax.set_xlim(0.9, 1.0)
056| ax.set_ylim(0.0, +80.0)
057|
058| ax.xaxis.set_major_locator(MultipleLocator(0.010))
059| ax.xaxis.set_minor_locator(MultipleLocator(0.005))
060| ax.xaxis.set_major_formatter(FormatStrFormatter('%.3f'))
061|
062| ax.yaxis.set_major_locator(MultipleLocator(10.0))
063| ax.yaxis.set_minor_locator(MultipleLocator(2.0))
064| ax.yaxis.set_major_formatter(FormatStrFormatter('%.0f'))
065|
066| ax.grid(which='major', axis='both', color='#7f7f7f', lw=1.2,ls='-',zorder=1, alpha=1.0)
067| ax.grid(which='minor', axis='both', color='#bfbfbf', lw=0.6,ls='-',zorder=0, alpha=1.0)
068| ax.tick_params(which='major', axis='both', direction='out', color='#000000')
069| ax.tick_params(which='minor', axis='both', direction='out', color='#000000')
070|
071| for tick in ax.xaxis.get_major_ticks():
072| tick.label1.set_rotation('vertical')
073| tick.label1.set_size(9)
074|
075| ax.plot(xs,ys1, label=b1, color='#ff0000', lw=1.2, ms=0, alpha=1.0, zorder=7)
076| ax.plot(xs,ys2, label=b2, color='#005f00', lw=1.8, ms=0, alpha=1.0, zorder=8)
077| ax.plot(xs,ys3, label=b3, color='#0000ff', lw=2.5, ms=0, alpha=1.0, zorder=9)
078| ax.plot(xs,ys4, label=b4, color='#007fff', lw=2.5, ms=0, alpha=1.0, zorder=6)
079|
080| ax.legend(loc='upper left', bbox_to_anchor=(0.0,1.0), shadow=True, fontsize=11)
081| ax.text(0.965,76, kmtext, **kmprops)
082|
083| fig.set_figwidth(8.0)
084| fig.set_figheight(6.0)
085| fig.subplots_adjust(left=0.08, right=0.98, bottom=0.13, top=0.94)
086|
087| fn = path.join(ImgsPath, Filename)
088| fig.savefig(fn)
089| print('wrote: %s' % fn)
090| print()
091|
092|
093| def main ():
094| xs = [distro(x) for x in range(nx)]
095|
096| ys1 = [(1.5 * gamma(x)) for x in xs]
097| ys2 = [(2.2 * gamma(x)) for x in xs]
098| ys3 = [(x * Cus) * (2.2 * gamma(x)) for x in xs]
099| ys4 = [(50.0 / gamma(x)) for x in xs]
100|
101| make_chart(xs, ys1, ys2, ys3, ys4)
102|
103|
104| if __name__ == '__main__':
105| print(argv[0])
106| print()
107| main()
108| print()
109|
110| '''eof'''
111|