ml_fractions.html


001| """Curves of p/q fractions with constant p+q values."""
002| ####################################################################################################
003| from sys import argv
004| from os import path
005| from mlchart import mlchart
006| ####################################################################################################
007| BasePath = r'C:\CJS\prj\Python\charts'
008| ImgsPath = path.join(BasePath,'img')
009| Filename = 'ml_fractions.png'
010| 
011| ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
012| class fractions_chart (mlchart):
013|     """ML Chart Class."""
014| 
015|     def __init__ (self, data):
016|         """New instance."""
017|         super().__init__()
018|         self.data = data
019|         self.dims = (7.0, 5.0)
020|         self.margins = dict(left=0.080, right=0.980, bottom=0.100, top=0.930)
021|         self.title = 'Rational Number Curves'
022|         self.x_title = 'Numerator'
023|         self.y_title = 'Fraction Value'
024|         self.x_limit = (0.0, +40.0)
025|         self.y_limit = (0.0, +30.0)
026|         self.x_grids = (10.0, 1.0)
027|         self.y_grids = (10.0, 1.0)
028|         self.x_tickf = '%d'
029|         self.y_tickf = '%d'
030|         self.fpath = BasePath
031|         self.fname = Filename
032| 
033|     def plotter (self):
034|         """Plot something!"""
035|         for rx,row in enumerate(fraction_data):
036|             #if (rx % 2) == 0:
037|             # continue
038|             ys = [p/q for p,q in row]
039|             xs = [p for p,q in row]
040|             self.ax.plot(xs,ys, lw=0.9, marker='o', ms=5, alpha=0.80, zorder=11+rx)
041|         #self.ax.legend(loc='lower right', shadow=True)
042| 
043|     def _make_x_axis_spcl (self):
044|         """Rotate X-axis labels 90 degrees."""
045|         for tick in self.ax.xaxis.get_major_ticks():
046|             tick.label1.set_rotation('vertical')
047|             tick.label1.set_size(9)
048| 
049| ##================================================================================================##
050| def main ():
051|     def fraction_list (n):
052|         p = 0
053|         q = n
054|         while 0 < q:
055|             yield (p,q)
056|             p += 1
057|             q -= 1
058| 
059|     fraction_data = [[(p,q) for p,q in fraction_list(n)] for n in range(42)]
060| 
061|     def dump_fraction_data ():
062|         for rx,row in enumerate(fraction_data):
063|             for p,q in row:
064|                 print('%2d: %d/%d; %.6f' % (rx,p,q,p/q))
065|             print()
066|         print()
067| 
068|     cht = fractions_chart(fraction_data)
069|     cht.make()
070|     print('wrote: %s' % path.join(cht.fpath, cht.fname))
071|     print()
072| 
073| ####################################################################################################
074| if __name__ == '__main__':
075|     print(argv[0])
076|     print()
077|     main()
078|     print()
079| ####################################################################################################
080| '''eof'''
081|