Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib.pyplot as plt
- import numpy as np
- from datetime import datetime
- import os, calendar
- #******************************************************************************
- def time_in_hours(time):
- return (time.hour + (time.minute / 60) + (time.second / 3600))
- #******************************************************************************
- themonth = int(input('Enter the month number(1-12): '))
- days = calendar.monthrange(2018, themonth)[1]
- monthlytotal = 0
- dailytotal = []
- for i in range(days):
- dailytotal.append(0)
- fig, ax = plt.subplots(figsize=(12, 6))
- plt.subplots_adjust(bottom = 0.15)
- logfile = open("log.txt","r")
- for idx, line in enumerate(logfile.readlines(), start=1):
- try:
- times = line.split(',')
- dt1 = datetime.strptime(times[0], '%m/%d/%Y %I:%M:%S %p')
- dt2 = datetime.strptime(times[1], '%m/%d/%Y %I:%M:%S %p')
- t1 = dt1.time()
- t2 = dt2.time()
- if dt1.month == themonth and dt2.month == themonth: # if Startup Time and Shutdown Time are in the same month
- if dt1.day == dt2.day: # same day
- starttime = time_in_hours(t1)
- endtime = time_in_hours(t2)
- duration = endtime - starttime
- ax.broken_barh([(starttime, duration)], ( ((dt1.day - 1)) , 1 ) )
- dailytotal[dt1.day - 1 ] += duration
- else: # same month, not the same day
- starttime = time_in_hours(t1)
- endtime = time_in_hours(t2)
- ax.broken_barh([(starttime, 24 - starttime)], ( ((dt1.day - 1)) , 1 ) ) # draw for day n
- dailytotal[dt1.day - 1 ] += (24 - starttime)
- ax.broken_barh([(0, endtime)], ( ((dt2.day - 1)) , 1 ) ) # draw for day n+1
- dailytotal[dt2.day - 1 ] += endtime
- elif dt1.month != themonth and dt2.month == themonth: # if Startup Time is from the previous month
- endtime = time_in_hours(t2)
- ax.broken_barh([(0, endtime)], ( ((dt2.day - 1)) , 1 ) )
- dailytotal[dt2.day - 1 ] += endtime
- elif dt1.month == themonth and dt2.month != themonth: # if Shutdown Time goes to the next month
- starttime = time_in_hours(t1)
- ax.broken_barh([(starttime, 24 - starttime)], ( ((dt1.day - 1)) , 1 ) )
- dailytotal[dt1.day - 1 ] += (24 - starttime)
- except Exception as e:
- print('error in line ' + str(idx) + ': ' + str(e))
- logfile.close()
- for i in range(1 , days + 1 ):
- ax.text(24.1, i - 0.1 , i)
- h = int(dailytotal[i-1])
- m = int((dailytotal[i-1] - h) * 60 )
- ax.text(24.8, i - 0.1 , format(h, '02d') + ':' + format(m, '02d'))
- monthlytotal += dailytotal[i-1]
- ax.text(21, days + 3 , 'Monthly total = ' + str(round(monthlytotal , 1)) )
- ax.text(21, days + 4 , 'Daily average = ' + str(round(monthlytotal / days , 1)) )
- #******************************************************************************
- ax.set_title('Times my lapotp was on in ' + calendar.month_name[themonth])
- ax.set_xlim(0, 24)
- ax.set_ylim(0, days)
- ax.set_xticks(np.arange(25))
- ax.set_xlabel('Time of day')
- ax.set_ylabel('Day of month')
- ax.set_yticks(np.arange(0 , days))
- ax.set_yticklabels('')
- #to make day numbers show between the ticks: set minor ticks between major ticks. add labels.
- ax.set_yticks(np.arange(0.5 , days ), minor=True)
- ax.set_yticklabels(np.arange(1, days+1) , minor=True)
- # hide minor ticks but show labels
- ax.tick_params(axis=u'y', which=u'minor',length=0)
- ax.invert_yaxis()
- ax.grid(True)
- plt.show()
- fig.savefig(os.path.basename(__file__) + '.png')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement