-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
87 lines (73 loc) · 2.72 KB
/
test.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
import csv
import matplotlib.pyplot as plt
from datetime import datetime
import numpy as np
def open_data(file_name='trans.csv'):
with open('trans.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=';')
data = {}
for idx, row in enumerate(csv_reader):
if idx == 0:
# skip header
continue
date = datetime.strptime(row[0], '%Y-%m-%d')
company = row[3]
n_shares = row[4]
amount = float(row[6].replace(',', '.'))
if not(date.year in data):
data[date.year] = []
data[date.year].append({
'date': date,
'company': company,
'n_shares': n_shares,
'amount': amount
})
return data
def bar_representation(data):
bar_data = {}
for year in data:
for transaction in data[year]:
if not(transaction['date'].year in bar_data):
bar_data[transaction['date'].year] = [0] * 12
bar_data[transaction['date'].year][transaction['date'].month-1] += transaction['amount']
fig, ax = plt.subplots()
ind = np.arange(12) + 1
n_years = len(bar_data)
width = 1/(n_years+1)
years = sorted(bar_data.keys())
for idx, year in enumerate(years):
ax.bar(ind + idx*width, bar_data[year], width, label=year)
labels = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
ax.set_xticks(ind + width*n_years/2)
ax.set_xticklabels(labels)
plt.legend()
plt.show()
def get_cmap(n, name='hsv'):
'''Returns a function that maps each index in 0, 1, ..., n-1 to a distinct
RGB color; the keyword argument name must be a standard mpl colormap name.'''
return plt.cm.get_cmap(name, n)
def accumulated(data):
dates = []
accumulated = []
previous = 0
years = sorted(data.keys())
for year in years:
for transaction in reversed(data[year]):
previous += transaction['amount']
dates.append(transaction['date'])
accumulated.append(previous)
fig, ax = plt.subplots()
cmap = get_cmap(len(years))
for idx, year in enumerate(years):
ax.axvspan(
datetime.strptime(f'{year}-01-01', '%Y-%m-%d'), datetime.strptime(f'{year}-12-30', '%Y-%m-%d'), alpha=0.2, color=cmap(idx)
)
ax.plot(dates, accumulated, color='blue', linewidth=1)
ax.set_title('Accumulated dividends')
plt.show()
def organic_growth():
pass
if __name__ == "__main__":
data = open_data(file_name='trans.csv')
#bar_representation(data)
accumulated(data)