pi-freq.html


001| '''Pi Frequency Chart.'''
002| ####################################################################################################
003| from sys import argv
004| from os import path
005| from math import sqrt
006| import matplotlib.pyplot as plt
007| from matplotlib.ticker import NullLocator, MultipleLocator, NullFormatter, FormatStrFormatter
008| ####################################################################################################
009| BasePath = r'C:\CJS\prj\Python\blog\proj'
010| ImgsPath = path.join(BasePath, 'img')
011| Filename = 'pi-freq.png'
012| 
013| ##------------------------------------------------------------------------------------------------##
014| PiFreqs = [-0.000560, -0.000667, +0.000306, -0.000036, +0.001093,
015|            +0.000466, -0.000663, +0.000207, -0.000186, +0.000040]
016| 
017| PiCounts = [ 999440, 999333, 1000306, 999964, 1001093,
018|             1000466, 999337, 1000207, 999814, 1000040]
019| 
020| Total = float(sum(PiCounts))
021| 
022| ##================================================================================================##
023| def make_chart (xs, ys):
024|     fig, ax = plt.subplots()
025|     ax.set_title('(Base 10) Digit Frequencies in Pi')
026|     ax.set_xlabel('Digit')
027|     ax.set_ylabel('Frequency Diff (%)')
028| 
029|     ax.set_xlim(-0.5, 9.5)
030|     ax.xaxis.set_major_locator(MultipleLocator(1))
031|     ax.xaxis.set_major_formatter(FormatStrFormatter('%d'))
032| 
033|     ax.set_ylim(+9.985, +10.015)
034|     ax.yaxis.set_major_locator(MultipleLocator(0.005))
035|     ax.yaxis.set_minor_locator(MultipleLocator(0.001))
036|     ax.yaxis.set_major_formatter(FormatStrFormatter('%+.3f'))
037| 
038|     ax.grid(which='major', axis='both', color='#7f7f7f', lw=1.2,ls='-',zorder=1, alpha=1.0)
039|     ax.grid(which='minor', axis='both', color='#bfbfbf', lw=0.6,ls='-',zorder=0, alpha=1.0)
040|     ax.tick_params(which='major', axis='both', direction='out', color='#000000')
041|     ax.tick_params(which='minor', axis='both', direction='out', color='#000000')
042| 
043|     ax.bar(xs,ys, color='#2367bf', zorder=10)
044|     ax.axhline(10.0, lw=2.00, color='k', zorder=11)
045| 
046|     fig.set_figwidth(8.0)
047|     fig.set_figheight(6.0)
048|     fig.subplots_adjust(left=0.13, right=0.99, bottom=0.10, top=0.94)
049| 
050|     fn = path.join(ImgsPath, Filename)
051|     fig.savefig(fn)
052|     print(f'wrote: {fn}')
053|     print()
054| 
055| ##================================================================================================##
056| def main ():
057|     xs = list(range(10))
058|     ys = [100*float(n)/Total for n in PiCounts]
059| 
060|     make_chart(xs, ys)
061| 
062| ####################################################################################################
063| if __name__ == '__main__':
064|     print(argv[0])
065|     print()
066|     main()
067|     print()
068| ####################################################################################################
069| '''eof'''
070|