💾 Archived View for gem.sdf.org › jquah › extracurriculars › stackedArea.py captured on 2021-12-17 at 13:26:06.

View Raw

More Information

⬅️ Previous capture (2021-12-03)

-=-=-=-=-=-=-

#!/usr/bin/env python
# coding: utf-8

# In[1]:
import sys,re,math
MoDays = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
IncoWch = [0 for wk in range(53)]
IncoWui = [0 for wk in range(53)]
IncoWmc = [0 for wk in range(53)]
IncoWmd = [0 for wk in range(53)]
UtilW = [0 for wk in range(53)]
FoodW = [0 for wk in range(53)]
TrvlW = [0 for wk in range(53)]
AndrW = [0 for wk in range(53)]


# In[2]:
assets = ['bank0.txt','bank1.txt','cash.txt']
liabils = ['creditcard0.txt', 'creditcard1.txt']


# In[3]:
for file in assets:
        c = open(file, 'r')
        for line in c:
                workline=re.split('\t+',line)
                if len(workline) > 6:
                        thiscost=float(workline[6])
                else:
                        thiscost=0
                date=workline[0]
                month=date[4:6]
                domonth=date[6:8]
                doyear=float(domonth)
                fmonth=float(month)
                imonth=1
                while (imonth < fmonth):
                        doyear += MoDays[imonth-1]
                        imonth += 1
                week=int(doyear/7)
                if workline[2] not in ["-", "xfer", "eqty"]:
                    employer=workline[5]    # typical example is "Emp2 Salary"
                    employerShort=employer[3]
                    if employerShort == "0":
                        IncoW0[week] += thiscost
                    elif employerShort == "1":
                        IncoW1[week] += thiscost
                    elif employerShort == "2":
                        IncoW2[week] += thiscost
                    elif employerShort == "3":
                        IncoW3[week] += thiscost
                    else:
                        pass
                category=workline[1]
                if category == "util":
                        UtilW[week] += thiscost
                elif category == "food":
                        FoodW[week] += thiscost
                elif category == "trvl":
                        TrvlW[week] += thiscost
                elif category in ["wdrw", "xfer", "gift", "-", "eqty"]:
                        pass
                else:
                        AndrW[week] += thiscost

        c.close()


# In[4]:
for file in liabils:
        d = open(file, 'r')
        for line in d:
                workline=re.split('\t+',line)
                if len(workline) > 6:
                        thiscost=float(workline[6])
                else:
                        thiscost=0
                date=workline[0]
                month=date[4:6]
                domonth=date[6:8]
                doyear=float(domonth)
                fmonth=float(month)
                imonth=1
                while (imonth < fmonth):
                        doyear += MoDays[imonth-1]
                        imonth += 1
                week=int(doyear/7)
                category=workline[2]
                if category == "util":
                        UtilW[week] += thiscost
                elif category == "food":
                        FoodW[week] += thiscost
                elif category == "trvl":
                        TrvlW[week] += thiscost
                elif category in ["wdrw", "xfer", "gift", "-", "eqty"]:
                        pass
                else:
                        AndrW[week] += thiscost

        d.close()


# In[5]:
import numpy as np
import matplotlib.pyplot as plt
weekstoplot = np.arange(53)


# In[8]:
Inco0 = IncoW0[0]*np.arange(len(weekstoplot))
Inco1 = IncoW1[0]*np.arange(len(weekstoplot))
Inco2 = IncoW2[0]*np.arange(len(weekstoplot))
Inco3 = IncoW3[0]*np.arange(len(weekstoplot))
Util = UtilW[0]*np.arange(len(weekstoplot))
Food = FoodW[0]*np.arange(len(weekstoplot))
Trvl = TrvlW[0]*np.arange(len(weekstoplot))
Andr = AndrW[0]*np.arange(len(weekstoplot))
for week in range(len(weekstoplot)-1):
    Inco0[week+1]=Inco0[week]+IncoW0[week+1]
    Inco1[week+1]=Inco1[week]+IncoW1[week+1]
    Inco2[week+1]=Inco2[week]+IncoW2[week+1]
    Inco3[week+1]=Inco3[week]+IncoW3[week+1]
    Util[week+1]=Util[week]+UtilW[week+1]
    Food[week+1]=Food[week]+FoodW[week+1]
    Trvl[week+1]=Trvl[week]+TrvlW[week+1]
    Andr[week+1]=Andr[week]+AndrW[week+1]


# In[9]:
plt.stackplot(weekstoplot,[Util,Food,Andr],labels=['housing/utilities','food','other'],alpha=0.8)
plt.xlabel('weeks since Jan 1')
plt.ylabel('year-to-date expenses (dollars)')
plt.legend(loc=2, fontsize='large')
plt.savefig('YTDexpenses.png')
plt.show()


# In[10]:
plt.stackplot(weekstoplot,[Inco0,Inco1,Inco2,Inco3],labels=['Employer 0','Employer 1','Employer 2','Employer 3'],alpha=0.8)
plt.xlabel('weeks since Jan 1')
plt.ylabel('year-to-date income (dollars)')
plt.legend(loc=2, fontsize='large')
plt.savefig('YTDincome.png')
plt.show()