curvature.html


001| """Curvature Demo Chart."""
002| ####################################################################################################
003| from sys import argv
004| from os import path
005| from math import sqrt,exp,log, sin,cos, tau
006| import numpy as np
007| import matplotlib.pyplot as plt
008| from matplotlib.ticker import MultipleLocator, NullFormatter, FormatStrFormatter
009| from goodies import safediv
010| ####################################################################################################
011| BasePath = r'C:\CJS\prj\Python\charts'
012| ImgsPath = path.join(BasePath,'img')
013| Filename = 'curvature-%d.png'
014| 
015| ##------------------------------------------------------------------------------------------------##
016| Which = 1
017| 
018| pts = [1.0, 2.0, 3.0]
019| 
020| ##================================================================================================##
021| def configure_chart (ax, ttl, bg='#ffffff'):
022|     """Configure a chart panel with common parameters."""
023|     ax.set_title(ttl)
024|     ax.set_fc(bg)
025|     # X-Axis...
026|     ax.set_xlabel('')
027|     ax.set_xlim(-1.0, +5.0)
028|     ax.xaxis.set_major_locator(MultipleLocator(1.0))
029|     ax.xaxis.set_minor_locator(MultipleLocator(0.2))
030|     ax.xaxis.set_major_formatter(NullFormatter())
031|     # Y-Axis...
032|     ax.set_ylabel('')
033|     ax.set_ylim(-1.0, +5.0)
034|     ax.yaxis.set_major_locator(MultipleLocator(1.0))
035|     ax.yaxis.set_minor_locator(MultipleLocator(0.2))
036|     ax.yaxis.set_major_formatter(NullFormatter())
037|     # Grid...
038|     ax.grid(which='major', axis='both', color='#bfbfbf', lw=0.5,ls='-', alpha=1.0, zorder=0)
039|     ax.grid(which='minor', axis='both', color='#bfbfbf', lw=0.5,ls='-', alpha=1.0, zorder=1)
040|     ax.axvline(pts[0], ymax=1.0, color='#000000', lw=1.5, zorder=2)
041|     ax.axvline(pts[1], ymax=1.0, color='#000000', lw=1.5, zorder=2)
042|     ax.axvline(pts[2], ymax=1.0, color='#000000', lw=1.5, zorder=2)
043| 
044| ##================================================================================================##
045| def make_chart (xs, ys1, ys2, ys3):
046|     # Create a chart...
047|     fig,axes = plt.subplots(nrows=1, ncols=3, squeeze=True)
048|     fig.set_figwidth(8.8)
049|     fig.set_figheight(3.2)
050|     fig.subplots_adjust(left=0.02, right=0.99, bottom=0.05, top=0.83, wspace=0.08)
051|     fig.suptitle('Curvature: Actual vs. Expected Average (%s)' % ('Positive Slope' if Which==1 else 'Negative Slope'))
052|     ax1 = axes[0]
053|     ax2 = axes[1]
054|     ax3 = axes[2]
055| 
056|     # Left Axes...
057|     configure_chart(ax1, 'Negative Curvature', bg='#fff0f0')
058|     ax1.plot((pts[0],pts[2]),(f1(pts[0]),f1(pts[2])), c='#7f7f7f', lw=2.0, ls='--', zorder=11)
059|     ax1.plot(xs,ys1, c='#006600', lw=3.0, zorder=21)
060|     ax1.plot(pts,[f1(x) for x in pts], c='red', lw=0, marker='o', ms=5, zorder=31)
061|     ax1.axhline(f1(pts[0]), xmax=1.0, color='#ff0000', lw=1.8, zorder=3)
062|     ax1.axhline(f1(pts[1]), xmax=1.0, color='#ff0000', lw=1.8, zorder=3)
063|     ax1.axhline(f1(pts[2]), xmax=1.0, color='#ff0000', lw=1.8, zorder=4)
064| 
065|     # Middle Axes...
066|     configure_chart(ax2, 'Zero Curvature', bg='#f0fff0')
067|     ax2.plot(xs,ys2, c='#009900', lw=3.0, zorder=21)
068|     ax2.plot(pts,[f2(x) for x in pts], c='red', lw=0, marker='o', ms=5, zorder=31)
069|     ax2.axhline(f2(pts[0]), xmax=1.0, color='#ff0000', lw=1.8, zorder=3)
070|     ax2.axhline(f2(pts[1]), xmax=1.0, color='#ff0000', lw=1.8, zorder=3)
071|     ax2.axhline(f2(pts[2]), xmax=1.0, color='#ff0000', lw=1.8, zorder=4)
072| 
073|     # Right Axes...
074|     configure_chart(ax3, 'Positive Curvature', bg='#e0f0ff')
075|     ax3.plot((pts[0],pts[2]),(f3(pts[0]),f3(pts[2])), c='#7f7f7f', lw=2.0, ls='--', zorder=11)
076|     ax3.plot(xs,ys3, c='#000099', lw=3.0, zorder=21)
077|     ax3.plot(pts,[f3(x) for x in pts], c='red', lw=0, marker='o', ms=5, zorder=31)
078|     ax3.axhline(f3(pts[0]), xmax=1.0, color='#ff0000', lw=1.8, zorder=3)
079|     ax3.axhline(f3(pts[1]), xmax=1.0, color='#ff0000', lw=1.8, zorder=3)
080|     ax3.axhline(f3(pts[2]), xmax=1.0, color='#ff0000', lw=1.8, zorder=4)
081| 
082|     fn = path.join(BasePath, Filename % Which)
083|     fig.savefig(fn)
084|     print('wrote: %s' % fn)
085|     print()
086| 
087| ##================================================================================================##
088| def main ():
089|     xs = np.linspace(0.0, 4.0, num=50)
090|     if Which == 1:
091|         # -slope...
092|         f1 = lambda x: ((10*(1/(1.5+pow(4, -(x-1.2)))))-2.6)
093|         f2 = lambda x: (x)
094|         f3 = lambda x: (0.05*pow(x,3.3))
095|     if Which == 2:
096|         # -slope...
097|         f1 = lambda x: ((4-(pow(x,3)/16)) if 0<x else 4.0)
098|         f2 = lambda x: (4-x)
099|         f3 = lambda x: (4/(1+pow(x,2)))
100| 
101|     ys1 = [f1(x) for x in xs]
102|     ys2 = [f2(x) for x in xs]
103|     ys3 = [f3(x) for x in xs]
104| 
105|     make_chart(xs, ys1, ys2, ys3)
106| 
107| ####################################################################################################
108| if __name__ == '__main__':
109|     print(argv[0])
110|     print()
111|     main()
112|     print()
113| ####################################################################################################
114| '''eof'''
115|