spacetime.html


001| """Spacetime Interval."""
002| ####################################################################################################
003| from os import path
004| from math import sqrt
005| import matplotlib.pyplot as plt
006| from matplotlib.ticker import NullLocator, MultipleLocator, NullFormatter, FormatStrFormatter
007| ####################################################################################################
008| BasePath = r'C:\CJS\prj\Python\charts'
009| ImgsPath = path.join(BasePath,'img')
010| print()
011| 
012| ##================================================================================================##
013| def spacetime_1 (fname='spacetime-1.png', fpath=ImgsPath):
014|     steps = 200
015|     xs = [-6.0 + (12.0*(float(ix)/float(steps))) for ix in range(steps)]
016|     ns = [n*n for n in [1,2,3,4,5]]
017| 
018|     data = {}
019|     for n in ns:
020|         data[n] = []
021|         for x in xs:
022|             data[n].append(sqrt(n+(x*x)))
023| 
024|     fig, ax = plt.subplots()
025|     ax.set_title('Spacetime Interval', fontsize='14', fontweight='bold')
026|     ax.set_xlabel('X (Space) Axis')
027|     ax.set_ylabel('T (Time) Axis')
028| 
029|     ax.set_xlim(-6.0, +6.0)
030|     ax.xaxis.set_major_locator(MultipleLocator(1))
031|     ax.xaxis.set_minor_locator(MultipleLocator(0.25))
032|     ax.xaxis.set_major_formatter(FormatStrFormatter('%d'))
033| 
034|     ax.set_ylim(-6.0, +6.0)
035|     ax.yaxis.set_major_locator(MultipleLocator(1))
036|     ax.yaxis.set_minor_locator(MultipleLocator(0.25))
037|     ax.yaxis.set_major_formatter(FormatStrFormatter('%d'))
038| 
039|     ax.grid(which='major', axis='both', color='#cfcfcf', lw=1.4,ls='-',zorder=1, alpha=1.0)
040|     ax.grid(which='minor', axis='both', color='#cfcfcf', lw=0.5,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|     ax.plot([-6,0,+6], [-6,0,+6], color='red', lw=2.0, zorder=10)
045|     ax.plot([-6,0,+6], [+6,0,-6], color='red', lw=2.0, zorder=10)
046|     ax.axhline(0.0, lw=2.0, color='k', zorder=11)
047|     ax.axvline(0.0, lw=4.0, color='k', zorder=11)
048|     ax.plot([-3,0,+3], [-6,0,+6], color='#0000ff', lw=4.0, zorder=12)
049|     ax.plot([-6,0,+6], [-3,0,+3], color='#0000bf', lw=3.0, zorder=12)
050| 
051|     for n in ns:
052|         ys = data[n]
053|         ax.plot(xs,ys, color='#009f00', lw=0.8, zorder=9)
054|         ax.plot(ys,xs, color='#bf00bf', lw=0.8, zorder=9)
055|         ys = [-y for y in ys]
056|         ax.plot(xs,ys, color='#009f00', lw=0.8, zorder=9)
057|         ax.plot(ys,xs, color='#bf00bf', lw=0.8, zorder=9)
058| 
059|     fig.set_figwidth(8.0)
060|     fig.set_figheight(8.0)
061|     fig.subplots_adjust(left=0.07, right=0.99, bottom=0.07, top=0.96)
062| 
063|     fn = path.join(fpath, fname)
064|     fig.savefig(fn)
065|     print('wrote: %s' % fn)
066|     print()
067| 
068| ##================================================================================================##
069| def spacetime_2 (fname='spacetime-2.png', fpath=ImgsPath):
070|     steps = 200
071|     gamma = 1.0/sqrt(0.75)
072|     xdist = gamma * 0.5
073|     xs = [-2.0 + (4.0*(float(ix)/float(steps))) for ix in range(steps)]
074|     ys = [sqrt(1+(x*x)) for x in xs]
075| 
076|     fig, ax = plt.subplots()
077|     ax.set_title('Spacetime Interval', fontsize='14', fontweight='bold')
078|     ax.set_xlabel('X (Space) Axis')
079|     ax.set_ylabel('T (Time) Axis')
080| 
081|     ax.set_xlim(-2.0, +2.0)
082|     ax.xaxis.set_major_locator(MultipleLocator(1))
083|     ax.xaxis.set_minor_locator(MultipleLocator(0.10))
084|     ax.xaxis.set_major_formatter(FormatStrFormatter('%d'))
085| 
086|     ax.set_ylim(-2.0, +2.0)
087|     ax.yaxis.set_major_locator(MultipleLocator(1))
088|     ax.yaxis.set_minor_locator(MultipleLocator(0.10))
089|     ax.yaxis.set_major_formatter(FormatStrFormatter('%d'))
090| 
091|     ax.grid(which='major', axis='both', color='#cfcfcf', lw=1.4,ls='-',zorder=1, alpha=1.0)
092|     ax.grid(which='minor', axis='both', color='#cfcfcf', lw=0.5,ls='-',zorder=0, alpha=1.0)
093|     ax.tick_params(which='major', axis='both', direction='out', color='#000000')
094|     ax.tick_params(which='minor', axis='both', direction='out', color='#000000')
095| 
096|     ax.plot([-2,0,+2], [-2,0,+2], color='#ff7f7f', lw=1.0, zorder=10)
097|     ax.plot([-2,0,+2], [+2,0,-2], color='#ff7f7f', lw=1.0, zorder=10)
098|     ax.axvline(0.0, lw=2.0, color='k', zorder=11)
099|     ax.axhline(0.0, lw=2.0, color='k', zorder=11)
100|     ax.plot([-1.0,0,+1.0], [-2.0,0,+2.0], color='#0000ff', lw=3.0, zorder=13)
101|     ax.plot([-2.0,0,+2.0], [-1.0,0,+1.0], color='#0000bf', lw=3.0, zorder=12)
102| 
103|     ax.plot(xs,ys, color='#009f00', lw=1.5, zorder=9)
104|     ax.plot(ys,xs, color='#bf00bf', lw=1.5, zorder=9)
105|     ax.plot(xs,[-y for y in ys], color='#009f00', lw=1.5, zorder=9)
106|     ax.plot([-y for y in ys],xs, color='#bf00bf', lw=1.5, zorder=9)
107| 
108|     ax.vlines([xdist],0,gamma, color='#007f7f', lw=4.0, zorder=14)
109|     ax.hlines([gamma],0,xdist, color='#007f7f', lw=4.0, zorder=14)
110| 
111|     fig.set_figwidth(8.0)
112|     fig.set_figheight(8.0)
113|     fig.subplots_adjust(left=0.07, right=0.99, bottom=0.07, top=0.96)
114| 
115|     fn = path.join(fpath, fname)
116|     fig.savefig(fn)
117|     print('wrote: %s' % fn)
118|     print()
119| 
120| ####################################################################################################
121| if __name__ == '__main__':
122|     spacetime_1()
123|     spacetime_2()
124| ####################################################################################################
125| '''eof'''
126|