X-Git-Url: https://asedeno.scripts.mit.edu/gitweb/?a=blobdiff_plain;f=bluechips%2Flib%2Fplotting.py;fp=bluechips%2Flib%2Fplotting.py;h=89c56fcd6eaab03a169fcc293ff19cdbad40f86c;hb=40658e2f4285a2253627a44b7f4bf26a358fe3b9;hp=0000000000000000000000000000000000000000;hpb=316ff50ff5fa2e6d2744bf397f840fdfcbaaac63;p=bluechips.git diff --git a/bluechips/lib/plotting.py b/bluechips/lib/plotting.py new file mode 100644 index 0000000..89c56fc --- /dev/null +++ b/bluechips/lib/plotting.py @@ -0,0 +1,32 @@ +""" +Generate various plots +""" +import matplotlib.pyplot as plt +import numpy as np + +from bluechips import model +from bluechips.model import meta, User, Expenditure + +def balance_plot(filename='balance', dates=None): + """ + Create a plot showing the net balance for each user over the + course of time. + + dates is a tuple of datetime objects. + """ + users = meta.Session.query(User).all() + expenses = meta.Session.query(Expenditure).order_by(Expenditure.date) + + totals = dict() + dates = [e.date for e in expenses] + for u in users: + totals[u.name] = np.cumsum([e.share(u) for e in expenses]) + + fig = plt.figure() + ax = fig.add_subplot(1,1,1) + lines = dict() + for u,y in totals.items(): + lines[u] = plt.plot(dates,totals[u], label=u) + + ax.legend(loc='best') + fig.savefig(filename)