-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_y_axis.py
101 lines (67 loc) · 2.33 KB
/
log_y_axis.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
"""
Display a demo graph with log y axis for teaching logarithm.
"""
from collections import defaultdict
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter
def load():
"""Load."""
data = defaultdict(lambda: defaultdict(int))
for year in range(2007, 2014):
with open(f'data/{year}_noi_00.csv', encoding='utf-8') as infile:
next(infile) # skip first line
for line in infile:
try:
_, name, freq = line.strip().split('\t')
except ValueError:
(_, name), freq = line.strip().split('\t'), '0'
name = name.strip('"')
name = name[0] + name[1:].lower()
data[year][name] = int(freq.replace(',', ''))
df = pd.DataFrame.from_dict(data, orient="index") # !
df.fillna(0, inplace=True) # !
return df
def sample(df):
"""Sample."""
names = [
'Borbála', 'Sarolta', 'Iringó', 'Villő', 'Franciska',
'Kata', 'Míra', 'Dalma', 'Veronika'
]
# https://stackoverflow.com/questions/30673684
# https://stackoverflow.com/questions/11285613
samples = [
df.iloc[:, list(range(0, 200, 39))], # !
df.loc[:, names] # !
]
return samples
def plot(dfs):
"""Plot."""
fig, axs = plt.subplots(2, 2, figsize=(9,10))
yticks = [20, 50, 100, 200, 500, 1000]
for a, df in zip(axs, dfs):
plain, logar = a
plain.set_title("sima")
df.plot(ax=plain, legend=0)
logar.set_title("logaritmikus")
logar.set_yscale("log")
logar.set_yticks(yticks)
logar.get_yaxis().set_major_formatter(ScalarFormatter()) # !
df.plot(ax=logar, legend=0)
lh = plain.get_legend_handles_labels()
plain.legend(*lh, bbox_to_anchor=[1, 1]) # !
fig.suptitle('újszülöttek névgyakorisága' +
' simán és logaritmikus tengelyen ábrázolva', fontsize=16)
plt.figtext(0.99, 0.002, '(c) sassbalint@gmail.com',
horizontalalignment='right')
plt.tight_layout() # !
fig.savefig('log_y_nevek.png')
def main():
"""Do the thing."""
dataframe = load()
samples = sample(dataframe)
for s in samples:
print(s.to_csv(sep='\t'))
plot(samples)
if __name__ == '__main__':
main()