plato-div-line2.html


001| """Plato's Divided Line Analysis."""
002| ####################################################################################################
003| from sys import argv
004| from os import path
005| from mlchart import mlchart
006| from matplotlib import lines
007| from goodies import intervals
008| ####################################################################################################
009| BasePath = r'C:\CJS\prj\Python\blog'
010| ImgsPath = path.join(BasePath,'img')
011| 
012| ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
013| class mlchart_plato_div_line (mlchart):
014|     """Plato's Divided Line Analysis."""
015|     def __init__ (self, p, q, start=None, stop=None, steps=1.0):
016|         """New instance requires ratio P/Q (control info optional)."""
017|         super().__init__()
018|         self.dims = (5.0, 5.0)
019|         self.margins = dict(left=0.150, right=0.980, bottom=0.14, top=0.93)
020|         self.title = 'Plato\'s Divided Line'
021|         self.x_title = 'Segment Height'
022|         self.y_title = 'Segment Height'
023|         self.x_limit = (1.0, +21.0)
024|         self.y_limit = (0.0, +21.0)
025|         self.x_grids = ( 3.0, 1.0)
026|         self.y_grids = ( 3.0, 1.0)
027|         self.x_tickf = '%.2f'
028|         self.y_tickf = '%.2f'
029|         self.p = p
030|         self.q = q
031|         self.u = q - p
032|         self.fp = float(p)
033|         self.fq = float(q)
034|         self.d = self.fq / self.fp
035|         self.scale = self.d - 1.0
036|         self.start = start if start else self.x_limit[0]
037|         self.stop  = stop if stop else self.x_limit[1]
038|         self.steps = steps
039|         self.fpath = ImgsPath
040|         self.fname = 'platos-div-line2_.png'
041|         print()
042|         print('upper: %d/%d (%.3f)' % (self.u,self.q, self.u/self.fq))
043|         print('lower: %d/%d (%.3f)' % (self.p,self.q, self.fp/self.fq))
044|         print('ratio: [1:d] d=%.3f' % self.d)
045|         print('scale: %.3f' % self.scale)
046|         print()
047| 
048|     def plotter (self):
049|         """Plot something!"""
050|         xs = list(intervals(self.start, self.stop, self.steps))
051|         ys1 = [x for x in xs]
052|         ys2 = [x/self.scale for x in xs]
053|         #
054|         self.ax.fill_between(xs,ys1,ys2, color='#ff0000', lw=1.0, alpha=0.50, zorder=11)
055|         self.ax.fill_between(xs,ys2,0, color='#0000ff', lw=1.0, alpha=0.50, zorder=12)
056|         self.ax.plot(xs,ys1, c='#000000', lw=2.0, alpha=1.0, zorder=13)
057|         #ATTN NO WORK!
058|         self.ax.add_line(lines.Line2D([0.0,1.00], [2.0,2.0], lw=2, color='green', axes=self.ax))
059|         #self.ax.legend(loc=(+0.70, +0.95), ncol=2, fontsize=8, shadow=True, facecolor='#000000', framealpha=1.0)
060| 
061|     def make_x_axis_spcl (self):
062|         """Rotate X-axis labels 90 degrees."""
063|         for tick in self.ax.xaxis.get_major_ticks():
064|             tick.label1.set_rotation('vertical')
065|             tick.label1.set_size(9)
066| 
067| ##================================================================================================##
068| def main ():
069|     cht = mlchart_plato_div_line(1, 3)
070|     cht.make()
071|     print('wrote: %s' % path.join(cht.fpath, cht.fname))
072|     print()
073| 
074| ####################################################################################################
075| if __name__ == '__main__':
076|     print(argv[0])
077|     print()
078|     main()
079|     print()
080| ####################################################################################################
081| '''eof'''
082|