US_elections_2000s.html
001|
002| from sys import argv
003| from os import path
004| from mlchart import mlchart
005|
006| BasePath = r'C:\CJS\prj\Python\blog\proj'
007| ImgsPath = path.join(BasePath,'img')
008|
009|
010| data = [
011| (2000, 282.2, ('Al Gore' , 50_999_897), ('George Bush' , 50_456_002)),
012| (2004, 292.8, ('John Kerry' , 59_028_444), ('George Bush' , 62_040_610)),
013| (2008, 304.1, ('Barack Obama' , 69_498_516), ('John McCain' , 59_948_323)),
014| (2012, 313.9, ('Barack Obama' , 65_915_795), ('Mitt Romney' , 60_933_504)),
015| (2016, 323.1, ('Hillary Clinton', 65_853_514), ('Donald Trump', 62_984_828)),
016| (2020, 329.5, ('Joe Biden' , 81_283_501), ('Donald Trump', 74_223_975)),
017| (2024, 345.0, ('Kamala Harris' , 74_312_576), ('Donald Trump', 76_823_951)),
018| ]
019|
020|
021| class election_votes_chart (mlchart):
022|
023| def __init__ (self, data=None, **kwargs):
024| super().__init__((8.0, 4.0), **kwargs)
025| self.data = data
026| self.title = 'US Presidential Elections in the 2000s'
027| self.x_title = 'Election Year'
028| self.y_title = 'Millions of Votes'
029| self.x_limit = (1998, 2026)
030| self.y_limit = (0, 90)
031| self.x_grids = (4, 1)
032| self.y_grids = (10, 2)
033| self.x_tickf = '%d'
034| self.y_tickf = '%d'
035| self.x_tickr = True
036| self.x_dates = False
037| self.title_kw['fontsize'] = 16
038| self.title_kw['fontweight'] = 'bold'
039| self.x_title_kw['fontsize'] = 12
040| self.y_title_kw['fontsize'] = 12
041| self.fpath = ImgsPath
042| self.fname = 'USA Elections 2000s.png'
043| self.margins = dict(left=0.07, right=0.99, bottom=0.17, top=0.93)
044|
045| def plotter (self):
046| xs1 = self.data[0]
047| ys1 = self.data[1]
048| xs2 = self.data[2]
049| ys2 = self.data[3]
050| xs3 = self.data[4]
051| ys3 = self.data[5]
052| self.ax.bar(xs1,ys1, label='Dems', color='#0000ff', width=2.0, alpha=0.85, zorder=13)
053| self.ax.bar(xs2,ys2, label='Reps', color='#ff0000', width=2.0, alpha=0.85, zorder=12)
054| self.ax.plot(xs3,ys3, label='Pop', c='#000000', lw=3.0, alpha=0.66, zorder=11)
055|
056|
057|
058| class election_percentages_chart (mlchart):
059|
060| def __init__ (self, data=None, **kwargs):
061| super().__init__((8.0, 4.0), **kwargs)
062| self.data = data
063| self.title = 'US Presidential Elections in the 2000s'
064| self.x_title = 'Election Year'
065| self.y_title = 'Percentage of Population'
066| self.x_limit = (1998, 2026)
067| self.y_limit = (0, 0.30)
068| self.x_grids = (4, 1)
069| self.y_grids = (0.1, 0.01)
070| self.x_tickf = '%d'
071| self.y_tickf = '%.2f'
072| self.x_tickr = True
073| self.x_dates = False
074| self.title_kw['fontsize'] = 16
075| self.title_kw['fontweight'] = 'bold'
076| self.x_title_kw['fontsize'] = 12
077| self.y_title_kw['fontsize'] = 12
078| self.fpath = ImgsPath
079| self.fname = 'USA Election Percentage.png'
080| self.margins = dict(left=0.09, right=0.99, bottom=0.17, top=0.93)
081|
082| def plotter (self):
083| xs1 = self.data[0]
084| ys1 = self.data[1]
085| xs2 = self.data[2]
086| ys2 = self.data[3]
087| xs3 = self.data[4]
088| ys3 = self.data[5]
089| self.ax.bar(xs1,ys1, label='Dems', color='#0000ff', width=2.0, alpha=0.85, zorder=13)
090| self.ax.bar(xs2,ys2, label='Reps', color='#ff0000', width=2.0, alpha=0.85, zorder=12)
091| self.ax.plot(xs3,ys3, label='Pop', c='#000000', lw=3.0, alpha=0.66, zorder=11)
092|
093|
094|
095| def main ():
096| xs1 = [t[0]-0.5 for t in data]
097| xs2 = [t[0]+0.5 for t in data]
098| xs3 = [t[0] for t in data]
099|
100| ys1 = [t[2][1]/1e6 for t in data]
101| ys2 = [t[3][1]/1e6 for t in data]
102| ys3 = [t[1] for t in data]
103|
104| chart_data = (xs1, ys1, xs2, ys2, xs3, ys3)
105|
106| cht = election_votes_chart(chart_data)
107| cht.make()
108| print('wrote: %s' % path.join(cht.fpath, cht.fname))
109| print()
110|
111| ys1 = [y/p for y,p in zip(ys1,ys3)]
112| ys2 = [y/p for y,p in zip(ys2,ys3)]
113|
114| chart_data = (xs1, ys1, xs2, ys2, xs3, ys3)
115|
116| cht = election_percentages_chart(chart_data)
117| cht.make()
118| print('wrote: %s' % path.join(cht.fpath, cht.fname))
119| print()
120|
121|
122| if __name__ == '__main__':
123| print(argv[0])
124| print()
125| main()
126| print()
127|
128| '''eof'''
129|