def myTest(): xdata = [ "2018-01-02", "2018-01-03", "2018-01-04", "2018-01-05", "2018-01-06", "2018-01-07", "2018-01-08", "2018-01-09", "2018-01-10", "2018-01-11", "2018-01-12" ] # xdata = [712588., 712688., # 712788., 712888., # 712988., 713088., # 713188., 713288., # 713388., 713488., # 713588.,] ydata = [ 5424.455042071198, 5437.436073513513, 5443.326118918919, 5453.535032397408, 5465.99996972973, 5470.846144864865, 5442.501206486487, 5329.997431351351, 5311.448544864865, 5312.012034594594, 5312.620194384449 ] rule = rrulewrapper(MINUTELY, byeaster=1, interval=5) loc = RRuleLocator(rule) formatter = DateFormatter('%Y-%m-%d') fig = plt.figure() ax = fig.add_subplot(1, 1, 1) xx = [datetime.strptime(x, '%Y-%m-%d') for x in xdata] print(xx) plt.plot_date(xx, ydata) ax.xaxis.set_major_locator(mdates.DayLocator(interval=1)) ax.xaxis.set_major_formatter(formatter) plt.gcf().autofmt_xdate() plt.show()
def build_gantt(self, filename, title): '''builds the chart from self.objects''' num = len(self.objects) pos = np.arange(0.5,num*0.5+0.5,0.5) fig_height = 5 + num * 0.25 fig = plt.figure(figsize=(20,fig_height)) ax = fig.add_subplot(111) ylabels = [] i = 0 for obj in self.objects: starttime, endtime, uid, color = obj start = matplotlib.dates.date2num(starttime) end = matplotlib.dates.date2num(endtime) ylabels.append(uid) ax.barh((i*0.5)+0.5, end - start, left=start, height=0.3, align='center', edgecolor='darkorange', color=color, alpha = 0.8) i+=1 locsy, labelsy = plt.yticks(pos, ylabels) plt.setp(labelsy, fontsize = 14) ax.set_ylim(ymin = -0.1, ymax = num*0.5+0.5) ax.grid(color = 'g', linestyle = ':') ax.xaxis_date() #rule = rrulewrapper(WEEKLY, interval=1) rule = rrulewrapper(WEEKLY, interval=1) loc = RRuleLocator(rule) formatter = DateFormatter("%d-%b") ax.xaxis.set_major_locator(loc) ax.xaxis.set_major_formatter(formatter) labelsx = ax.get_xticklabels() plt.setp(labelsx, rotation=30, fontsize=10) font = font_manager.FontProperties(size='small') ax.legend(loc=1,prop=font) ax.invert_yaxis() fig.autofmt_xdate() plt.title(title) plt.savefig(filename)
def plot_temperature_roomsche_twingraph(logfile, room_sche, t, pinterval, pstep, oat, room_zt, room_tsa): fig = plt.figure() # tfig = fig.add_subplot(111) # rsfig = tfig.twinx() rsfig = fig.add_subplot(111) tfig = rsfig.twinx() ind = t # np.arange(len(room_sche[0])) width = 0.02 # num_room = len(room_sche) colors = ('r', 'g', 'b', 'y') t_labels = ('R1', 'R2', 'R3', 'R4') labels = ('R1', 'R2', 'R3', 'R4') # t_labels = ('LRLC', 'LRHC', 'HRLC', 'HRHC') # labels = ('LRLC', 'LRHC', 'HRLC', 'HRHC') inputd = [] for i in xrange(len(room_sche)): inputd.append(room_sche[i]) data = np.array(inputd) bottom = np.vstack((np.zeros((data.shape[1],), dtype=data.dtype), np.cumsum(data, axis=0)[:-1])) for dat, col, lab, bot in zip(data, colors, labels, bottom): plt.bar(ind, dat, width, color=col, label=lab, bottom=bot) tfig.plot(ind, oat, 'k', label='Outdoor') for i in xrange(len(room_zt)): tfig.plot(ind, room_zt[i], colors[i], label=t_labels[i]) tfig.plot(ind, [21]*len(ind), 'k--', label='Comfort Bounds') tfig.plot(ind, [23]*len(ind), 'k--') tfig.grid(True) tfig.set_ylabel("Temperature (Celcius)") rsfig.axes.set_yticks([]) rsfig.set_xlabel("Scheduling Periods") rsfig.set_ylabel("# of Meetings") handles, labels = tfig.get_legend_handles_labels() fontP = FontProperties() fontP.set_size('small') rsfig.legend(handles, labels, ncol=2, loc='best', prop=fontP) ymdhFmt = mdates.DateFormatter('%Y-%m-%d %H:%M') rule_1 = rrulewrapper(_getPlotRule((int)(pinterval)), interval=(int)(pstep)) loc_1 = RRuleLocator(rule_1) rsfig.xaxis.set_major_locator(loc_1) rsfig.xaxis.set_major_formatter(ymdhFmt) datemin = datetime(min(t).year, min(t).month, min(t).day, min(t).hour, min(t).minute) datemax = datetime(max(t).year, max(t).month, max(t).day, max(t).hour, max(t).minute) rsfig.set_xlim(datemin, datemax) fig.autofmt_xdate() plt.savefig('Output\\'+ logfile +'_temperature_ralloc.png') # plt.show() plt.close(fig) print "Done!"
def CreateGanttChart(my_dic, first, last): """ Create gantt charts with matplotlib Give file name. """ pos = np.arange(0.5, (len(my_dic) * 0.5) + 0.5, 0.5) fig = plt.figure(figsize=(100, 30)) ax = fig.add_subplot(111) cont = 0 for item in my_dic.items(): cont = cont + 1 n = len(item[1]['end_date']) for counter in range(0, n): curr_barh = ax.barh(((cont - 1) * 0.5) + 0.5, item[1]['width'][counter], left=item[1]['pos'][counter], height=0.2, align='center', color='#80aaff', alpha=0.8)[0] text = item[1]['label'][counter] # Center the text vertically in the bar yloc = curr_barh.get_y() + (curr_barh.get_height() / 2.0) ax.text(item[1]['pos'][counter] + (item[1]['width'][counter]) / 2.0, yloc, text, fontsize=9, horizontalalignment='center', verticalalignment='center', color='white', weight='bold', clip_on=True) locsy, labelsy = plt.yticks(pos, my_dic.keys()) plt.setp(labelsy, fontsize=10) ax.set_xlabel('Hours') ax.set_xticks(np.arange(np.floor(first), np.ceil(last), 1)) ax.set_ylim(ymin=-0.05, ymax=len(my_dic.keys()) * 0.5 + 0.5) ax.grid(color='g', linestyle=':') rule = rrulewrapper(WEEKLY, interval=1) loc = RRuleLocator(rule) # formatter = DateFormatter("%d-%b") font = font_manager.FontProperties(size='small') ax.legend(loc=1, prop=font) ax.invert_yaxis() fig.autofmt_xdate() fig.tight_layout(rect=[0.027, 0.069, 0.984, 0.981], h_pad=0.2, w_pad=0.2) plt.savefig('gantt.svg') plt.show()
def cleanup_axis_datetime(axis, rule=None): if rule is None: rule = rrulewrapper(SECONDLY, interval=1) loc = RRuleLocator(rule) fmt = DateFormatter('%M:%S') axis.set_major_locator(loc) axis.set_major_formatter(fmt) return axis
def __drawPlots(self, counter, **kwargs): import datetime if kwargs['rrulewrapperFrequency'] == 'DAILY': rrulewrapperFrequency = DAILY elif kwargs['rrulewrapperFrequency'] == 'MONTHLY': rrulewrapperFrequency = MONTHLY elif kwargs['rrulewrapperFrequency'] == 'YEARLY': rrulewrapperFrequency = YEARLY rule = rrulewrapper(rrulewrapperFrequency, interval = kwargs['rrulewrapperInterval']) loc = RRuleLocator(rule) formatter = DateFormatter('%Y/%m/%d') plt.style.use("seaborn-whitegrid") fig, ax = plt.subplots() keys = counter.keys() if len(keys) == 1: dates = [datetime.date(*[int(item) for item in date.split('-')]) for date in counter['total']['dates']] counts = counter['total']['counts'] plt.plot_date(dates, counts, linestyle = '-', label = 'total') else: if kwargs['includeTotal']: dates = [datetime.date(*[int(item) for item in date.split('-')]) for date in counter['total']['dates']] counts = counter['total']['counts'] plt.plot_date(dates, counts, linestyle = '-', label = 'total') for key in sorted(keys): if key == 'total': continue dates = [datetime.date(*[int(item) for item in date.split('-')]) for date in counter[key]['dates']] counts = counter[key]['counts'] plt.plot_date(dates, counts, linestyle = '-', label = key) plt.title('project = \'%s\', componentType = \'%s\' -- Generated on %s' % (kwargs['project'], kwargs['componentType'], kwargs['startTime'])) plt.xlabel('Date [a.u.]') plt.ylabel('Frequency [a.u.]') plt.legend() ax.xaxis.set_major_locator(loc) ax.xaxis.set_major_formatter(formatter) # if kwargs['lowerDate'] != None: # plt.gca().set_xlim(left = datetime.date(*[int(item) for item in kwargs['lowerDate'].split('-')])) # if kwargs['upperDate'] != None: # plt.gca().set_xlim(right = datetime.date(*[int(item) for item in kwargs['upperDate'].split('-')])) if kwargs['savePath'][-4:] == '.pdf': plt.savefig(kwargs['savePath']) elif kwargs['savePath'][-4:] == '.png': plt.savefig(kwargs['savePath'], dpi = 150) if kwargs['saveJson']: counter['kwargs'] = kwargs import json with open(kwargs['savePath'][0:-4] + '.json', 'w') as file: json.dump(counter, file, sort_keys = True, indent = 4, separators = (',', ': '))
def get_locator(self, dmin, dmax): # get the recommended tick locations from AutoDateLocator, then # go one level more numerous ticks self.old_refresh() major_freq = self._freq tick_locations = self._old_locator.tick_values(dmin, dmax) tick_dt = tick_locations[1] - tick_locations[0] # tick_dt is in terms of days. convert it to major_freq dt = self._freqconverter[major_freq](tick_dt) # Check each possible interval of self._freq minor_interval = 0 minor_interval_index = 0 minor_freq = major_freq for i, interval in enumerate(self.intervald[major_freq]): if dt - .5 > interval: minor_interval = interval minor_interval_index = i else: break if minor_interval == 0: # Then we need to go to the next smaller time interval minor_freq = self.next_freq(major_freq) minor_interval = self.intervald[minor_freq][-3] minor_interval_index = len(self.intervald[minor_freq]) - 3 byranges = [None, 1, 1, 0, 0, 0, None] for i, freq in enumerate(self._freqs): if freq != minor_freq: byranges[i] = None else: break minor_freq_index = self.freq_to_index(minor_freq) byranges[minor_freq_index] = self._byranges[minor_freq_index] if minor_freq != self._freqs[-1]: _, bymonth, bymonthday, byhour, byminute, bysecond, _ = byranges rrule = rrulewrapper(minor_freq, interval=minor_interval, dtstart=dmin, until=dmax, bymonth=bymonth, bymonthday=bymonthday, byhour=byhour, byminute=byminute, bysecond=bysecond) locator = RRuleLocator(rrule, self.tz) else: locator = MicrosecondLocator(minor_interval, tz=self.tz) locator.set_axis(self.axis) locator.set_view_interval(*self.axis.get_view_interval()) locator.set_data_interval(*self.axis.get_data_interval()) return locator
def plotTemperatureNAirMassZoom(pinterval, pstep, t, OT, ZT1, TSA1, ASA1, ZT2, TSA2, ASA2, start, end): # ymdhFmt = mdates.DateFormatter('%H:%M') ymdhFmt = mdates.DateFormatter('%Y-%m-%d %H:%M') rc('mathtext', fontset='stixsans') fig = plt.figure() ztfig = fig.add_subplot(111) ztfig.plot(t, ZT1, 'k--', label='T (S)') ztfig.set_xlabel("Scheduling Period", fontsize=12) ztfig.set_ylabel("Temperature ($^\circ$C)", fontsize=12) # ztfig.set_ylim(ymin=-2, ymax=maxT+5) ztfig_fontP = FontProperties() ztfig_fontP.set_size(13) #option 1: # ztfig_fontP.set_weight('bold') ztfig_handles, ztfig_labels = ztfig.get_legend_handles_labels() # ztfig.legend(ztfig_handles, ztfig_labels, loc='best', prop=ztfig_fontP) ztfig.legend(ztfig_handles, ztfig_labels, loc='best', ncol=3, prop=ztfig_fontP) #option 2: # box = ztfig.get_position() # ztfig.set_position([box.x0, box.y0 + box.height * 0.1, box.width, box.height * 0.9]) # ztfig.legend(loc='upper center', bbox_to_anchor=(0.4, 1.05), ncol=3, prop=ztfig_fontP) # # ztfig.legend(loc='upper center', bbox_to_anchor=(0.5, 1.15), fancybox=True, shadow=False, ncol=3, prop=ztfig_fontP) ztfig_rule_1 = rrulewrapper(_getPlotRule((int)(pinterval)), interval=(int)(pstep)) ztfig_loc_1 = RRuleLocator(ztfig_rule_1) ztfig.xaxis.set_major_locator(ztfig_loc_1) ztfig.xaxis.set_major_formatter(ymdhFmt) ztfig_datemin = datetime( min(t).year, min(t).month, min(t).day, min(t).hour, min(t).minute) ztfig_datemax = datetime( max(t).year, max(t).month, max(t).day, max(t).hour, max(t).minute) ztfig.set_xlim(ztfig_datemin, ztfig_datemax) ztfig.grid(True) fig.autofmt_xdate() plt.tight_layout() # plt.savefig('Output\\2_LRHC_1600_400dpi.png', dpi=400) plt.show()
def CreateGanttChart(fname): """Create gantt charts with matplotlib Give file name. """ ylabels = [] customDates = [] textlist = open(fname).readlines() for tx in textlist: if not tx.startswith('#'): ylabel, startdate, enddate = tx.split(',') ylabels.append(ylabel.replace('\n', '')) customDates.append([ _create_date(startdate.replace('\n', '')), _create_date(enddate.replace('\n', '')) ]) ilen = len(ylabels) pos = np.arange(0.5, ilen * 0.5 + 0.5, 0.5) task_dates = {} for i, task in enumerate(ylabels): task_dates[task] = customDates[i] fig = plt.figure(figsize=(20, 8)) ax = fig.add_subplot(111) for i in range(len(ylabels)): start_date, end_date = task_dates[ylabels[i]] ax.barh((i * 0.5) + 0.5, end_date - start_date, left=start_date, height=0.3, align='center', edgecolor='lightgreen', color='orange', alpha=0.8) locsy, labelsy = plt.yticks(pos, ylabels) plt.setp(labelsy, fontsize=14) # ax.axis('tight') ax.set_ylim(ymin=-0.1, ymax=ilen * 0.5 + 0.5) ax.grid(color='g', linestyle=':') ax.xaxis_date() rule = rrulewrapper(WEEKLY, interval=1) loc = RRuleLocator(rule) # formatter = DateFormatter("%d-%b '%y") formatter = DateFormatter("%d-%b") ax.xaxis.set_major_locator(loc) ax.xaxis.set_major_formatter(formatter) labelsx = ax.get_xticklabels() plt.setp(labelsx, rotation=30, fontsize=10) font = font_manager.FontProperties(size='small') ax.legend(loc=1, prop=font) ax.invert_yaxis() fig.autofmt_xdate() plt.savefig(fname + '.svg') plt.show()
def build(self, filename, title): '''builds the chart from self.objects''' #determine the min and max latitude across all objects overall_minlat = min([x[2] for x in self.objects]) overall_maxlat = max([x[3] for x in self.objects]) print('overall minmax: {} {}'.format(overall_minlat, overall_maxlat)) lat_height = overall_maxlat - overall_minlat height_multiplier = 2.0 num = len(self.objects) #pos = np.arange(0.5, lat_height, 0.5) fig_height = height_multiplier * lat_height fig = plt.figure(figsize=(20, fig_height)) ax = fig.add_subplot(111) #ylabels = [float('%.4g' % x) for x in np.arange(overall_minlat, overall_maxlat, 0.5)] #ylabels = ylabels.extend(float('%.4g' % overall_maxlat)) ylabels = np.linspace(overall_minlat, overall_maxlat, num=int(lat_height * height_multiplier), endpoint=True) ylabels = [float('%.4g' % x) for x in ylabels] pos = [float(x) - overall_minlat for x in ylabels] for obj in self.objects: starttime, endtime, minlat, maxlat, uid, color = obj start = matplotlib.dates.date2num(starttime) end = matplotlib.dates.date2num(endtime) element_height = (maxlat - minlat) yposition = (minlat - overall_minlat) ax.barh(yposition, end - start, left=start, height=element_height, align='edge', edgecolor='darkorange', color=color, alpha=0.5) locsy, labelsy = plt.yticks(pos, ylabels) plt.setp(labelsy, fontsize=14) #plt.gca().invert_yaxis() ax.set_ylim(ymin=0.0, ymax=lat_height) ax.grid(color='g', linestyle=':') ax.xaxis_date() #rule = rrulewrapper(WEEKLY, interval=1) rule = rrulewrapper(MONTHLY, interval=1) loc = RRuleLocator(rule) formatter = DateFormatter("%Y-%m-%d") ax.xaxis.set_major_locator(loc) ax.xaxis.set_major_formatter(formatter) labelsx = ax.get_xticklabels() plt.setp(labelsx, rotation=30, fontsize=10) font = font_manager.FontProperties(size='small') ax.legend(loc=1, prop=font) #ax.invert_yaxis() fig.autofmt_xdate() plt.title(title) plt.savefig(filename)
def draw(data: Data): available_colors = [c for c in matplotlib.colors.TABLEAU_COLORS] colors = {} print("PIDs found:") for pid in data.list_pids(): l = data.get_measures(pid) m = l[0] if m.cmd not in colors: i = len(colors) % len(available_colors) c = available_colors[i] colors[m.cmd] = c print(f"- {m.pid} {colors[m.cmd]} {m.cmd}") cpu_ax = pyplot.subplot(2, 1, 1) cpu_ax.title.set_text("CPU in percent") cpu_ax.set_ylabel("cpu%") mem_ax = pyplot.subplot(2, 1, 2, sharex=cpu_ax) mem_ax.title.set_text("Memory in percent") mem_ax.set_ylabel("memory%") mem_ax.set_xlabel("datetime") # tick every 5th easter rule = rrulewrapper(YEARLY, byeaster=1, interval=5) loc = RRuleLocator(rule) formatter = DateFormatter("%m/%d/%y") mem_ax.xaxis.set_major_locator(loc) mem_ax.xaxis.set_major_formatter(formatter) mem_ax.xaxis.set_tick_params(rotation=30, labelsize=10) cpu_ax.xaxis.set_visible(False) handles = [] for pid in data.list_pids(): l = data.get_measures(pid) cmd = l[0].cmd color = colors[cmd] handles.append(matplotlib.patches.Patch(color=color, label=pid)) dates = [p.date for p in l] cpu = [p.cpu for p in l] mem = [p.mem for p in l] cpu_ax.plot_date(dates, cpu, linestyle="-", marker="", color=color, label=cmd) mem_ax.plot_date(dates, mem, linestyle="-", marker="", color=color, label=cmd) mem_ax.legend(handles=handles) pyplot.show()
def _config_plot(days_interval, minutes_interval, start_date, end_date, x_label, yLabel, titulo, lim_y, max_var, interval_yticker, figsize, dias_corridos=0): ax = plt.gca() plt.xlim(start_date, end_date) plt.ylim(lim_y[0], lim_y[1]) plt.xlabel(x_label, fontsize=20) plt.ylabel(yLabel, fontsize=20) plt.title(f'{titulo} [{dias_corridos} Dias]', fontsize=20) plt.grid(True) ax.yaxis.set_major_locator(ticker.MultipleLocator(interval_yticker)) rule = rrulewrapper(DAILY, interval=days_interval) minor_rule = rrulewrapper(MINUTELY, interval=minutes_interval) legenda_tag = [] loc = RRuleLocator(rule) formatter = DateFormatter('%d-%m-%Y') minor_loc = RRuleLocator(minor_rule) minor_formatter = DateFormatter('%H:%M') if (days_interval < 2): ax.xaxis.set_major_locator(loc) ax.xaxis.set_major_formatter(formatter) ax.xaxis.set_tick_params(which='major', rotation=90, pad=60) ax.xaxis.set_minor_locator(minor_loc) ax.xaxis.set_minor_formatter(minor_formatter) ax.xaxis.set_tick_params(which='minor', rotation=90) else: ax.xaxis.set_major_locator(loc) ax.xaxis.set_major_formatter(formatter) ax.xaxis.set_tick_params(which='major', rotation=90, pad=0)
def SetDateAxis(ax, form): if "year" in form: formatter = DateFormatter('%Y') rule = rrulewrapper(YEARLY, byeaster=1, interval=5) loc = RRuleLocator(rule) if "month" in form: formatter = DateFormatter('%Y-%m') loc = MonthLocator(interval=4) ax.xaxis.set_major_locator(loc) ax.xaxis.set_major_formatter(formatter) plt.xticks(rotation=20) plt.tight_layout()
def run(subreddits): for subreddit in subreddits: c = Counter() fName = f'{subreddit}/{subreddit}.csv' if not os.path.isfile(fName) : continue print (subreddit) # open('test.csv','rU'), encoding='utf-8', engine='c' records = pd.read_csv(fName, names = ["author","subreddit", "id", "title", "time","score","num_comments","domain","url",'selftext'],engine='c') #Count on same day for index, row in records.iterrows(): if index==0: continue #Headers c[ datetime.strptime(row["time"],'%Y-%m-%d %H:%M:%S').strftime("%y/%m/%d")] += 1 #Convert to numpy temp = np.array([ [datetime.strptime(r,"%y/%m/%d"), c[r]] for r in c ] ) temp = temp[temp[:,0].argsort()] #Sort #Start plot plt.style.use('ggplot') fig, ax = plt.subplots() ax.xaxis_date() ax.xaxis.set_major_locator(RRuleLocator(rrulewrapper(MONTHLY, bymonthday=15))) ax.xaxis.set_major_formatter(DateFormatter('%m/%d/%Y')) ax.xaxis.set_tick_params(rotation=90, labelsize=8) #Plot with smoothness # dateTimeToNumber = np.vectorize( lambda dt: (dt - datetime(1970,1,1)).total_seconds() ) # numberToDateTime = np.vectorize( lambda num: datetime.utcfromtimestamp(num) ) # numeric_x = dateTimeToNumber(temp[:,0] ) # f = interp1d( numeric_x, temp[:,1] , kind='nearest') # x_range = np.linspace(numeric_x[0], numeric_x[-1],500) # y_smooth= f(x_range) # plt.plot ( numberToDateTime(x_range) ,y_smooth, "b",linewidth=2 ) #Plot Lines # plt.plot( temp[:,0],temp[:,1], "o-", markersize=1, linewidth=0.5 ) #Plot bars plt.bar( temp[:,0],temp[:,1], width=1 ) #Draw region >2018 # ax.fill_between(temp[:,0], 0, temp[:,1].max(), where=temp[:,0]>datetime(2018,1,1), facecolor='green', alpha=0.25) #Axes and labels # plt.xlabel('mm/dd/yy') plt.ylabel('# Posts') plt.title(f'# of Posts related with latinos/hispanic in r/{subreddit}') plt.grid(True) plt.tight_layout() #Plot # plt.show() plt.savefig(f'figs/{subreddit}.png')
def plotOutdoor(self, pinterval, pstep, oatdat, pcfg, Tinit, Toccmin, ToccminRange, Toccmax, ToccRange, xtime, oat): t = xtime OT = oat.values() min_occ1 = [21] * len(t) max_occ1 = [23] * len(t) min_occ2 = [22] * len(t) max_occ2 = [24] * len(t) ymdhFmt = mdates.DateFormatter('%Y-%m-%d %H:%M') fig = plt.figure() ztfig = fig.add_subplot(111) # ztfig.set_title(" \nOutdoorTemp: " + oatdat + "\nInitial Temp: [" + str(Tinit) + 'C]\n Occ Zone Temp Bound: [' + # str(Toccmin+ToccminRange) + 'C $\leq$ T $\leq$ ' + str(Toccmax-ToccRange) + ']\n UnOcc Zone Temp Bound: [' + # str(Toccmin) + 'C $\leq$ T $\leq$ ' + str(Toccmax) + # 'C]', fontsize=12) ztfig.plot(xtime, OT, 'r-', label='Outdoor Temperature') ztfig.plot(xtime, min_occ1, 'g-', label='Min Occupied Zone Temperature 21C') ztfig.plot(xtime, max_occ1, 'b-', label='Max Occupied Zone Temperature 23C') ztfig.plot(xtime, min_occ2, 'g--', label='Min Occupied Zone Temperature 22C') ztfig.plot(xtime, max_occ2, 'b--', label='Max Occupied Zone Temperature 24') ztfig.set_xlabel("Scheduling Period",fontsize=12) ztfig.set_ylabel("Celcius",fontsize=12) handles, labels = ztfig.get_legend_handles_labels() fontP = FontProperties() fontP.set_size('small') ztfig.legend(handles, labels, loc='best', prop=fontP) ztfig_rule_1 = rrulewrapper(self._getPlotRule((int)(pinterval)), interval=(int)(pstep)) ztfig_loc_1 = RRuleLocator(ztfig_rule_1) ztfig.xaxis.set_major_locator(ztfig_loc_1) ztfig.xaxis.set_major_formatter(ymdhFmt) ztfig_datemin = datetime(min(t).year, min(t).month, min(t).day, min(t).hour, min(t).minute) ztfig_datemax = datetime(max(t).year, max(t).month, max(t).day, max(t).hour, max(t).minute) ztfig.set_xlim(ztfig_datemin, ztfig_datemax) fig.autofmt_xdate() plt.tight_layout() plt.savefig('Output\\OutdoorTemperature_' + datetime.now().strftime('%Y_%m_%d_%H_%M_%S_%f') +'.png') # plt.show() plt.close(fig)
def printTimeSeries( CDF4Variable, timeseries, fileName='/Users/viniciussantino/Documents/PhD/Code/figure1.pdf', labelsArray=['CNDC', 'TEMP', 'PRES_REL', 'DOX1_3', 'CPHL', 'TURB']): timeData = num2date(timeseries[-1, :], CDF4Variable.variables['TIME'].units) fig1 = figure(num=None, figsize=(25, 20), dpi=80, facecolor='w', edgecolor='k') for i in range(0, len(labelsArray)): ax1 = subplot((len(labelsArray) + 1) / 2, 2, i + 1) plot(timeData, timeseries[i, :]) title( #DF4Variable.title + '\n' + #'%0.2f m depth\n'% TEMP.sensor_depth + #'location: lat=%0.2f; lon=%0.2f' % (CDF4Variable.variables['LATITUDE'][:], # CDF4Variable.variables['LONGITUDE'][:]) CDF4Variable.variables[labelsArray[i]].long_name) xlabel(CDF4Variable.variables['TIME'].long_name) ylabel(CDF4Variable.variables[labelsArray[i]].units) rule = rrulewrapper(DAILY, interval=60) formatter = DateFormatter('%d/%m/%y') loc = RRuleLocator(rule) ax1.xaxis.set_major_locator(loc) ax1.xaxis.set_major_formatter(formatter) labels = ax1.get_xticklabels() setp(labels, rotation=30, fontsize=10) show() fig1.savefig(fileName, dpi=None, facecolor='w', edgecolor='w', orientation='portrait', papertype=None, format='png', transparent=True, bbox_inches=None, pad_inches=0.1, frameon=None)
def plot_data(dates, code_stock): # Plot the data ax = plt.gca() code_stockplot = {} for code, stock in sorted(code_stock.items()): stock = code_stock[code] stockplot = StockPlot(code) line, = ax.plot_date(dates, stock.prices, label=code, linestyle='solid', marker='None', picker=5) stockplot.plot_line = line color = line.get_color() line, = ax.plot_date(stock.buy_dates, stock.buy_rates, marker='o', markersize=5, color=color) stockplot.buy_line = line line, = ax.plot_date(stock.sell_dates, stock.sell_rates, marker='s', markersize=5, color=color) stockplot.sell_line = line line = ax.axhline(y=stock.cost_price, color=color, alpha=0.7) stockplot.cost_line = line code_stockplot[code] = stockplot # Format dates on x-axis. rule = rrulewrapper(MONTHLY, interval=2) loc = RRuleLocator(rule) date_format = "%d-%b-%y" formatter = DateFormatter(date_format) ax.xaxis.set_major_locator(loc) ax.xaxis.set_major_formatter(formatter) ax.xaxis.set_tick_params(rotation=30, labelsize=10) # Other settings - tight axes, grid, title. ax.set_xlim(dates[0], dates[-1]) ax.grid(b=True, axis='y') ax.set_title('Stock Price History') return code_stockplot
def __showChart(self, chartData): figure1 = pyplot.figure() ax = figure1.add_subplot(111) for i in range(len(chartData.getYLabels())): startDate, endDate = chartData.getTaskDates()[ chartData.getYLabels()[i]] ax.barh((i * 0.5) + 0.5, endDate - startDate, left=startDate, height=0.3, align='center', edgecolor='lightblue', color='blue', alpha=1) iLen = len(chartData.getYLabels()) pos = np.arange(0.5, iLen * 0.5 + 0.5, 0.5) locsy, labelsy = pyplot.yticks(pos, chartData.getYLabels()) pyplot.setp(labelsy, fontsize=8) ax.set_ylim(bottom=-0.1, top=iLen * 0.5 + 0.5) ax.grid(color='lightblue', linestyle=':') ax.xaxis_date() ax.xaxis.set_major_locator( RRuleLocator(rrulewrapper(MONTHLY, interval=4))) ax.xaxis.set_major_formatter(DateFormatter("%d-%b-%Y")) pyplot.setp(ax.get_xticklabels(), rotation=30, fontsize=8) font = font_manager.FontProperties(size='small') #ax.legend(loc=1,prop=font) ax.invert_yaxis() figure1.autofmt_xdate() # Works only on Windows. # pyplot.get_current_fig_manager().window.state('zoomed') pyplot.show()
def wasm_text_rotated(): # tick every 5th easter np.random.seed(42) rule = rrulewrapper(YEARLY, byeaster=1, interval=5) loc = RRuleLocator(rule) formatter = DateFormatter("%m/%d/%y") date1 = datetime.date(1952, 1, 1) date2 = datetime.date(2004, 4, 12) delta = datetime.timedelta(days=100) dates = drange(date1, date2, delta) s = np.random.rand(len(dates)) # make up some random y values fig, ax = plt.subplots() plt.plot_date(dates, s) ax.xaxis.set_major_locator(loc) ax.xaxis.set_major_formatter(formatter) labels = ax.get_xticklabels() plt.setp(labels, rotation=30, fontsize=10) plt.show() plt.close("all") plt.clf()
def example(): # Fixing random state for reproducibility np.random.seed(19680801) # tick every 5th easter rule = rrulewrapper(YEARLY, byeaster=1, interval=5) loc = RRuleLocator(rule) formatter = DateFormatter('%m/%d/%y') date1 = datetime.date(1952, 1, 1) date2 = datetime.date(2004, 4, 12) delta = datetime.timedelta(days=100) dates = drange(date1, date2, delta) print(dates) s = np.random.rand(len(dates)) # make up some random y values fig, ax = plt.subplots() plt.plot_date(dates, s) ax.xaxis.set_major_locator(loc) ax.xaxis.set_major_formatter(formatter) ax.xaxis.set_tick_params(rotation=75, labelsize=10) plt.show()
async def plot(ctx): dates = [] people = [] with open('edge_data.csv', "r", encoding="utf-8", errors="ignore") as scraped: reader = csv.reader(scraped, delimiter=",") for row in reader: if row: # avoid blank lines people.append(row[0]) time = datetime.strptime(row[1], '%Y-%m-%d %H:%M:%S.%f') dates.append(time) rule = rrulewrapper(YEARLY, interval=24) loc = RRuleLocator(rule) formatter = DateFormatter('%m/%d/%y') fig, ax = plt.subplots() plt.plot_date(dates, people) ax.xaxis.set_major_locator(loc) ax.xaxis.set_major_formatter(formatter) ax.xaxis.set_tick_params(rotation=30, labelsize=10) imagename = 'graphs/{}.png'.format(uuid.uuid1()) plt.savefig(imagename) await ctx.send(file=discord.File(imagename))
ax3 = subplot(234) plot(profileIndex,lonCruise) xlabel('Profile Index') ylabel(xbt_DATA.variables['LONGITUDE'].long_name + ' in ' + xbt_DATA.variables['LONGITUDE'].units) #plot the LAT timeseries ax4 = subplot(235) plot(profileIndex,latCruise) xlabel('Profile Index') ylabel(xbt_DATA.variables['LATITUDE'].long_name + ' in ' + xbt_DATA.variables['LATITUDE'].units) #plot the profile index with time values # create the time label ticks rule = rrulewrapper(MONTHLY, bymonthday=1, interval=1) formatter = DateFormatter('%d/%m/%y') loc = RRuleLocator(rule) ax5 = subplot(236) plot(timeCruise,profileIndex) ax5.xaxis.set_major_locator(loc) ax5.xaxis.set_major_formatter(formatter) labels = ax5.get_xticklabels() setp(labels, rotation=30, fontsize=10) xlabel(TIME.long_name + ' in ' + 'dd/mm/yy' ) ylabel('Profile Index') # plot of a single profile of this cruise profileToPlot = 1 # this is arbitrary. We can plot all profiles from 1 to ncol, modify profileToPlot if desired figure2 = figure(num=None, figsize=(13, 9.2), dpi=80, facecolor='w', edgecolor='k') plot (tempCruise[:,profileToPlot],-depthCruise[:,profileToPlot])
def get_locator(self, dmin, dmax): 'Pick the best locator based on a distance.' delta = relativedelta(dmax, dmin) numYears = (delta.years * 1.0) numMonths = (numYears * 12.0) + delta.months numDays = (numMonths * 31.0) + delta.days numHours = (numDays * 24.0) + delta.hours numMinutes = (numHours * 60.0) + delta.minutes numSeconds = (numMinutes * 60.0) + delta.seconds # numticks = 5 # Difference to original AutoDateLocator: less ticks numticks = self.numticks # self._freq = YEARLY interval = 1 bymonth = 1 bymonthday = 1 byhour = 0 byminute = 0 bysecond = 0 if (numYears >= numticks): self._freq = YEARLY interval = int(numYears // numticks) elif (numMonths >= numticks): self._freq = MONTHLY bymonth = range(1, 13) interval = int(numMonths // numticks) elif (numDays >= numticks): self._freq = DAILY bymonth = None bymonthday = range(1, 32) interval = int(numDays // numticks) elif (numHours >= numticks): self._freq = HOURLY bymonth = None bymonthday = None byhour = range(0, 24) # show every hour interval = int(numHours // numticks) elif (numMinutes >= numticks): self._freq = MINUTELY bymonth = None bymonthday = None byhour = None byminute = range(0, 60) interval = int(numMinutes // numticks) # end if elif (numSeconds >= numticks): self._freq = SECONDLY bymonth = None bymonthday = None byhour = None byminute = None bysecond = range(0, 60) interval = int(numSeconds // numticks) # end if else: # do what? # microseconds as floats, but floats from what reference point? pass rrule = rrulewrapper(self._freq, interval=interval, dtstart=dmin, until=dmax, bymonth=bymonth, bymonthday=bymonthday, byhour=byhour, byminute=byminute, bysecond=bysecond) locator = RRuleLocator(rrule, self.tz) locator.set_axis(self.axis) locator.set_view_interval(*self.axis.get_view_interval()) locator.set_data_interval(*self.axis.get_data_interval()) return locator
def plot_data_chart(time_tracks, time_interval): """ Uses fdsn to find availave stations in a specific geographical location and time period. Parameters ---------- time_tracks: pkl Pickel file outputed by preprocessor or mseed_predictor. time_interval: int Time interval in hours for tick spaces in xaxes. Returns ---------- data_chart.png: fig """ with open(time_tracks, 'rb') as f: time_tracks = pickle.load(f) def create_date(date_time): date = dt.datetime(int(str(date_time).split('T')[0].split('-')[0]), int(str(date_time).split('T')[0].split('-')[1]), int(str(date_time).split('T')[0].split('-')[2]), int(str(date_time).split('T')[1].split(':')[0]), int(str(date_time).split('T')[1].split(':')[1])) mdate = matplotlib.dates.date2num(date) return mdate ylabels, customDates, task_dates = [], [], {} for st, tracks in time_tracks.items(): ylabels.append(st) time_slots, comp_types = [], [] for times in tracks[0]: time_slots.append((create_date(times[0]), create_date(times[1]) - create_date(times[0]))) for comps in tracks[1]: comp_types.append(comps) task_dates[st] = [time_slots, comp_types] customDates.append(time_slots) fig, ax = plt.subplots(figsize=(8, 10)) ax.patch.set_facecolor('lavender') # use a colormap cmap = plt.cm.Blues barHeight = len(ylabels) / 3 ticklist = [] def drawLoadDuration(period, starty, compt, c1, c2, c3): if len(compt) >= 1: if compt[0] == 3: if c3 == 0: ax.broken_barh((period), (starty, barHeight), facecolors='crimson', lw=0, zorder=2, alpha=0.9, label='3-component') c3 += 1 else: ax.broken_barh((period), (starty, barHeight), facecolors='crimson', lw=0, zorder=2, alpha=0.9) if compt[0] == 1: if c1 == 0: ax.broken_barh((period), (starty, barHeight), facecolors='mediumslateblue', lw=0, zorder=2, alpha=0.9, label='1-component') c1 += 1 else: ax.broken_barh((period), (starty, barHeight), facecolors='mediumslateblue', lw=0, zorder=2, alpha=0.9) if compt[0] == 2: if c2 == 0: ax.broken_barh((period), (starty, barHeight), facecolors='darkorange', lw=0, zorder=2, alpha=0.9, label='2-component') c2 += 1 else: ax.broken_barh((period), (starty, barHeight), facecolors='darkorange', lw=0, zorder=2, alpha=0.9) ticklist.append(starty + barHeight / 2.0) return c1, c2, c3 h0 = 3 c1 = 0 c2 = 0 c3 = 0 for st in ylabels: c1, c2, c3 = drawLoadDuration(task_dates[st][0], h0, task_dates[st][1], c1, c2, c3) h0 += int(len(ylabels) * 2) legend_properties = {'weight': 'bold'} box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.8, box.height]) plt.legend(loc='center left', bbox_to_anchor=(1, 0.5)) ax.set_ylim(0, h0) ax.set_xlabel('Time', fontsize=12) ax.set_ylabel('Stations', fontsize=12) ax.set_yticks(ticklist) ax.tick_params('x', colors=cmap(1.)) ax.set_yticklabels(ylabels, fontsize=10) ax.grid(True) ax.xaxis_date() #Tell matplotlib that these are dates... rule = rrulewrapper(HOURLY, interval=time_interval) loc = RRuleLocator(rule) formatter = DateFormatter('%Y-%m-%d %H') ax.xaxis.set_major_locator(loc) ax.xaxis.set_major_formatter(formatter) labelsx = ax.get_xticklabels() plt.setp(labelsx, rotation=30, fontsize=10) # Format the legend font = font_manager.FontProperties(size='small') ax.legend(loc=1, prop=font) plt.locator_params(axis='x', nbins=10) fig.autofmt_xdate() fig.tight_layout() plt.savefig('data_chart.png', dpi=300) plt.show()
ax = fig.add_subplot(111) # add barh to current subplot ax barh_i = 0 for task_name, (start_date, end_date, color) in tasks_d.items(): start_num, end_num = map(date2num, (start_date, end_date)) ax.barh((barh_i * 0.5) + 0.5, end_num - start_num + 1, left=start_num, height=0.3, align='center', color=color, alpha=0.8) barh_i += 1 # create y ticks with tasks labels pos = np.arange(0.5, len(tasks_d) * 0.5 + 0.5, 0.5) plt.yticks(pos, tasks_d.keys()) # x axis job ax.xaxis.set_major_locator(RRuleLocator(rrulewrapper(DAILY, interval=1))) ax.xaxis.set_major_formatter(DateFormatter('%d-%b-%y')) fig.autofmt_xdate() # y axis job ax.set_ylim(ymin=-0.1, ymax=len(tasks_d) * 0.5 + 0.5) ax.invert_yaxis() # add grid ax.grid(linestyle=':') # add title plt.title(prj_name) # output # plt.savefig('out.svg')
def plot_full(df, variables_array, start_date, end_date, **params): """ Descrição: Função responsável por apresentar o gráfico com as seguintes características: - Delimitação de período a ser apresentado; - Escolha das variáveis; - Uma curva de tendência utilizando uma filtro de média móvel (personalizado); - Personalização das fontes; - Marcação de períodos com blocos personalizados; - Marcação de dias; - Plot do erro Absoluto (entre duas variaveis especificadas); - Salve do gráfico; - Salve do csv do dataframe delimitado; ===================================================================== Parâmetros: df: DataFrame a ser plotado (obrigatório) variables_array: Variáveis do DataFrame a serem plotados (obrigatório) start_date: Início do período a ser plotado (obrigatório) end_date: Fim do período a ser plotado (obrigatório) ====================================================================== **params (Foi implementado essa forma de parâmetro **kwargs onde terá a possibilidade de passar um dicionários com os parametros a seres escolhidos. Esse conjuntos de parametros são todos opcionais): ylabel: Descrição da label para o eixo y (Default: '') xlabel: Descrição da label para o eixo x (Default: "Data") titulo: Título para o gráfico (Default: '') (É utilizado um combinação dos nomes das colunas)) days_interval: Intervalo de dias que será plotado (Default: 2) minutes_interval: Intervalo de minutos que será plotado (Default: 180) save_figure_name: Nome do arquivo da imagem do gráfico que será salvo (Default: '') error: Erro entre duas variáveis (Default: False) abs_error Erro absoluto entre duas variáveis (Default: False) rolling_time: Janela do filtro de média móvel a ser calculada(Default: 0) figure_heigh: Altura da figura do gráfico (Default: 10) figure_width: Comprimento da figura do gráfico (Default: 30) ticks: Ticks do eixo Y a serem personalizado (Default: []) Ex: [0, 1, 0.1] ==>> eixo y == [0. , 0.1, 0.2, ... 0.8, 0.9, 1. ] tendencia: Plota a curva de cada variável e sua respectiva tendência (Default.: False) label_font_size: Tamanho da fonte da label (eixos) (Default: 18) label_font_weight Espessura da fonte do label (eixos) (Default: 'bold') title_font_weight: Espessura da fonte do título (Default: 'bold') tick_font_weight: Espessura da fonte do tick (Default: 'bold') tick_font_size: Tamanho da fonte da tick (Default: 14) legenda_font_size Tamanho da fonte da legenda (Default: 16) treino_true_data: Janela do treino limpo para ser destacada (Ex.: [data_ini:str, data_fim:str]) treino_true_color: Cor do bloco de treino limpo (Ex.: 'green') treino_true_alpha: Quantidade de transparencia do bloco de treino limpo (Default.: 0.3) treino_true_label: Legenda do bloco de treino limpo (Default.: 'Período de Treino Limpo) treino_false_data: Janelas do treino sujo para ser destacada (Ex.: [(data_ini:str, data_fim:str)]) treino_false_color: Cor do bloco de treino sujo (Ex.: 'red') treino_false_alpha: Quantidade de transparência do bloco de treino sujo (Default.: 0.3) treino_false_label: Legenda do bloco de treino sujo (Default.: 'Período de Treino Sujo) bloco_destaque_data: Janelas do bloco extra para ser destacada (Ex.: [(data_ini:str, data_fim:str)]) bloco_destaque_color: Cor do bloco extra (Ex.: 'yellow') bloco_destaque_label: Legenda do bloco extra (Default.: 'Período Extra') bloco_destaque_alpha: Quantidade de transparencia do bloco extra (Default.: 0.3) dia_destaque_data: Datas a serem destacada (Ex.:[data]) dia_destaque_color: Cor da data destacada (Default: 'red') dia_destaque_label: Descrição da data destacada (Default: 'Dia Destaque') reta_tendencia_data: Períodos das retas de tendências a serem plotadas (Ex.: [(data_ini, data_fim)]) reta_tendencia_color: Cores a serem definidas para cada reta de tendência reta_tendencia_label: Descrição na legenda de cada reta de tendência (Default: Reta de Tendência (Tag)) reta_tendencia_ordem: Descrição da ordem do FIT de todas as linhas de tendência (Default: 1) interval_yticker: Intervalo inteiro no qual será distribuído os valores do eixo Y show_legenda: Mostrar a legenda (Default:True) label_legenda: Descrição da label para a legenda (Default: nome da variavel) linestyle: Estilo da linha (Default: '-') Ex:'-' ==> ______ | ':' => ...... | '-.' ==> --.--. | '--' ==> ------ marker: Marcação do ponto no gráfico (Default:'o') Ex: '.' ',' 's' '*' ... linewidth: Espessura da linha (Default: 1.5) hotinzontal treino_true_data, treino_false_data,bloco_destaque_data: adicione h na variavel --> treino_true_h_data ============================================================================== Exemplo de uso (Exemplo passando apenas os parâmetros obrigatórios): params = { "treino_true_data": [(date_ini_limpo,date_fim_limpo )], "treino_false_data":[(date_ini_sujo,date_fim_sujo )], "bloco_destaque_data":[(date_ini_bloco,date_fim_bloco )], "dia_destaque_data":[date_ini_limpo], "reta_tendencia_data": [(data_reta_tendencia_ini, data_reta_tendencia_fim)], "reta_tendencia_color": ['red'] } plt_variables_full(dataframe_tags, dataframe.columns, dataframe_tags.index.min(), dataframe.index.max(), **params) Obs.: o uso do **kwargs tem a possibilidade do uso de um dicionário com os parâmetros, ou também pela a forma convencional (passando cada um por vez) """ #Recuperando parametros de **params ylabel = params.get("ylabel", '') xlabel = params.get("xlabel", "Data") titulo = params.get("titulo", '') days_interval = params.get("days_interval", 2) minutes_interval = params.get("minutes_interval", 180) save_figure_name = params.get("save_figure_name", '') error = params.get("error", False) rolling_time = params.get("rolling_time", 0) abs_error = params.get("abs_error", False) figure_heigh = params.get("figure_heigh", 10) figure_width = params.get("figure_width", 30) ticks = params.get("ticks", []) tendencia = params.get("tendencia", False) show_legenda = params.get("show_legenda", True) label_legenda = params.get("label_legenda", '') leg_labels = label_legenda #Recuperando parametros de estilização do gráfico label_font_size = params.get("label_font_size", 18) label_font_weight = params.get("label_font_weight", "bold") title_font_size = params.get("title_font_size", 20) title_font_weight = params.get("title_font_weight", "bold") tick_font_weight = params.get("tick_font_weight", "bold") tick_font_size = params.get("tick_font_size", 14) legenda_font_size = params.get("legenda_font_size", 16) reta_tendencia_data = params.get("reta_tendencia_data", []) reta_tendencia_color = params.get("reta_tendencia_color", ['darkmagenta']) reta_tendencia_label = params.get("reta_tendencia_label", ["Reta de Tendência"]) reta_tendencia_ordem = params.get("reta_tendencia_ordem", [1]) interval_yticker = params.get("interval_yticker") linestyle = params.get("linestyle", '-') marker = params.get("marker", "o") linewidth = params.get("linewidth", 1.5) df_filtered = df[(df.index > start_date) & (df.index < end_date)] print(df_filtered.shape) for variable in variables_array: if 'MODE' in variable: df_filtered[variable] = df_filtered[variable].apply( lambda d: 1 if d == "CAS" else 0) rule = rrulewrapper(DAILY, interval=days_interval) minor_rule = rrulewrapper(MINUTELY, interval=minutes_interval) legenda_tag = [] loc = RRuleLocator(rule) formatter = DateFormatter('%d-%m-%Y') minor_loc = RRuleLocator(minor_rule) minor_formatter = DateFormatter('%H:%M') fig, ax = plt.subplots() xs = {} series = {} smask = {} df_return = pd.DataFrame() if (error): if (label_legenda == ''): label_legenda = 'Erro' if (abs_error): error_abs = abs(df_filtered[variables_array[0]] - df_filtered[variables_array[1]]) if (rolling_time > 0): plot, = plt.plot_date(df_filtered.index, error_abs.rolling(rolling_time).mean(), linestyle=linestyle, marker=marker, linewidth=linewidth, label=f'{label_legenda}') else: plot, = plt.plot_date(df_filtered.index, error_abs, linestyle=linestyle, marker=marker, label="Erro") legenda_tag.append(plot) else: error_ = df_filtered[variables_array[0]] - df_filtered[ variables_array[1]] if (rolling_time > 0): plot_legenda, = plt.plot_date( df_filtered.index, error_.rolling(rolling_time).mean(), linestyle=linestyle, marker=marker, linewidth=linewidth, label="Erro Tendência") legenda_tag.append(plot_legenda) else: plot, = plt.plot_date(df_filtered.index, error_, linestyle=linestyle, marker=marker, linewidth=linewidth, label="Erro") legenda_tag.append(plot) else: k = 0 for variable in variables_array: if (label_legenda == ''): label_legenda = variable else: if (len(variables_array) == len(leg_labels)): label_legenda = leg_labels[k] else: if (len(variables_array) > 1): label_legenda = variable else: label_legenda = leg_labels[k] k += 1 if (tendencia): plot, = plt.plot_date(df_filtered.index, df_filtered[variable], linestyle=linestyle, marker=marker, linewidth=linewidth, label=label_legenda) legenda_tag.append(plot) if (rolling_time > 0): plt_tendencia, = plt.plot_date( df_filtered.index, df_filtered[variable].rolling(rolling_time).mean(), linestyle=linestyle, marker=marker, linewidth=linewidth, label=f'{label_legenda} (Tendência)') legenda_tag.append(plt_tendencia) else: if (rolling_time > 0): plt_tendencia, = plt.plot_date( df_filtered.index, df_filtered[variable].rolling(rolling_time).mean(), linestyle=linestyle, marker=marker, linewidth=linewidth, label=f'{label_legenda}') legenda_tag.append(plt_tendencia) else: plot, = plt.plot_date(df_filtered.index, df_filtered[variable], linestyle=linestyle, marker=marker, linewidth=linewidth, label=label_legenda) legenda_tag.append(plot) if (len(titulo) > 0): plt.title(titulo, fontsize=title_font_size, fontweight=title_font_weight) elif (error): plt.title('Error', fontsize=title_font_size, fontweight=title_font_weight) else: plt.title(' - '.join(variables_array), fontsize=title_font_size, fontweight=title_font_weight) ax.tick_params(labelsize=tick_font_size) plt.rcParams['figure.titleweight'] = tick_font_weight plt.rcParams['font.weight'] = tick_font_weight # Adicionando da Reta de tendência legenda_reta_tendencia = [] if (reta_tendencia_data): for col in variables_array: i = 0 for reta in reta_tendencia_data: ini = pd.Timestamp(reta[0]) fim = pd.Timestamp(reta[1]) if (ini > fim): raise (ValueError( f'O o período para o fit da reda de tendência da variável {col} está com a Data Início maior que Data Fim.' )) df_filtrado = df_filtered[(df_filtered.index > ini) & (df_filtered.index < fim)] x = np.arange(len(df_filtrado)) y = df_filtrado[col].values coefs = np.polyfit(x, y, reta_tendencia_ordem[i]) ffit = np.poly1d(coefs) df_filtrado['ffit'] = ffit(x) legenda, = plt.plot(df_filtrado.index, df_filtrado['ffit'], color=reta_tendencia_color[i], linestyle='--', linewidth=4, label=f'{reta_tendencia_label[i]}') legenda_reta_tendencia.append(legenda) i += 1 if (len(ticks) > 2): ax.set_yticks(np.arange(ticks[0], ticks[1] + ticks[2], ticks[2])) if (interval_yticker): ax.yaxis.set_major_locator(ticker.MultipleLocator(interval_yticker)) if (days_interval < 2): ax.xaxis.set_major_locator(loc) ax.xaxis.set_major_formatter(formatter) ax.xaxis.set_tick_params(which='major', rotation=90, pad=60) ax.xaxis.set_minor_locator(minor_loc) ax.xaxis.set_minor_formatter(minor_formatter) ax.xaxis.set_tick_params(which='minor', rotation=90) else: ax.xaxis.set_major_locator(loc) ax.xaxis.set_major_formatter(formatter) ax.xaxis.set_tick_params(which='major', rotation=90, pad=0) fig.set_figheight(figure_heigh) fig.set_figwidth(figure_width) plt.grid(True) if (len(ylabel) > 0): plt.ylabel(ylabel, fontsize=label_font_size, fontweight=label_font_weight) if (len(xlabel) > 0): plt.xlabel(xlabel, fontsize=label_font_size, fontweight=label_font_weight) #Dia Destaque legenda_data_line = [] dia_destaque_data = params.get("dia_destaque_data", []) if (len(dia_destaque_data) > 0): color_dia = params.get("dia_destaque_color", 'r') label_dia = params.get('dia_destaque_label', "Dia Destaque") for dia in dia_destaque_data: plt.axvline(x=dia, color=color_dia, lw=2) line_limpeza = Line2D([0], [0], color=color_dia, lw=2) line_limpeza.set_label(label_dia) legenda_data_line.append(line_limpeza) # Data Blocos (True, False, Bloco destaque) legenda_bloco = [] tipos_blocos = ["treino_true", "treino_false", "bloco_destaque"] dic_bloco = { "treino_true": ["green", "Treino (limpo)"], "treino_false": ["red", "Treino (sujo)"], "bloco_destaque": ["yellow", "Bloco Destaque"] } for bloco in tipos_blocos: bloco_data = params.get(f"{bloco}_data") if (bloco_data and len(bloco_data) > 0): color_bloco = params.get(f'{bloco}_color', dic_bloco[bloco][0]) label_bloco = params.get(f'{bloco}_label', f'Período de {dic_bloco[bloco][1]}') alpha_bloco = params.get(f'{bloco}_alpha', 0.2) for data in bloco_data: ini = pd.Timestamp(data[0]) fim = pd.Timestamp(data[1]) if (ini > fim): raise (ValueError( f'O parametro {bloco} está com a Data Início maior que Data Fim.' )) plt.axvspan(ini, fim, color=color_bloco, alpha=alpha_bloco) legenda = mpatches.Patch(color=color_bloco, label=label_bloco, alpha=alpha_bloco) legenda_bloco.append(legenda) legenda_bloco_h = [] tipos_blocos = ["treino_true_h", "treino_false_h", "bloco_destaque_h"] dic_bloco = { "treino_true_h": ["green", "Treino (limpo)"], "treino_false_h": ["red", "Treino (sujo)"], "bloco_destaque_h": ["yellow", "Bloco Destaque"] } for bloco in tipos_blocos: bloco_data = params.get(f"{bloco}_data") if (bloco_data and len(bloco_data) > 0): color_bloco = params.get(f'{bloco}_color', dic_bloco[bloco][0]) label_bloco = params.get(f'{bloco}_label', f'Período de {dic_bloco[bloco][1]}') alpha_bloco = params.get(f'{bloco}_alpha', 0.2) for data in bloco_data: plt.axhspan(data['ymin'], data['ymax'], xmin=data['xmin'], xmax=data['xmax'], color=color_bloco, alpha=alpha_bloco) legenda = mpatches.Patch(color=color_bloco, label=label_bloco, alpha=alpha_bloco) legenda_bloco_h.append(legenda) if (show_legenda and (len(legenda_tag) > 0 or len(legenda_data_line) > 0 or len(legenda_bloco) > 0 or len(legenda_bloco_h) > 0)): plt.legend(handles=legenda_tag + legenda_data_line + legenda_bloco + legenda_reta_tendencia + legenda_bloco_h, fontsize=legenda_font_size, loc='best') if (len(save_figure_name) > 0): plt.savefig(save_figure_name, dpi=150, bbox_inches='tight') plt.show() # Removendo estilos plt.clf() plt.close()
def predictCases(data, days_predicted=35, vis=True): ''' Parameters ---------- data : Pandas Series Contains COVID-19 cases data for a county or state. days_predicted : Integer, optional Number of days to predict. The default is 35. vis : TYPE, optional Flag for creating visualization of fitting process. The default is True. Returns ------- case_output : Dictionary Contains predicted cases ''' # Parameters learn_rate = 0.125 w1 = 0.5 w2 = 0.5 z1 = w1 z2 = w2 learn_period = 14 county = data['County Name'] + ', ' + data['State'] # County name y = np.array(data.to_list()[4:]).astype(np.float) #Number of cases y_orig = y # Create dates start_date = datetime.date(2020, 1, 22) end_date = start_date + timedelta(days=len(y)) delta = datetime.timedelta(days=1) dates = drange(start_date, end_date, delta) end_date_pred = start_date + timedelta(days=len(y) + days_predicted) dates_pred = drange(start_date, end_date_pred, delta) dates_str = [item.strftime('%m/%d/%Y') for item in num2date(dates_pred)] # Need to remove leading 0's and make sure there is enough data to find a meaningful curve fit first_case_idx = np.nonzero(y)[0][0] # index of first case y_first = y[first_case_idx:] #data since first case # Learn from N days if len(y_first) < learn_period: learn_period = 3 y_idx_start = len(y_first) - learn_period # Initialize variables to sum cumulative observed error for comparing exponential vs quadratic quad_pred_err_tot = 0 exp_pred_err_tot = 0 # Loop through dates to fit and learn from for idx, y_idx in enumerate(np.arange(y_idx_start, len(y_first) + 1)): y = y_first[:y_idx] X = np.linspace(0, len(y) - 1, len(y)) #Days X_pred = np.linspace(0, len(y) - 1 + days_predicted, len(y) + days_predicted) #Days # Create dates if (idx == 0): p0_sig = [max(y_first), np.median(X), 1, min(y_first)] p0_quad = None p0_exp = None else: p0_sig = ps p0_quad = pq p0_exp = pe sig, quad, exp, ps, pq, pe = fit(y, X, X_pred, p0_sig, p0_quad, p0_exp) y_pred_quad = w1 * quad + w2 * sig y_pred_exp = z1 * exp + z2 * sig if (y_idx < len(y_first)): sig_err = np.sum((y_first[y_idx:] - sig[y_idx:len(y_first)])**2) quad_err = np.sum((y_first[y_idx:] - quad[y_idx:len(y_first)])**2) exp_err = np.sum((y_first[y_idx:] - exp[y_idx:len(y_first)])**2) if (idx > 2): quad_pred_err_tot += np.sum( (y_first[y_idx:] - y_pred_quad[y_idx:len(y_first)])**2) exp_pred_err_tot += np.sum( (y_first[y_idx:] - y_pred_exp[y_idx:len(y_first)])**2) tot_error = sig_err + quad_err w1_new = 1 - (quad_err / tot_error) w2_new = 1 - (sig_err / tot_error) tot_error = sig_err + exp_err z1_new = 1 - (exp_err / tot_error) z2_new = 1 - (sig_err / tot_error) # Save for plotting w1_old = np.copy(w1) w2_old = np.copy(w2) z1_old = z1 z2_old = z2 # Update weights w1 = w1 + (w1_new - w1) * learn_rate w2 = w2 + (w2_new - w2) * learn_rate z1 = z1 + (z1_new - z1) * learn_rate z2 = z2 + (z2_new - z2) * learn_rate if (vis): dates_pred_trunc = dates_pred[first_case_idx:first_case_idx + len(X_pred)] # Plot fig = plt.figure(figsize=(8, 6)) ax = fig.add_subplot(1, 1, 1) ax.plot_date(dates[first_case_idx:first_case_idx + y_idx], y, color='#e74c3c', ls='solid', marker='o') try: ax.plot_date(dates[first_case_idx + y_idx:first_case_idx + len(y_first)], y_first[y_idx:], color='#636e72', ls='solid', marker='o') except: pass ax.plot_date(dates_pred_trunc, y_pred_quad, color='#00b894', ls='solid', marker="") ax.plot_date(dates_pred_trunc, y_pred_exp, color='#2d3436', ls='solid', marker="") ax.plot_date(dates_pred_trunc, sig, color='#0984e3', ls='dashed', marker="") ax.plot_date(dates_pred_trunc, quad, color='#6c5ce7', ls='dashed', marker="") ax.plot_date(dates_pred_trunc, exp, color='#d63031', ls='dashed', marker="") w1r = np.round(w1_old, decimals=2) w2r = np.round(w2_old, decimals=2) z1r = np.round(z1_old, decimals=2) z2r = np.round(z2_old, decimals=2) # Format plot formatter = DateFormatter('%m/%d') rule = rrulewrapper(DAILY, interval=7) loc = RRuleLocator(rule) ax.xaxis.set_major_locator(loc) ax.set_xlabel('Days') ax.set_ylabel('Confirmed Cases') ax.set_title(county + '-- Q: ' + str(w1r) + ' L1: ' + str(w2r) + ' E: ' + str(z1r) + ' L2: ' + str(z2r)) ax.xaxis.set_major_formatter(formatter) ax.xaxis.set_tick_params(rotation=30, labelsize=8) ax.legend([ 'True [Training]', 'True [Testing]', 'Predicted-Quad.', 'Predicted-Exp.', 'Logistic', 'Quadratic', 'Exponential' ]) ax.set_ylim([0, np.max(y_first) * 1.5]) if (quad_pred_err_tot < exp_pred_err_tot): y_pred = y_pred_quad # growth = 'quadratic' else: y_pred = y_pred_exp # growth = 'exponential' pad_width = len(y_orig) - len(y) y = np.pad(y, (pad_width, 0), 'constant') y_pred = np.pad(y_pred, (pad_width, 0), 'constant') y_pred[y_pred < 0] = 0 pad_width = len(y_pred) - len(y) y = np.pad(y, (0, pad_width), 'constant', constant_values=(-1, -1)) X = np.linspace(0, len(y) - 1, len(y)) #Days X_pred = np.linspace(0, len(y) - 1 + days_predicted, len(y) + days_predicted) #Days # Create dictionary output case_output = { 'y': y, 'y_pred': y_pred, 'dates_str': dates_str, } return (case_output)
def out_ploting_installation(Installation, logistic_phase_description): # Data num_phases = len(Installation['OPERATION']) pos = arange(0.5, (num_phases) / 2 + 1.0, 0.5) ylabels = [] customDates = [] # for operation in Installation['OPERATION']: for operation in logistic_phase_description: l_phase = operation log_phase_descript = l_phase # log_phase_descript = log_phase_descript.replace("mooring systems with","") # log_phase_descript = log_phase_descript.replace(" ","_") # log_phase_descript = log_phase_descript.replace("/","_") ylabels.append(log_phase_descript) start_dt = Installation['OPERATION'][l_phase]['DATE'][ 'Start Date'] - timedelta( hours=sum(Installation['OPERATION'][l_phase]['TIME'] ['Preparation Time [h]'])) prep_dt = Installation['OPERATION'][l_phase]['DATE']['Start Date'] depart_dt = Installation['OPERATION'][l_phase]['DATE']['Depart Date'] end_dt = Installation['OPERATION'][l_phase]['DATE']['End Date'] # print 'log_phase_descript: ' + l_phase # DEBUGGING # print 'start_dt: ' + str(start_dt) # DEBUGGING # print 'prep_dt: ' + str(prep_dt) # DEBUGGING # print 'depart_dt: ' + str(depart_dt) # DEBUGGING # print 'end_dt: ' + str(end_dt) # DEBUGGING customDates.append([ create_date(start_dt.year, start_dt.month, start_dt.day, start_dt.hour, start_dt.minute, start_dt.second), create_date(prep_dt.year, prep_dt.month, prep_dt.day, prep_dt.hour, prep_dt.minute, prep_dt.second), create_date(depart_dt.year, depart_dt.month, depart_dt.day, depart_dt.hour, depart_dt.minute, depart_dt.second), create_date(end_dt.year, end_dt.month, end_dt.day, end_dt.hour, end_dt.minute, end_dt.second) ]) task_dates = {} for i, task in enumerate(ylabels): task_dates[task] = customDates[i] # Initialise plot fig = plt.figure() # ax = subplot2grid((1,3), (0,1), colspan=2) ax = subplot2grid((1, 2), (0, 1), colspan=1) # Plot the data: start_date, end_prep_begin_waiting_date, end_waiting_begin_sea_date, end_date = task_dates[ ylabels[0]] ax.barh(0.5, end_date - start_date, left=start_date, height=0.4, align='center', color='blue', alpha=0.75) ax.barh(0.4, (end_prep_begin_waiting_date - start_date), left=start_date, height=0.1, align='center', color='red', alpha=0.75, label="Prep Time") ax.barh(0.5, (end_waiting_begin_sea_date - end_prep_begin_waiting_date), left=end_prep_begin_waiting_date, height=0.1, align='center', color='yellow', alpha=0.75, label="Waiting Time") ax.barh(0.6, (end_date - end_waiting_begin_sea_date), left=end_waiting_begin_sea_date, height=0.1, align='center', color='green', alpha=0.75, label="Sea Time") for i in range(0, len(ylabels) - 1): start_date, end_prep_begin_waiting_date, end_waiting_begin_sea_date, end_date = task_dates[ ylabels[i + 1]] ax.barh((i * 0.5) + 1.0, end_date - start_date, left=start_date, height=0.4, align='center', color='blue', alpha=0.75) ax.barh((i * 0.5) + 1.0 - 0.1, (end_prep_begin_waiting_date - start_date), left=start_date, height=0.1, align='center', color='red', alpha=0.75) ax.barh((i * 0.5) + 1.0, (end_waiting_begin_sea_date - end_prep_begin_waiting_date), left=end_prep_begin_waiting_date, height=0.1, align='center', color='yellow', alpha=0.75) ax.barh((i * 0.5) + 1.0 + 0.1, (end_date - end_waiting_begin_sea_date), left=end_waiting_begin_sea_date, height=0.1, align='center', color='green', alpha=0.75) # Format the y-axis locsy, labelsy = yticks(pos, ylabels) plt.setp(labelsy, fontsize=12) # Format the x-axis ax.axis('tight') ax.set_ylim(ymin=-0.1, ymax=(num_phases) / 2 + 1.0) ax.grid(color='g', linestyle=':') ax.xaxis_date() #Tell matplotlib that these are dates... rule = rrulewrapper(MONTHLY, interval=1) loc = RRuleLocator(rule) formatter = DateFormatter("%b '%y") ax.xaxis.set_major_locator(loc) ax.xaxis.set_major_formatter(formatter) labelsx = ax.get_xticklabels() plt.setp(labelsx, rotation=30, fontsize=11) # Format the legend font = font_manager.FontProperties(size='small') ax.legend(loc=1, prop=font) # Finish up ax.invert_yaxis() fig.autofmt_xdate() #plt.savefig('gantt.svg') plt.savefig('Outputs' + '/Fig_Gantt_Installation' + '.png') # plt.savefig( 'Fig_Gantt_Installation' + '.svg' ) # plt.savefig( 'Fig_Gantt_Installation' + '.eps' ) plt.show() return
def plot_ev_demand_compare(request): supplyPoint = request.GET['supplyPoint'] supplyPoint2 = request.GET['supplyPoint2'] m = int(request.GET['month']) d = request.GET['days'] gspName = GSP.objects.filter(idx=supplyPoint) na0 = ([p.name for p in gspName]) gspAuthority = gspLocalAuthority.objects.filter(gsp=na0[0]) na1 = ([p.localAuthority for p in gspAuthority]) gspName2 = GSP.objects.filter(idx=supplyPoint2) na0_2 = ([p.name for p in gspName2]) gspAuthority2 = gspLocalAuthority.objects.filter(gsp=na0_2[0]) na1_2 = ([p.localAuthority for p in gspAuthority2]) mediumEV = request.GET['mediumEV'] smallEV = request.GET['smallEV'] geographicArea = int(request.GET['geographicArea']) if geographicArea == 1: gA = 'Urban' else: gA = 'Rural' if d == '': d = int(0) else: d = int(d) if m == 13: d = 366 if mediumEV == '': mediumEV = int(0) else: mediumEV = int(mediumEV) if smallEV == '': smallEV = int(0) else: smallEV = int(smallEV) electricalData = process_data( (electricalGSP.objects.filter(GSP=supplyPoint)), electricalGSP, 3) chargeDataSmall = process_data((Journey.objects.filter( localAuthority=na1[0], Area=gA, typeEV='Economy')), Journey, 7) chargeDataMedium = process_data((Journey.objects.filter( localAuthority=na1[0], Area=gA, typeEV='Midsize')), Journey, 7) electricalData2 = process_data( (electricalGSP.objects.filter(GSP=supplyPoint2)), electricalGSP, 3) chargeDataSmall2 = process_data((Journey.objects.filter( localAuthority=na1_2[0], Area=gA, typeEV='Economy')), Journey, 7) chargeDataMedium2 = process_data((Journey.objects.filter( localAuthority=na1_2[0], Area=gA, typeEV='Midsize')), Journey, 7) ### electricalData = electricalData[0:, 2] / 2 chargeDataSmall = chargeDataSmall[0:, 0:4] chargeDataMedium = chargeDataMedium[0:, 0:4] profileSmall = formatChargeDemand(smallEV, chargeDataSmall, 'Economy', d) profileMedium = formatChargeDemand(mediumEV, chargeDataMedium, 'Midsize', d) profileTotal = np.zeros([len(profileSmall), 1]) profileTotal[0:, 0] = (profileSmall['Charge'].as_matrix() + profileMedium['Charge'].as_matrix()) dS = pd.date_range(start='2015-04-01', end='2016-04-01', freq='H') proData = pd.DataFrame({'Time': dS[0:8784], 'Data': electricalData}) data = proData.set_index('Time') d1, d2, S = month(m, data) elec = S['Data'] pC = max(profileTotal)[0] peakCharge = round(pC, 2) ### electricalData2 = electricalData2[0:, 2] / 2 chargeDataSmall2 = chargeDataSmall2[0:, 0:4] chargeDataMedium2 = chargeDataMedium2[0:, 0:4] profileSmall2 = formatChargeDemand(smallEV, chargeDataSmall2, 'Economy', d) profileMedium2 = formatChargeDemand(mediumEV, chargeDataMedium2, 'Midsize', d) profileTotal2 = np.zeros([len(profileSmall), 1]) profileTotal2[0:, 0] = (profileSmall2['Charge'].as_matrix() + profileMedium2['Charge'].as_matrix()) pC2 = max(profileTotal2)[0] peakCharge2 = round(pC2, 2) ### if d == 1: formatter = DateFormatter('%H-%M') rule = rrulewrapper(HOURLY, interval=6) elif d < 4: formatter = DateFormatter('%d-%m-%y') rule = rrulewrapper(DAILY, interval=1) elif d < 10: formatter = DateFormatter('%d-%m-%y') rule = rrulewrapper(DAILY, interval=2) elif d > 9 and d < 16: formatter = DateFormatter('%d-%m-%y') rule = rrulewrapper(DAILY, interval=3) elif d > 15 and d < 32: formatter = DateFormatter('%d-%m') rule = rrulewrapper(DAILY, interval=5) if m == 13: formatter = DateFormatter('%b-%Y') rule = rrulewrapper(MONTHLY, interval=3) loc = RRuleLocator(rule) delta = datetime.timedelta(hours=1) dates = drange(d1, d2, delta) finalDates = dates[0:(24 * d)] fig, ax = plt.subplots() ax.plot_date(finalDates, profileTotal, linestyle='-', marker='None', label=na0[0]) ax.plot_date(finalDates, profileTotal2, linestyle='-', marker='None', label=na0_2[0]) ax.xaxis.set_major_formatter(formatter) ax.xaxis.set_major_locator(loc) plt.ylabel('Charging Demand (kWh)') plt.grid(True) plt.legend() buffer = BytesIO() canvas = pylab.get_current_fig_manager().canvas canvas.draw() pilImage = PIL.Image.frombytes("RGB", canvas.get_width_height(), canvas.tostring_rgb()) pilImage.save(buffer, "PNG") pylab.close() image_png = buffer.getvalue() graphic = base64.b64encode(image_png) graphic = graphic.decode('utf-8') ## return render( request, 'transport/show_plot_ev_compare.html', { 'graphic': graphic, 'name': na0[0], 'name2': na0_2[0], 'peakCharge': peakCharge, 'peakCharge2': peakCharge2 })
def get_locator(self, dmin, dmax): "pick the best locator based on a distance" delta = relativedelta(dmax, dmin) numYears = delta.years * 1.0 numMonths = (numYears * 12.0) + delta.months numDays = (numMonths * 31.0) + delta.days numHours = (numDays * 24.0) + delta.hours numMinutes = (numHours * 60.0) + delta.minutes numSeconds = (numMinutes * 60.0) + delta.seconds numticks = 5 # self._freq = YEARLY interval = 1 bymonth = 1 bymonthday = 1 byhour = 0 byminute = 0 bysecond = 0 if numYears >= numticks: self._freq = YEARLY elif numMonths >= numticks: self._freq = MONTHLY bymonth = list(range(1, 13)) if (0 <= numMonths) and (numMonths <= 14): interval = 1 # show every month elif (15 <= numMonths) and (numMonths <= 29): interval = 3 # show every 3 months elif (30 <= numMonths) and (numMonths <= 44): interval = 4 # show every 4 months else: # 45 <= numMonths <= 59 interval = 6 # show every 6 months elif numDays >= numticks: self._freq = DAILY bymonth = None bymonthday = list(range(1, 32)) if (0 <= numDays) and (numDays <= 9): interval = 1 # show every day elif (10 <= numDays) and (numDays <= 19): interval = 2 # show every 2 days elif (20 <= numDays) and (numDays <= 35): interval = 3 # show every 3 days elif (36 <= numDays) and (numDays <= 80): interval = 7 # show every 1 week else: # 100 <= numDays <= ~150 interval = 14 # show every 2 weeks elif numHours >= numticks: self._freq = HOURLY bymonth = None bymonthday = None byhour = list(range(0, 24)) # show every hour if (0 <= numHours) and (numHours <= 14): interval = 1 # show every hour elif (15 <= numHours) and (numHours <= 30): interval = 2 # show every 2 hours elif (30 <= numHours) and (numHours <= 45): interval = 3 # show every 3 hours elif (45 <= numHours) and (numHours <= 68): interval = 4 # show every 4 hours elif (68 <= numHours) and (numHours <= 90): interval = 6 # show every 6 hours else: # 90 <= numHours <= 120 interval = 12 # show every 12 hours elif numMinutes >= numticks: self._freq = MINUTELY bymonth = None bymonthday = None byhour = None byminute = list(range(0, 60)) if numMinutes > (10.0 * numticks): interval = 10 # end if elif numSeconds >= numticks: self._freq = SECONDLY bymonth = None bymonthday = None byhour = None byminute = None bysecond = list(range(0, 60)) if numSeconds > (10.0 * numticks): interval = 10 # end if else: # do what? # microseconds as floats, but floats from what reference point? pass rrule = rrulewrapper( self._freq, interval=interval, dtstart=dmin, until=dmax, bymonth=bymonth, bymonthday=bymonthday, byhour=byhour, byminute=byminute, bysecond=bysecond, ) locator = RRuleLocator(rrule, self.tz) locator.set_axis(self.axis) locator.set_view_interval(*self.axis.get_view_interval()) locator.set_data_interval(*self.axis.get_data_interval()) return locator
def create(): pos = arange(0.5,2.5,0.5) print "pos:\n "+str(pos) ylabels = [] # ylabels.append('Hardware Design & Review') # ylabels.append('Hardware Construction') # ylabels.append('Integrate and Test Laser Source') # ylabels.append('Objective #1') # ylabels.append('Objective #2') # ylabels.append('Present at ASMS') ylabels.append('') ylabels.append('Activity') effort = [] # effort.append([0.2, 1.0]) # effort.append([0.2, 1.0]) # effort.append([0.2, 1.0]) # effort.append([0.3, 0.75]) # effort.append([0.25, 0.75]) # effort.append([0.3, 0.75]) effort.append([0.5, 0.9]) effort.append([0.7, 0.4]) customDates = [] print "first_tweet_month: "+str(Global.first_tweet_month) print "first_tweet_year: "+str(Global.first_tweet_year) first_tweet_month_end = (Global.first_tweet_month+2)%13 if (Global.first_tweet_month != 12 and Global.first_tweet_month != 11) else 2 first_tweet_year_end = Global.first_tweet_year if first_tweet_month_end != 2 else Global.first_tweet_year+1 print "first_tweet_month_end: "+str(first_tweet_month_end) print "first_tweet_year_end: "+str(first_tweet_year_end) customDates.append([create_date(Global.first_tweet_month, Global.first_tweet_year), create_date(first_tweet_month_end, first_tweet_year_end)]) # customDates.append([create_date(6,2014),create_date(8,2014),create_date(8,2014)]) # customDates.append([create_date(7,2014),create_date(9,2014),create_date(9,2014)]) # customDates.append([create_date(10,2014),create_date(3,2015),create_date(3,2015)]) # customDates.append([create_date(2,2015),create_date(6,2015),create_date(6,2015)]) # customDates.append([create_date(5,2015),create_date(6,2015),create_date(6,2015)]) print "created_at_month: "+str(Global.created_at_month) print "created_at_year: "+str(Global.created_at_year) created_at_month_end = (Global.created_at_month+2)%13 if (Global.created_at_month != 12 and Global.created_at_month != 11) else 2 created_at_year_end = Global.created_at_year if created_at_month_end != 2 else Global.created_at_year+1 print "created_at_month_end: "+str(created_at_month_end) print "created_at_year_end: "+str(created_at_year_end) # print "cien_tweet_month: "+str(Global.cien_tweet_month) print "cien_tweet_year: "+str(Global.cien_tweet_year) cien_tweet_month_end = (Global.cien_tweet_month+2)%13 if (Global.cien_tweet_month != 12 and Global.cien_tweet_month != 11) else 2 cien_tweet_year_end = Global.cien_tweet_year if cien_tweet_month_end != 2 else Global.cien_tweet_year+1 print "cien_tweet_month_end: "+str(cien_tweet_month_end) print "cien_tweet_year_end: "+str(cien_tweet_year_end) if(Global.cien_tweet_year != 0): customDates.append([create_date(Global.created_at_month, Global.created_at_year), create_date(created_at_month_end, created_at_year_end), create_date(created_at_month_end, created_at_year_end), create_date(Global.first_tweet_month, Global.first_tweet_year), create_date(first_tweet_month_end, first_tweet_year_end), create_date(first_tweet_month_end, first_tweet_year_end), create_date(Global.cien_tweet_month, Global.cien_tweet_year), create_date(cien_tweet_month_end, cien_tweet_year_end), create_date(cien_tweet_month_end, cien_tweet_year_end) ]) else: customDates.append([create_date(Global.created_at_month, Global.created_at_year), create_date(created_at_month_end, created_at_year_end), create_date(created_at_month_end, created_at_year_end), create_date(Global.first_tweet_month, Global.first_tweet_year), create_date(first_tweet_month_end, first_tweet_year_end), create_date(first_tweet_month_end, first_tweet_year_end) ]) # customDates.append([create_date(4,2015),create_date(8,2015),create_date(8,2015)]) task_dates = {} for i, task in enumerate(ylabels): print "i: "+str(i) print " task: "+task print " customDates[i]: "+str(customDates[i]) task_dates[task] = customDates[i] # task_dates['Climatology'] = [create_date(5,2014),create_date(6,2014),create_date(10,2013)] # task_dates['Structure'] = [create_date(10,2013),create_date(3,2014),create_date(5,2014)] # task_dates['Impacts'] = [create_date(5,2014),create_date(12,2014),create_date(2,2015)] # task_dates['Thesis'] = [create_date(2,2015),create_date(5,2015)] # Initialise plot fig = plt.figure() # ax = fig.add_axes([0.15,0.2,0.75,0.3]) #[left,bottom,width,height] ax = fig.add_subplot(111) # Plot the data # start_date,end_date = task_dates[ylabels[0]] # ax.barh(0.5, end_date - start_date, left=start_date, height=0.3, align='center', color='blue', alpha = 0.75) # ax.barh(0.45, (end_date - start_date)*effort[0][0], left=start_date, height=0.1, align='center', color='blue', alpha = 0.75, label = "account created") # ax.barh(0.45, (end_date - start_date)*effort[0][0], left=start_date, height=0.1, align='center', color='green', alpha = 0.75, label = "time taken for 1st tweet: "+str(Global.time_taken_1st_tweet)) # ax.barh(0.55, (end_date - start_date)*effort[0][1], left=start_date, height=0.1, align='center', color='red', alpha = 0.75, label = "after that, time taken for 100 tweets: "+str(Global.time_taken_100_tweets)) for i in range(0,len(ylabels)-1): labels = ['Analysis','Reporting'] if i == 1 else [None,None] print "i date: "+str(i) piEffort, studentEffort = effort[i+1] print "NOW....." print "task_dates: "+str(task_dates) if(Global.cien_tweet_year != 0): start_date,mid_date,end_date, start_date_new,mid_date_new,end_date_new, cien_start,cien_mid,cien_end = task_dates[ylabels[i+1]] else: start_date,mid_date,end_date, start_date_new,mid_date_new,end_date_new = task_dates[ylabels[i+1]] ax.barh((i*0.5)+1.0+0.3, mid_date - start_date, left=start_date, height=0.3, align='center', color='blue', alpha = 0.75, label = "account created") # ax.barh((i*0.5)+1.0-0.05, (mid_date - start_date)*piEffort, left=start_date, height=0.1, align='center', color='yellow', alpha = 0.75) # ax.barh((i*0.5)+1.0+0.05, (mid_date - start_date)*studentEffort, left=start_date, height=0.1, align='center', color='orange', alpha = 0.75) # ax.barh((i*0.5)+1.0, end_date - mid_date, left=mid_date, height=0.3, align='center',label=labels[1], color='yellow') ax.barh((i*0.5)+1.0, mid_date_new - start_date_new, left=start_date_new, height=0.3, align='center', color='green', alpha = 0.75, label = "time taken for 1st tweet: "+str(Global.time_taken_1st_tweet)) # ax.barh((i*0.5)+1.0-0.05, (mid_date_new - start_date_new)*piEffort, left=start_date_new, height=0.1, align='center', color='red', alpha = 0.75) # ax.barh((i*0.5)+1.0+0.05, (mid_date_new - start_date_new)*studentEffort, left=start_date_new, height=0.1, align='center', color='yellow', alpha = 0.75) if(Global.cien_tweet_year != 0): ax.barh((i*0.5)+1.0-0.3, cien_mid - cien_start, left=cien_start, height=0.3, align='center', color='red', alpha = 0.75, label = "after that, time taken for 100 tweets: "+str(Global.time_taken_100_tweets)) # ax.barh((i*0.5)+1.0-0.05, (cien_mid - cien_start)*piEffort, left=cien_start, height=0.1, align='center', color='red', alpha = 0.75) # ax.barh((i*0.5)+1.0+0.05, (cien_mid - cien_start)*studentEffort, left=cien_start, height=0.1, align='center', color='orange', alpha = 0.75) # Format the y-axis locsy, labelsy = yticks(pos,ylabels) plt.setp(labelsy, fontsize = 12) # Format the x-axis ax.axis('tight') ax.set_ylim(ymin = -0.1, ymax = 1.5) ax.grid(color = 'g', linestyle = ':') ax.xaxis_date() #Tell matplotlib that these are dates... created_at_date = dt.datetime(Global.created_at_year, Global.created_at_month, 1) first_tweet_date = dt.datetime(Global.first_tweet_year, Global.first_tweet_month, 1) if(Global.cien_tweet_year != 0): final_tweet_date = dt.datetime(Global.cien_tweet_year, Global.cien_tweet_month, 1) else: final_tweet_date = dt.datetime(Global.first_tweet_year, Global.first_tweet_month, 1) date_delta = final_tweet_date - created_at_date # max_timespan = dt.datetime(2, 1, 1) # 2 years print "created date: "+str(created_at_date) print "first tweet date: "+str(first_tweet_date) print "final tweet date: "+str(final_tweet_date) print "difference: "+str(date_delta.days) if(date_delta.days > (365 * 11)-1): # 11 years rule = rrulewrapper(MONTHLY, interval=15) elif(date_delta.days > (365 * 8)-1): # 8 years rule = rrulewrapper(MONTHLY, interval=9) elif(date_delta.days > (365 * 4)-1): # 4 years rule = rrulewrapper(MONTHLY, interval=6) elif(date_delta.days > (365 +(365/2))-1): # 1 and 1/2 years rule = rrulewrapper(MONTHLY, interval=3) else: rule = rrulewrapper(MONTHLY, interval=1) loc = RRuleLocator(rule) formatter = DateFormatter("%b '%y") ax.xaxis.set_major_locator(loc) ax.xaxis.set_major_formatter(formatter) labelsx = ax.get_xticklabels() plt.setp(labelsx, rotation=30, fontsize=12) # Format the legend font = font_manager.FontProperties(size='small') ax.legend(loc=1,prop=font) # Finish up ax.invert_yaxis() fig.autofmt_xdate() plt.savefig('png/'+Global.screen_name+'_gantt.png')
def get_locator( self, dmin, dmax ): 'pick the best locator based on a distance' delta = relativedelta( dmax, dmin ) numYears = ( delta.years * 1.0 ) numMonths = ( numYears * 12.0 ) + delta.months numDays = ( numMonths * 31.0 ) + delta.days numHours = ( numDays * 24.0 ) + delta.hours numMinutes = ( numHours * 60.0 ) + delta.minutes numSeconds = ( numMinutes * 60.0 ) + delta.seconds numticks = 5 # self._freq = YEARLY interval = 1 bymonth = 1 bymonthday = 1 byhour = 0 byminute = 0 bysecond = 0 if numYears >= numticks: self._freq = YEARLY elif numMonths >= numticks: self._freq = MONTHLY bymonth = range( 1, 13 ) if ( 0 <= numMonths ) and ( numMonths <= 14 ): interval = 1 # show every month elif ( 15 <= numMonths ) and ( numMonths <= 29 ): interval = 3 # show every 3 months elif ( 30 <= numMonths ) and ( numMonths <= 44 ): interval = 4 # show every 4 months else: # 45 <= numMonths <= 59 interval = 6 # show every 6 months elif numDays >= numticks: self._freq = DAILY bymonth = None bymonthday = range( 1, 32 ) if ( 0 <= numDays ) and ( numDays <= 9 ): interval = 1 # show every day elif ( 10 <= numDays ) and ( numDays <= 19 ): interval = 2 # show every 2 days elif ( 20 <= numDays ) and ( numDays <= 35 ): interval = 3 # show every 3 days elif ( 36 <= numDays ) and ( numDays <= 80 ): interval = 7 # show every 1 week else: # 100 <= numDays <= ~150 interval = 14 # show every 2 weeks elif numHours >= numticks: self._freq = HOURLY bymonth = None bymonthday = None byhour = range( 0, 24 ) # show every hour if ( 0 <= numHours ) and ( numHours <= 14 ): interval = 1 # show every hour elif ( 15 <= numHours ) and ( numHours <= 30 ): interval = 2 # show every 2 hours elif ( 30 <= numHours ) and ( numHours <= 45 ): interval = 3 # show every 3 hours elif ( 45 <= numHours ) and ( numHours <= 68 ): interval = 4 # show every 4 hours elif ( 68 <= numHours ) and ( numHours <= 90 ): interval = 6 # show every 6 hours else: # 90 <= numHours <= 120 interval = 12 # show every 12 hours elif numMinutes >= numticks: self._freq = MINUTELY bymonth = None bymonthday = None byhour = None byminute = range( 0, 60 ) if numMinutes > ( 10.0 * numticks ): interval = 10 # end if elif numSeconds >= numticks: self._freq = SECONDLY bymonth = None bymonthday = None byhour = None byminute = None bysecond = range( 0, 60 ) if numSeconds > ( 10.0 * numticks ): interval = 10 # end if else: # do what? # microseconds as floats, but floats from what reference point? pass rrule = rrulewrapper( self._freq, interval = interval, \ dtstart = dmin, until = dmax, \ bymonth = bymonth, bymonthday = bymonthday, \ byhour = byhour, byminute = byminute, \ bysecond = bysecond ) locator = RRuleLocator( rrule, self.tz ) locator.set_axis( self.axis ) locator.set_view_interval( *self.axis.get_view_interval() ) locator.set_data_interval( *self.axis.get_data_interval() ) return locator