births-deaths.html


001| """Births and Deaths 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 MultipleLocator, NullFormatter, FormatStrFormatter
008| from files import csvfile
009| ####################################################################################################
010| BasePath = r'C:\CJS\prj\Python\proj\blog'
011| ImgsPath = path.join(BasePath,'img')
012| DataPath = path.join(BasePath,'dat')
013| Filename1 = 'births-deaths-all.png'
014| Filename2 = 'births-deaths-usa.png'
015| DataFile1 = 'births-per-year.csv'
016| DataFile2 = 'deaths-per-year.csv'
017| 
018| Factor = 1000000.0
019| FData = lambda t:(int(t[2]),float(int(t[4])/Factor) if t[3]=='0' else float(int(t[3])/Factor))
020| 
021| 
022| ##================================================================================================##
023| def chart1 (births_all, deaths_all):
024|     xs  = [t[0] for t in births_all]
025|     ys1 = [t[1] for t in births_all]
026|     ys2 = [t[1] for t in deaths_all]
027|     ys3 = [t1[1]-t2[1] for t1,t2 in zip(births_all,deaths_all)]
028| 
029|     fig, ax = plt.subplots()
030|     fig.suptitle('Births & Deaths (Worldwide)')
031|     fig.set_figwidth(8.0)
032|     fig.set_figheight(6.0)
033|     fig.subplots_adjust(left=0.12, right=0.99, bottom=0.12, top=0.94)
034| 
035|     ax.set_xlabel('Year')
036|     ax.set_xlim(1950, 2030)
037|     ax.xaxis.set_major_locator(MultipleLocator(10))
038|     ax.xaxis.set_minor_locator(MultipleLocator(1))
039|     ax.xaxis.set_major_formatter(FormatStrFormatter('%d'))
040| 
041|     for tick in ax.xaxis.get_major_ticks():
042|         tick.label1.set_rotation('vertical')
043|         tick.label1.set_size(9)
044| 
045|     ax.set_ylabel('Millions of People per Year')
046|     ax.set_ylim(0.0, 150.0)
047|     ax.yaxis.set_major_locator(MultipleLocator(150.0))
048|     ax.yaxis.set_minor_locator(MultipleLocator(10.0))
049|     ax.yaxis.set_major_formatter(NullFormatter())
050|     ax.yaxis.set_minor_formatter(FormatStrFormatter('%+.2f'))
051| 
052|     ax.grid(which='major', axis='both', color='#7f7f7f', lw=1.2,ls='-',zorder=1, alpha=1.0)
053|     ax.grid(which='minor', axis='both', color='#bfbfbf', lw=0.6,ls='-',zorder=0, alpha=1.0)
054|     ax.tick_params(which='major', axis='both', direction='out', color='#000000')
055|     ax.tick_params(which='minor', axis='both', direction='out', color='#000000')
056| 
057|     ax.plot(xs,ys1, color='#0000ff', label='Births', lw=2.00, zorder=13, alpha=0.85)
058|     ax.plot(xs,ys2, color='#ff0000', label='Deaths', lw=2.00, zorder=14, alpha=0.85)
059|     ax.plot(xs,ys3, color='#007f00', label='Added!', lw=2.50, zorder=15, alpha=0.85)
060|     ax.legend(loc='upper left')
061| 
062|     fn = path.join(ImgsPath, Filename1)
063|     fig.savefig(fn)
064|     print('wrote: %s' % fn)
065|     print()
066| 
067| ##================================================================================================##
068| def chart2 (births_usa, deaths_usa):
069|     xs  = [t[0] for t in births_usa]
070|     ys1 = [t[1] for t in births_usa]
071|     ys2 = [t[1] for t in deaths_usa]
072|     ys3 = [t1[1]-t2[1] for t1,t2 in zip(births_usa,deaths_usa)]
073| 
074|     fig, ax = plt.subplots()
075|     fig.suptitle('Births & Deaths (USA)')
076|     fig.set_figwidth(8.0)
077|     fig.set_figheight(6.0)
078|     fig.subplots_adjust(left=0.12, right=0.99, bottom=0.12, top=0.94)
079| 
080|     ax.set_xlabel('Year')
081|     ax.set_xlim(1950, 2030)
082|     ax.xaxis.set_major_locator(MultipleLocator(10))
083|     ax.xaxis.set_minor_locator(MultipleLocator(1))
084|     ax.xaxis.set_major_formatter(FormatStrFormatter('%d'))
085| 
086|     for tick in ax.xaxis.get_major_ticks():
087|         tick.label1.set_rotation('vertical')
088|         tick.label1.set_size(9)
089| 
090|     ax.set_ylabel('Millions of People per Year')
091|     ax.set_ylim(0.0, 5.0)
092|     ax.yaxis.set_major_locator(MultipleLocator(1.0))
093|     ax.yaxis.set_minor_locator(MultipleLocator(0.5))
094|     ax.yaxis.set_major_formatter(NullFormatter())
095|     ax.yaxis.set_minor_formatter(FormatStrFormatter('%+.2f'))
096| 
097|     ax.grid(which='major', axis='both', color='#7f7f7f', lw=1.2,ls='-',zorder=1, alpha=1.0)
098|     ax.grid(which='minor', axis='both', color='#bfbfbf', lw=0.6,ls='-',zorder=0, alpha=1.0)
099|     ax.tick_params(which='major', axis='both', direction='out', color='#000000')
100|     ax.tick_params(which='minor', axis='both', direction='out', color='#000000')
101| 
102|     ax.plot(xs,ys1, color='#0000ff', label='Births', lw=2.00, zorder=13, alpha=0.85)
103|     ax.plot(xs,ys2, color='#ff0000', label='Deaths', lw=2.00, zorder=14, alpha=0.85)
104|     ax.plot(xs,ys3, color='#007f00', label='Added!', lw=2.50, zorder=15, alpha=0.85)
105|     ax.legend(loc='upper left')
106| 
107|     fn = path.join(ImgsPath, Filename2)
108|     fig.savefig(fn)
109|     print('wrote: %s' % fn)
110|     print()
111| 
112| ##================================================================================================##
113| def main ():
114|     # Load file one...
115|     fn = path.join(DataPath, DataFile1)
116|     fo = csvfile(fn)
117|     fo.load()
118| 
119|     births_all = [FData(t) for t in fo if t[0]=='World']
120|     births_usa = [FData(t) for t in fo if t[0]=='United States']
121| 
122|     # Load file two...
123|     fn = path.join(DataPath, DataFile2)
124|     fo = csvfile(fn)
125|     fo.load()
126| 
127|     deaths_all = [FData(t) for t in fo if t[0]=='World']
128|     deaths_usa = [FData(t) for t in fo if t[0]=='United States']
129| 
130|     # Report on data...
131|     print('#births/all: %d' % len(births_all))
132|     print('#deaths/all: %d' % len(deaths_all))
133|     print('#births/usa: %d' % len(births_usa))
134|     print('#deaths/usa: %d' % len(deaths_usa))
135|     print()
136| 
137|     # Create charts...
138|     chart1(births_all, deaths_all)
139|     chart2(births_usa, deaths_usa)
140| 
141| ####################################################################################################
142| if __name__ == '__main__':
143|     print(argv[0])
144|     print()
145|     main()
146|     print()
147| ####################################################################################################
148| '''eof'''
149|