-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot.py
118 lines (84 loc) · 3.13 KB
/
plot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#======================================#
# BabyNamer #
# by Randall Arms #
# github.com/randallarms/babynamer #
#======================================#
# This program outputs a matplotlib/pyplot line graph of
# a name's popularity over the years, based on the SSA data.
# Requires matplotlib. Install with "pip install matplotlib"
# or through your own preferred installation method.
# IMPORTS
import matplotlib.pyplot as plt
import objectify
# MENU
# Greeting text
print("\n\n=========")
print("BABYNAMER")
print("=========")
print("Open source @ github.com/randallarms/babynamer")
# Loop/exit input variables
loop_input = "yes"
loop_continue = ["yes", "y", "continue"]
# An incredibly constraining list of genders that are unfortunately necessary for this query
genders = ["male", "female", "m", "f", "boy", "girl"]
gender_male = ["male", "m", "boy"]
gender_female = ["female", "f", "girl"]
# Plotting function
def plot_it():
# PROMPT
# Get the name
print("\nWhich name do you wish to plot the popularity of? (NOTE: case-sensitive!) ")
name = input("> ")
# Get the gender
print("\nWhich available gender do you wish to plot the popularity of? (Options: male, female) ")
gender = input("> ")
while not gender in genders:
print("\nPlease enter one of the available options provided: \'male\' or \'female\'")
print("\nWhich gender do you wish to plot the popularity of? (Options: male, female) ")
gender = input("> ")
# DATA
# All data as an object
__data__ = objectify.objectify()
y_coords_male = []
y_coords_female = []
x_coords = []
for __key__ in __data__['male'].keys():
x_coords.append(int(__key__))
if name in __data__['male'][__key__].values():
for rank, name_val in __data__['male'][__key__].items():
if name_val == name:
y_coords_male.append(1001-int(rank))
else:
y_coords_male.append(0)
for __key__ in __data__['female'].keys():
if name in __data__['female'][__key__].values():
for rank, name_val in __data__['female'][__key__].items():
if name_val == name:
y_coords_female.append(1001-int(rank))
else:
y_coords_female.append(0)
# PLOT
# Create line graph
plt.figure(num='Popularity of ' + name)
plt.title('Popularity of \"' + name + '\"')
# Plot the coords
if gender in gender_male:
y_coords = y_coords_male
if gender in gender_female:
y_coords = y_coords_female
plt.plot(x_coords, y_coords, marker=',')
# Build x, y axes
plt.xlabel('Year')
plt.ylabel('Rank')
plt.yticks([1000, 900, 800, 700, 600, 500, 400, 300, 200, 100, 1], ["#1", "#100", "#200", "#300", "#400", "#500", "#600", "#700", "#800", "#900", "#1000"])
# Build line graph
plt.grid(True)
# Display line graph
plt.show()
# Plotting loop
while loop_input in loop_continue:
# Plot it
plot_it()
# Prompt to continue
print("\nWould you like to look up another name? ")
loop_input = input("> ")