SHARE
TWEET

Untitled

a guest Mar 25th, 2019 142 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from csv import reader
  2. from collections import OrderedDict
  3. from datetime import datetime
  4. from matplotlib import pyplot
  5. from pprint import pprint
  6.  
  7.  
  8. def plot_dates_minutes(data):
  9.     sessions = [datetime.strptime(cell[0], '%m/%d/%Y %H:%M') for cell in data[1::]]
  10.     lengths = [int(cell[1]) for cell in data[1::]]
  11.     pyplot.bar(sessions, lengths)
  12.     pyplot.show()
  13.  
  14.  
  15. def plot_day(data):
  16.     sessions = OrderedDict.fromkeys(['Sunday',
  17.                                      'Monday',
  18.                                      'Tuesday',
  19.                                      'Wednesday',
  20.                                      'Thursday',
  21.                                      'Friday',
  22.                                      'Saturday'])
  23.     for row in data[1::]:
  24.         day_of_week = datetime.strptime(row[0], '%m/%d/%Y %H:%M')
  25.         if sessions[day_of_week.strftime('%A')]:
  26.             sessions[day_of_week.strftime('%A')] += int(row[1])
  27.         else:
  28.             sessions[day_of_week.strftime('%A')] = int(row[1])
  29.  
  30.     pyplot.bar(range(len(sessions)), sessions.values(), align='center')
  31.     pyplot.xticks(range(len(sessions)), list(sessions.keys()))
  32.     pyplot.show()
  33.  
  34.  
  35. def plot_years(data):
  36.     sessions = OrderedDict()
  37.     for row in data[1::]:
  38.         day_of_week = datetime.strptime(row[0], '%m/%d/%Y %H:%M')
  39.         if sessions.get(day_of_week.year):
  40.             sessions[day_of_week.year] += int(row[1])
  41.         else:
  42.             sessions[day_of_week.year] = int(row[1])
  43.  
  44.     pyplot.bar(range(len(sessions)), sessions.values(), align='center')
  45.     pyplot.xticks(range(len(sessions)), list(sessions.keys()))
  46.     pyplot.show()
  47.  
  48.  
  49. def plot_years_months_full(data):
  50.     # sessions = OrderedDict()
  51.     sessions = OrderedDict.fromkeys(['{:02d}/{}'.format(month, year) for year in range(14, 18)
  52.                                                                      for month in range(1, 13)])
  53.     for month in sessions:
  54.         sessions[month] = 0
  55.  
  56.     for row in data[1::]:
  57.         day_of_week = datetime.strptime(row[0], '%m/%d/%Y %H:%M')
  58.         month_day = day_of_week.strftime('%m/%y')
  59.         if sessions.get(month_day):
  60.             sessions[month_day] += int(row[1])
  61.         else:
  62.             sessions[month_day] = int(row[1])
  63.  
  64.     pyplot.bar(range(len(sessions)), sessions.values(), align='center')
  65.     pyplot.xticks(range(len(sessions)), list(sessions.keys()))
  66.     pyplot.show()
  67.  
  68.  
  69. def plot_and_show_years(data):
  70.     date_range = [datetime.strptime(cell[0], '%m/%d/%Y %H:%M') for cell in data[1::]]
  71.     years = [curdate.year for curdate in date_range]
  72.     year_set = sorted(set(years))
  73.     years_dict = OrderedDict.fromkeys(year_set)
  74.     for year in years_dict:
  75.         years_dict[year] = OrderedDict.fromkeys(['{:02d}'.format(month) for month in range(1, 13)])
  76.         for month in years_dict[year]:
  77.             years_dict[year][month] = 0
  78.  
  79.         for row in data[1::]:
  80.             current_date = datetime.strptime(row[0], '%m/%d/%Y %H:%M')
  81.             month_day = current_date.strftime('%m')
  82.             if current_date.year == year:
  83.                 years_dict[current_date.year][month_day] += int(row[1])
  84.  
  85.         pyplot.bar(range(len(years_dict[year])), years_dict[year].values(), align='center')
  86.         pyplot.title(year)
  87.         pyplot.ylabel('Meditation Minutes')
  88.         pyplot.xlabel('Month')
  89.         pyplot.xticks(range(len(years_dict[year])), list(years_dict[year].keys()))
  90.         pyplot.show()
  91.  
  92.  
  93. with open('insight_sessions_export.csv', 'r') as f:
  94.     sheet = list(reader(f))
  95.  
  96.  
  97. # plot_dates_minutes(sheet)
  98. # plot_day(sheet)
  99. # plot_years(sheet)
  100. # plot_years_months_full(sheet)
  101. plot_and_show_years(sheet)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top