-
-
Save XertroV/860b4acfaea8637d6d6f4981f8b13f22 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from collections import Counter, defaultdict | |
import requests | |
import sys | |
import matplotlib.pyplot as plt | |
def loads_and_parse(nums): | |
parse_f = lambda s : int(s.replace(',', '')) | |
return list(map(parse_f, json.loads(nums))) | |
def get_data(state_name): | |
ret = defaultdict(list) | |
counties = requests.get(f"https://static01.nyt.com/elections-assets/2020/data/api/2020-11-03/state-page/{state_name}.json").json()['data']['races'][0]['counties'] | |
for county in counties: | |
for cand, votes in county['results'].items(): | |
ret[cand].append(votes) | |
return ret | |
def first_digits_kvs(kvs): | |
k, vs = kvs | |
# return (k, Counter(list(map(lambda v: int(str(v)[0]), vs)))) | |
return (k, list(map(lambda v: int(str(v)[0]), vs))) | |
# from https://realpython.com/python-histograms/ | |
def ascii_histogram(seq) -> None: | |
"""A horizontal frequency-table/histogram plot.""" | |
for k in sorted(seq): | |
print('{0:5d} {1}'.format(k, '+' * seq[k])) | |
def heading(h): | |
print(f"\n\n{h} histogram") | |
def histogram_state(state, counts, only_two=True): | |
if only_two: | |
counts = dict(bidenj=counts['bidenj'], trumpd=counts['trumpd']) | |
plt.figure(figsize=([16,9])) | |
leg = list(counts.keys()) | |
print(leg) | |
zipped_cs = list(map(list, counts.values())) | |
print(zipped_cs) | |
n, bins, patches = plt.hist(zipped_cs, 9, density=True, histtype='bar', label=leg, color=['blue', 'red']) | |
plt.legend(prop=dict(size=10)) | |
# plt.grid(axis='y', alpha=0.75) | |
plt.xlabel('leading_digit') | |
plt.ylabel('frequency') | |
plt.title(f'benford histogram for {state}') | |
plt.savefig(f"b-out-{state}.png") | |
def print_graph(h, seq): | |
heading(h) | |
ascii_histogram(seq) | |
for state in ['illinois', 'nevada', 'wisconsin', 'washington', 'new-york', 'florida', 'arizona', 'georgia', 'wyoming', 'california', 'pennsylvania', 'ohio']: | |
try: | |
print(f"\n\n ## {state} ##\n") | |
cand_votes = get_data(state) | |
pairs = dict(map(first_digits_kvs, cand_votes.items())) | |
histogram_state(state, pairs) | |
# for k, v in pairs.items(): | |
# print_graph(k, v) | |
except Exception as e: | |
print(f'exception during state: {state}') | |
raise e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment