ml_derivative.html


001| """Derivatives Demo."""
002| ####################################################################################################
003| from sys import argv
004| from os import path
005| from mlchart import mlchart
006| from goodies import divisions
007| ####################################################################################################
008| BasePath = r'C:\CJS\prj\Python\charts'
009| ImgsPath = path.join(BasePath,'img')
010| Filename = 'derivatives.png'
011| 
012| xmin,xmax =  -3.0,  +3.0
013| ymin,ymax = -10.0, +10.0
014| 
015| ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
016| class derivatives_chart (mlchart):
017|     """Derivatives Chart Class."""
018|     def __init__ (self, data, lbls=[]):
019|         """New instance requires data (labels optional)."""
020|         super().__init__()
021|         self.data = data
022|         self.lbls = lbls
023|         self.dims = (8.0, 5.0)
024|         self.margins = dict(left=0.105, right=0.980, bottom=0.14, top=0.93)
025|         self.title = 'Basic Derivatives'
026|         self.x_title = 'X-Axis'
027|         self.y_title = 'Y-Axis'
028|         self.x_limit = (xmin, xmax)
029|         self.y_limit = (ymin, ymax)
030|         self.x_grids = (1.0, 0.10)
031|         self.y_grids = (5.0, 0.50)
032|         self.x_tickf = '%+.1f'
033|         self.y_tickf = '%+.1f'
034|         self.fpath = BasePath
035|         self.fname = Filename
036| 
037|     def plotter (self):
038|         """Plot something!"""
039|         self.ax.axvline(c='#000000', lw=2.0, alpha=1.00, zorder=11)
040|         self.ax.axhline(c='#000000', lw=2.0, alpha=1.00, zorder=12)
041|         self.ax.plot(self.data[0],self.data[1], label=self.lbls[0], c='#ff0000', lw=3.0, alpha=0.75, zorder=24)
042|         self.ax.plot(self.data[0],self.data[2], label=self.lbls[1], c='#007f00', lw=3.0, alpha=0.75, zorder=23)
043|         self.ax.plot(self.data[0],self.data[3], label=self.lbls[2], c='#0000ff', lw=3.0, alpha=0.75, zorder=22)
044|         self.ax.plot(self.data[0],self.data[4], label=self.lbls[3], c='#7f007f', lw=3.0, alpha=0.75, zorder=21)
045|         self.ax.legend(loc='lower right', shadow=True)
046| 
047|     def make_x_axis_spcl (self):
048|         """Rotate X-axis labels 90 degrees."""
049|         for tick in self.ax.xaxis.get_major_ticks():
050|             tick.label1.set_rotation('vertical')
051|             tick.label1.set_size(9)
052| 
053| 
054| ##================================================================================================##
055| def main ():
056|     lbls = [
057|         r"$f(x)=x^3$",
058|         r"$f'(x)=3x^2$",
059|         r"$f''(x)=6x$",
060|         r"$f'''(x)=6$",
061|     ]
062|     xs = list(divisions(xmin, xmax, 500))
063|     ys0 = [6 for x in xs]
064|     ys1 = [6*x for x in xs]
065|     ys2 = [3*pow(x,2) for x in xs]
066|     ys3 = [pow(x,3) for x in xs]
067|     data = (xs, ys3, ys2, ys1, ys0)
068| 
069|     cht = derivatives_chart(data, lbls)
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|