plot.py (1878B)
1 import datetime 2 from collections import defaultdict 3 4 import matplotlib 5 matplotlib.use('Qt5Agg') 6 import matplotlib.pyplot as plt 7 import matplotlib.dates as md 8 9 from .i18n import _ 10 from .bitcoin import COIN 11 12 13 class NothingToPlotException(Exception): 14 def __str__(self): 15 return _("Nothing to plot.") 16 17 18 def plot_history(history): 19 if len(history) == 0: 20 raise NothingToPlotException() 21 hist_in = defaultdict(int) 22 hist_out = defaultdict(int) 23 for item in history: 24 if not item['confirmations']: 25 continue 26 if item['timestamp'] is None: 27 continue 28 value = item['value'].value/COIN 29 date = item['date'] 30 datenum = int(md.date2num(datetime.date(date.year, date.month, 1))) 31 if value > 0: 32 hist_in[datenum] += value 33 else: 34 hist_out[datenum] -= value 35 36 f, axarr = plt.subplots(2, sharex=True) 37 plt.subplots_adjust(bottom=0.2) 38 plt.xticks( rotation=25 ) 39 ax = plt.gca() 40 plt.ylabel('BTC') 41 plt.xlabel('Month') 42 xfmt = md.DateFormatter('%Y-%m-%d') 43 ax.xaxis.set_major_formatter(xfmt) 44 axarr[0].set_title('Monthly Volume') 45 xfmt = md.DateFormatter('%Y-%m') 46 ax.xaxis.set_major_formatter(xfmt) 47 width = 20 48 49 r1 = None 50 r2 = None 51 dates_values = list(zip(*sorted(hist_in.items()))) 52 if dates_values and len(dates_values) == 2: 53 dates, values = dates_values 54 r1 = axarr[0].bar(dates, values, width, label='incoming') 55 axarr[0].legend(loc='upper left') 56 dates_values = list(zip(*sorted(hist_out.items()))) 57 if dates_values and len(dates_values) == 2: 58 dates, values = dates_values 59 r2 = axarr[1].bar(dates, values, width, color='r', label='outgoing') 60 axarr[1].legend(loc='upper left') 61 if r1 is None and r2 is None: 62 raise NothingToPlotException() 63 return plt