SHOW:
|
|
- or go back to the newest paste.
1 | import pandas as pd | |
2 | import matplotlib.pyplot as plt | |
3 | import numpy as np | |
4 | ||
5 | data = pd.read_csv("milwaukee_ward_votes.csv") | |
6 | ||
7 | def GetFirstDigit(num) -> int: | |
8 | first_digit = num | |
9 | while (first_digit >= 10): | |
10 | first_digit = first_digit // 10 | |
11 | return (1 if first_digit == 0 else first_digit) | |
12 | ||
13 | biden = [0] * 10 | |
14 | trump = [0] * 10 | |
15 | ||
16 | # get occurance data | |
17 | for index, row in data.iterrows(): | |
18 | biden_digit = GetFirstDigit(row['Joseph R. Biden / Kamala D. Harris']) | |
19 | trump_digit = GetFirstDigit(row['Donald J. Trump / Michael R. Pence']) | |
20 | ||
21 | biden[biden_digit] += 1 | |
22 | trump[trump_digit] += 1 | |
23 | ||
24 | ||
25 | for index in range(10): | |
26 | biden[index] = biden[index] / 478 | |
27 | trump[index] = trump[index] / 478 | |
28 | ||
29 | fig, ax = plt.subplots() | |
30 | plt.xlim((1, 9)) | |
31 | ||
32 | benford = [0, .301, .176, .125, .097, .079, .067, .058, .051, 0.046] | |
33 | ||
34 | ax.plot(biden, label='Biden', color='b') | |
35 | ax.plot(trump, label='Trump', color='r') | |
36 | ax.plot(benford, label='True Benford', color='y') | |
37 | ||
38 | ax.set_ylabel('Probability of Occurance') | |
39 | ax.set_xlabel('Most Significant Integer') | |
40 | ax.set_title('2020 Votes for Major Candidates vs Benford\'s Law') | |
41 | ax.legend() | |
42 | ||
43 | plt.show() |