z_word_histo.html
001|
002| from sys import argv
003| from os import path
004| import matplotlib.pyplot as plt
005| from matplotlib.ticker import NullLocator, MultipleLocator, NullFormatter, FormatStrFormatter
006| from goodies import KWARG, typename, makefilename
007| from files import csvfile
008|
009| BasePath = r'C:\CJS\prj\Python\blog\hcc'
010|
011|
012|
013| def word_count_histogram ():
014| fin_name = path.join(BasePath, 'word_sorted.list')
015| fin = csvfile(fin_name, headers=False)
016| fin.load()
017| print(f'loaded {len(fin)} records.')
018| print()
019|
020| print(fin.data[0])
021| print(fin.data[1])
022| print(fin.data[2])
023| print(fin.data[3])
024| print('...')
025| print()
026|
027| bucket_list = [100_000, 50_000, 10_000, 5_000, 1_000, 500, 100, 50]
028|
029| def get_bucket (nbr:str) -> int:
030| n = int(nbr)
031| for bucket in bucket_list:
032| if bucket <= n:
033| return bucket
034| return n
035|
036| histo = {}
037| for word,nbr in fin:
038| key = get_bucket(nbr)
039| if key not in histo: histo[key] = 0
040| histo[key] += 1
041|
042| fout_name = path.join(BasePath, 'word_histo.list')
043| fout = csvfile(fout_name)
044| fout.hdrs.append('bin')
045| fout.hdrs.append('count')
046| for n in sorted(histo):
047| fout.add((n, histo[n]))
048| fout.save()
049|
050| return histo
051|
052|
053| def main (*args):
054| data = word_count_histogram()
055|
056| xs = [d for d in sorted(data) if d < 50]
057| ys = [data[x] for x in xs]
058| cs = ['#ff3311' if x==1 else '#3366ff' for x in xs]
059|
060| xmin,xmax = 0, 50
061| ymin,ymax = 0, 2_000
062|
063| xgrid = 5, 1
064| ygrid = 500, 100
065|
066| fig, ax = plt.subplots()
067| fig.suptitle('Word Count Histogram', fontsize=13, fontweight='bold')
068| fig.set_figwidth(8.8)
069| fig.set_figheight(3.0)
070| fig.subplots_adjust(left=0.082, right=0.985, bottom=0.15, top=0.90)
071|
072| ax.set_xlabel('Word Count Bucket', fontsize=11)
073| ax.set_xlim(xmin, xmax)
074| ax.xaxis.set_major_locator(MultipleLocator(xgrid[0]))
075| ax.xaxis.set_minor_locator(MultipleLocator(xgrid[1]))
076| ax.xaxis.set_major_formatter(FormatStrFormatter('%d'))
077| ax.xaxis.set_minor_formatter(NullFormatter())
078|
079| ax.set_ylabel('Number Occurances', fontsize=11)
080| ax.set_ylim(ymin, ymax)
081| ax.yaxis.set_major_locator(MultipleLocator(ygrid[0]))
082| ax.yaxis.set_minor_locator(MultipleLocator(ygrid[1]))
083| ax.yaxis.set_major_formatter(FormatStrFormatter('%d'))
084| ax.yaxis.set_minor_formatter(NullFormatter())
085|
086| ax.grid(which='major', axis='both', color='#7f7f7f', lw=1.2,ls='-',zorder=1, alpha=1.0)
087| ax.grid(which='minor', axis='both', color='#bfbfbf', lw=0.6,ls='-',zorder=0, alpha=1.0)
088| ax.tick_params(which='major', axis='both', direction='out', color='#000000')
089| ax.tick_params(which='minor', axis='both', direction='out', color='#000000')
090|
091| ax.bar(xs,ys, width=0.75, color=cs, alpha=0.75, zorder=11)
092| arrprops = dict(width=2, fc='#000000', headwidth=5, headlength=10)
093| boxprops = dict(boxstyle='Round', fc='#ffffbf', ec='#000000')
094| ax.annotate(
095| f'{ys[0]:,}',
096| (xs[0], ymax),
097| xytext=(xs[3], ymax-(3*ygrid[1])),
098| arrowprops=arrprops,
099| bbox=boxprops,
100| zorder=20,
101| )
102|
103|
104| fn = path.join(BasePath, 'word count histo.png')
105| fig.savefig(fn)
106| print('wrote: %s' % fn)
107| print()
108|
109| return data
110|
111| if __name__ == '__main__':
112| print(argv[0])
113| print()
114| rv = main(*argv[1:])
115| print()
116|
117| '''eof'''
118|