def plot(): fig, ax = plt.subplots() interval = 3600 data = binned(interval) cmds = data.sum().sort_values(ascending=False)[:10].index.values total = data.sum(axis=1) float_times = data.index.to_series().map( lambda ts: date2num(ts.to_pydatetime())) plt.xticks(rotation='vertical') ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) ax.xaxis.set_major_formatter(DateFormatter("%b %d %H00")) ax.xaxis.set_major_locator(HourLocator(byhour=range(0, 24, 12))) ax.xaxis.set_minor_locator(HourLocator(byhour=range(0, 24, 3))) ax.set_title("Commands Run Per Hour") ax.plot(float_times, total, linestyle='-', linewidth=0.2, label='sum') bottom = np.zeros(len(float_times)) bar_width = interval / 86400.0 # Fraction of a day. for cmd in cmds: vals = data[cmd].values ax.bar(float_times, vals, bottom=bottom, width=bar_width, label=cmd) bottom += vals ax.legend() # Called after all data has been plotted. plt.savefig('shellhist_pandas_stacked_bars.svg')
def fmt_major_minor(axe): axe.fmt_xdata = DateFormatter('%d/%m/%y %H:%M') x_lim = axe.get_xlim() if (x_lim[-1] - x_lim[0]) == 0: axe.xaxis.set_major_locator(HourLocator()) axe.xaxis.set_major_formatter(DateFormatter('%d/%m/%y\n%H:%M')) elif 0 < (x_lim[-1] - x_lim[0]) < 5: axe.xaxis.set_major_locator(DayLocator()) axe.xaxis.set_major_formatter(DateFormatter('%d/%m/%y')) axe.xaxis.set_minor_locator(HourLocator(byhour=(3, 6, 9, 12, 15, 18, 21))) axe.xaxis.set_minor_formatter(DateFormatter('%H:%M')) elif 5 <= (x_lim[-1] - x_lim[0]) < 11: axe.xaxis.set_major_locator(DayLocator()) axe.xaxis.set_major_formatter(DateFormatter('%d/%m/%y')) axe.xaxis.set_minor_locator(HourLocator(byhour=(6, 12, 18))) axe.xaxis.set_minor_formatter(DateFormatter('%H:%M')) elif 11 <= (x_lim[-1] - x_lim[0]) < 45: axe.xaxis.set_major_locator(DayLocator(interval=3)) axe.xaxis.set_major_formatter(DateFormatter('%d/%m/%y')) axe.xaxis.set_minor_locator(HourLocator(byhour=(12, 00))) else: axe.xaxis.set_minor_locator(MonthLocator()) axe.xaxis.set_major_locator(YearLocator()) axe.xaxis.set_major_formatter(DateFormatter('%Y')) for label in axe.xaxis.get_majorticklabels(): label.set_fontsize(8) label.set_ha('right') label.set_rotation(45) for label in axe.xaxis.get_minorticklabels(): label.set_fontsize(7) label.set_ha('right') label.set_rotation(45) for label in axe.get_yticklabels(): label.set_fontsize(8)
def draw_chart(x, y, size, format="png", dpi=80, color="#000080", xlim=None): from matplotlib.dates import DayLocator, HourLocator, AutoDateFormatter from matplotlib import ticker import pylab as pl f = plt.figure() ax = f.add_axes([0, 0, 1, 1]) if xlim: ax.set_xlim(xlim[0], xlim[1]) else: ax.set_xlim(x[0], x[-1]) ax.plot(x, y) formatter = AutoDateFormatter(HourLocator()) formatter.scaled[(1/24.)] = "%H:%M" ax.xaxis.grid(True, which="major") ax.yaxis.grid(True, which="major") ax.xaxis.set_major_locator(HourLocator()) ax.xaxis.set_major_formatter(formatter) res = cStringIO.StringIO() w_inches = size[0] / dpi h_inches = size[1] / dpi if h_inches <= 0 or w_inches <= 0: raise Exception("chart size not allowed!") f.autofmt_xdate() f.set_size_inches(w_inches, h_inches) f.savefig(res, format=format, dpi=dpi, bbox_inches="tight") return res.getvalue()
def draw_chart(chart_title, predicted, actual, dates, png_filename): # three pd.Series import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt from matplotlib.dates import DayLocator, HourLocator, DateFormatter chart_width, chart_height = 11, 8.5 fig = plt.figure(figsize=(chart_width, chart_height)) day_count = 1 + (dates[-1] - dates[0]).days ax = fig.add_subplot(111) ordinals = [matplotlib.dates.date2num(d) for d in dates] ax.plot_date(ordinals, actual, 'b-', label='Actual') ax.plot_date(ordinals, predicted, 'r-', label='Predicted') ax.xaxis.set_major_locator( DayLocator(interval=compute_date_step(day_count, chart_width))) ax.xaxis.set_major_formatter(DateFormatter('%Y-%b-%d')) locator = HourLocator() locator.MAXTICKS = (day_count + 3) * 24 ax.xaxis.set_minor_locator(locator) ax.autoscale_view() ax.grid(True) fig.autofmt_xdate() ax.legend(loc='upper right', shadow=True) plt.title(chart_title, fontsize=10) fig.savefig(png_filename) plt.close() print(">> wrote: ", png_filename)
def label_dates_and_hours(axes): axes.xaxis.set_major_locator(HourLocator([0])) axes.xaxis.set_minor_locator(HourLocator([0, 6, 12, 18])) axes.xaxis.set_major_formatter(DateFormatter('0h\n%Y %b %d\n%A')) axes.xaxis.set_minor_formatter(DateFormatter('%Hh')) for label in ax.xaxis.get_ticklabels(which='both'): label.set_horizontalalignment('left') axes.yaxis.set_major_formatter('{x:.0f} km') axes.tick_params(which='both', length=0)
def _plot_prediction_true_res(self): """ Output: Plot of Model Prediction vs. True """ # y_trues = pd.DataFrame(ubm.hold_set['avg_price_est']) self.y_preds = pd.DataFrame(self.y_pred, index=self.hold_set.index, columns=['y_pred']) self.data = pd.concat([self.hold_set,self.y_preds], axis=1) # print data.columns for city in self.cities: for cartype in self.cartypes: plt.cla() if city != 'city_chicago': self.sub_data = self.data[(self.data[city] == 1) & (self.data[cartype] == 1)] else: self.sub_data = self.data[(self.data[self.cities[0]] == 0) & (self.data[self.cities[1]] == 0) & (self.data[self.cities[2]] == 0) & (self.data[self.cities[3]] == 0) & (self.data[cartype] == 1)] fig, ax = plt.subplots(2,1,figsize=(20,10)) if 'lyft' in self.filename: ax[0].plot_date(self.sub_data.index.to_pydatetime(), self.sub_data['avg_est_price'].values, 'o--', label='true data') else: ax[0].plot_date(self.sub_data.index.to_pydatetime(), self.sub_data['avg_price_est'].values, 'o--', label='true data') ax[0].plot_date(self.sub_data.index.to_pydatetime(), self.sub_data['y_pred'].values, '-', label='prediction', alpha=0.8) ax[0].xaxis.set_minor_locator(HourLocator(byhour=range(24), interval=2)) ax[0].xaxis.set_minor_formatter(DateFormatter('%H')) ax[0].xaxis.set_major_locator(WeekdayLocator(byweekday=range(7), interval=1)) ax[0].xaxis.set_major_formatter(DateFormatter('\n\n%a\n%D')) ax[0].xaxis.grid(True, which="minor") ax[0].set_xlabel('hour') ax[0].set_ylabel('average price estimate') ax[0].legend(loc="upper right") ax[0].set_title("Y Predictions vs Y Trues For {}, {}".format(cartype.split('_')[-1],city.split('_')[-1])) if 'lyft' in self.filename: self.data['resid'] = self.data['avg_est_price'] - self.data['y_pred'] else: self.data['resid'] = self.data['avg_price_est'] - self.data['y_pred'] if city != 'city_chicago': self.resid = self.data[(self.data[city] == 1) & (self.data[cartype] == 1)]['resid'] else: self.resid = self.data[(self.data[self.cities[0]] == 0) & (self.data[self.cities[1]] == 0) & (self.data[self.cities[2]] == 0) & (self.data[self.cities[3]] == 0) & (self.data[cartype] == 1)]['resid'] ax[1].plot_date(self.resid.index.to_pydatetime(), self.resid.values, 'o', label='residuals', alpha=0.3); ax[1].xaxis.set_minor_locator(HourLocator(byhour=range(24), interval=2)) ax[1].xaxis.set_minor_formatter(DateFormatter('%H')) ax[1].xaxis.set_major_locator(WeekdayLocator(byweekday=range(7), interval=1)) ax[1].xaxis.set_major_formatter(DateFormatter('\n\n%a\n%D')) ax[1].xaxis.grid(True, which="minor") ax[1].set_xlabel('hour') ax[1].set_ylabel('price residuals') ax[1].legend(loc="upper right") plt.tight_layout() plt.savefig('plots/pred_int_{}_{}.png'.format(cartype.split('_')[-1],city.split('_')[-1])) print "finished plot {}, {}".format(cartype.split('_')[-1],city.split('_')[-1]) plt.close('all')
def setaxdate(self, axes, datemn, datemx): u""" Customizar rótulos de eixo temporal. """ # Limites inferior e superior do eixo. (axes.set_xlim([ datemn - timedelta(hours=1), datemx + timedelta(hours=1) ]) if datemn == datemx else axes.set_xlim([datemn, datemx])) axes.fmt_xdata = DateFormatter('%d/%m/%y %H:%M:%S') # Escala temporal: < 1 hora. if (datemx - datemn) < timedelta(hours=1): axes.xaxis.set_major_locator( MinuteLocator(byminute=(00, 15, 30, 45))) axes.xaxis.set_major_formatter(DateFormatter('%H:%M')) axes.xaxis.set_minor_locator( MinuteLocator(byminute=(05, 10, 20, 25, 35, 40, 50, 55))) axes.xaxis.set_minor_formatter(DateFormatter('')) # Escala temporal: < 12 horas. elif timedelta(hours=1) < (datemx - datemn) < timedelta(hours=12): axes.xaxis.set_major_locator( HourLocator(byhour=(0, 3, 6, 9, 12, 15, 18, 21))) axes.xaxis.set_major_formatter(DateFormatter('%d/%m/%y\n%H:%M')) axes.xaxis.set_minor_locator(HourLocator()) axes.xaxis.set_minor_formatter(DateFormatter('')) # Escala temporal: > 12horas, 10[ dias. elif timedelta(hours=12) <= (datemx - datemn) < timedelta(hours=240): axes.xaxis.set_major_locator(DayLocator()) axes.xaxis.set_major_formatter(DateFormatter('%d/%m/%y')) axes.xaxis.set_minor_locator(HourLocator(byhour=(6, 12, 18))) axes.xaxis.set_minor_formatter(DateFormatter('%H:%M')) # Escala temporal: [11, 45[ dias. elif 10 <= (datemx.toordinal() - datemn.toordinal()) < 45: axes.xaxis.set_major_locator( DayLocator(bymonthday=(3, 8, 13, 18, 23, 28))) axes.xaxis.set_major_formatter(DateFormatter('%d/%m/%y')) axes.xaxis.set_minor_locator(DayLocator()) axes.xaxis.set_minor_formatter(DateFormatter('')) # Escala temporal: [45, 183[ dias. elif 45 <= (datemx.toordinal() - datemn.toordinal()) < 183: axes.xaxis.set_major_locator(MonthLocator()) axes.xaxis.set_major_formatter(DateFormatter('%m/%Y')) axes.xaxis.set_minor_locator( DayLocator(bymonthday=(5, 10, 15, 20, 25))) axes.xaxis.set_minor_formatter(DateFormatter('%d')) # Escala temporal: >= 365 dias. else: axes.xaxis.set_major_locator(YearLocator()) axes.xaxis.set_major_formatter(DateFormatter('%Y')) axes.xaxis.set_minor_locator(MonthLocator()) axes.xaxis.set_minor_formatter(DateFormatter('%m')) for label in axes.xaxis.get_majorticklabels(): label.set_rotation(45) for label in axes.xaxis.get_minorticklabels(): label.set_rotation(45)
def plot_EV_bid(results, df_batterydata, EV): df_buy = pd.read_csv(results + '/buy_bids.csv', index_col=[0], parse_dates=['timedate']) df_buy.set_index('timedate', inplace=True) df_buy = df_buy.loc[df_buy['appliance_name'] == EV] df_buy.rename(columns={'bid_price': 'bid_p_buy'}, inplace=True) df_EV = pd.read_csv(results + '/df_EV.csv', index_col=[0]) fig = ppt.figure(figsize=(6, 4), dpi=150) ppt.ioff() #House load ax = fig.add_subplot(111) ax.set_ylabel('Local Retail Price [USD/MW]') lns1 = ax.plot(df_batterydata['clearing_price'], 'rx', label='Realtime Local Price') lns2 = ax.plot(df_buy['bid_p_buy'], 'bx', label='EV buy bid') #ax.axvspan(df_EV.index[0], df_EV.index[-1], facecolor='r', alpha=0.25) start = pd.Timestamp(2016, 7, 1, 0, 0) end = pd.Timestamp(2016, 7, 1, 23, 59) #df_batterydata.index[0], df_batterydata.index[-1] ax.set_xlim(xmin=start, xmax=end) ax.xaxis.set_major_locator(HourLocator(arange(0, 25, 6))) #ax.xaxis.set_minor_locator(HourLocator(drange(0, 25, 6))) ax.xaxis.set_major_formatter(DateFormatter('%H:%M')) #ax.xaxis.set_major_locator(HourLocator(interval=1)) #ax.xaxis.set_minor_locator(HourLocator(drange(0, 25, 6))) #ax.xaxis.set_major_formatter(DateFormatter('%H:%M')) ppt.hlines(0, start, end, colors='k') ax.xaxis.set_major_locator(HourLocator(arange(0, 25, 6))) #ax.xaxis.set_minor_locator(HourLocator(drange(0, 25, 6))) ax.xaxis.set_major_formatter(DateFormatter('%H:%M')) #Legend lns = lns1 + lns2 labs = [l.get_label() for l in lns] L = ax.legend(lns, labs, bbox_to_anchor=(0.3, -0.4), loc='lower left', ncol=1) #L.get_texts()[0].set_text('Total system load') #L.get_texts()[1].set_text('Total unresponsive system load') ppt.savefig(results + '_vis/' + EV + '_bids.png', bbox_inches='tight') return
def plot_EVload(results,df_system,df_system2=None): fig = ppt.figure(figsize=(8,4),dpi=150) ppt.ioff() #House load ax = fig.add_subplot(111) lns1 = ax.plot(df_system['flex_EV'],'r',label='Total EV Load') try: lns2 = ax.plot(df_system2['flex_EV'],'k',label='Total EV Load 2') except: pass ax.set_ylabel('EV Load in [MW]') #ax.set_xlim(xmin=df_batterydata.index[0], xmax=df_batterydata.index[-1]) start = df_system.index[0] #pd.Timestamp(2016, 7, 1, 0, 0) end = df_system.index[-1] #pd.Timestamp(2016, 7, 1, 23, 59) #df_batterydata.index[0], df_batterydata.index[-1] ax.set_xlim(xmin=start, xmax=end) ax.set_ylim(ymin=0.0) ax.xaxis.set_major_locator(HourLocator(arange(0, 25, 24))) ax.xaxis.set_minor_locator(HourLocator(arange(0, 25, 6))) ax.xaxis.set_major_formatter(DateFormatter('%m-%d')) #ax.xaxis.set_major_locator(HourLocator(interval=1)) #ax.xaxis.set_minor_locator(HourLocator(drange(0, 25, 6))) #ax.xaxis.set_major_formatter(DateFormatter('%H:%M')) ppt.hlines(0, start, end, colors='k') #ax2 = ax.twinx() #ax2.set_ylabel('Local Retail Price [USD/MW]') #lns2 = ax2.plot(df_system['clearing_price'],'bx',label='Realtime Local Price') #ax.set_xlim(xmin=start, xmax=end) #ax.xaxis.set_major_locator(HourLocator(arange(0, 25, 24))) #ax.xaxis.set_minor_locator(HourLocator(drange(0, 25, 6))) #ax.xaxis.set_major_formatter(DateFormatter('%m-%d')) #Legend try: lns = lns1 + lns2 except: lns = lns1 labs = [l.get_label() for l in lns] L = ax.legend(lns, labs, bbox_to_anchor=(0.3, -0.2), loc='lower left', ncol=1) #L.get_texts()[0].set_text('Total system load') #L.get_texts()[1].set_text('Total unresponsive system load') if len(lns) == 1: ppt.savefig(results+'/EV_total_load.png', bbox_inches='tight') else: ppt.savefig(results+'/EV_total_load_comp.png', bbox_inches='tight') return
def plot(filename, title, espec, rehearsal=False): plt.style.use('pluto-paper') rcParams[ 'figure.autolayout'] = False # autolayout (default True in pluto-paper style) breaks these plots fig, ax = plt.subplots(figsize=figaspect(0.3)) if rehearsal: cbar_CH4 = None cbar_He, cbar_H = N_colorbars(fig, ax, 2, fraction=0.1) else: cbar_CH4, cbar_He, cbar_H = N_colorbars(fig, ax, 3, fraction=0.1) plot_espec(fig, ax, cbar_H, cbar_He, cbar_CH4, espec, mccomas=True, rehearsal=rehearsal) ax.set_ylabel('Energy/Q (eV/q)') if rehearsal: ax.set_title(title) ax.set_xlabel('Time (UTC)') ax.xaxis.set_major_locator(HourLocator()) ax.xaxis.set_major_formatter(DateFormatter('%H:%M')) ax.xaxis.set_minor_locator(MinuteLocator(byminute=range(0, 60, 10))) else: ax.set_title(title, pad=50) ax.set_xlabel('Time (UTC)') ax.set_xlabel('X ($R_p$)') ax.xaxis.set_major_locator(MultipleLocator(50)) ax.xaxis.set_minor_locator(MultipleLocator(10)) ax.set_xlim([-20, 105]) time_axis = ax.twiny() time_axis.set_xlim(spice_tools.et2pydatetime(espec['times'][0]), spice_tools.et2pydatetime(espec['times'][-1])) time_axis.set_xlabel('Time (UTC)') time_axis.xaxis.set_major_locator(HourLocator()) time_axis.xaxis.set_major_formatter(DateFormatter('%H:%M')) time_axis.xaxis.set_minor_locator( MinuteLocator(byminute=range(0, 60, 10))) if args.show: plt.show() else: fig.savefig(filename, bbox_inches='tight')
def plot_batteries(results, df_batterydata): fig = ppt.figure(figsize=(8, 4), dpi=150) ppt.ioff() #House load ax = fig.add_subplot(111) lns1 = ax.plot(df_batterydata['total_batt_load'], 'r', label='Total Battery Load') ax.set_ylabel('Battery Load in [kW]') #ax.set_xlim(xmin=df_batterydata.index[0], xmax=df_batterydata.index[-1]) start = df_batterydata.index[0] #pd.Timestamp(2016, 7, 1, 0, 0) end = df_batterydata.index[-1] #pd.Timestamp(2016, 7, 1, 23, 59) #df_batterydata.index[0], df_batterydata.index[-1] ax.set_xlim(xmin=start, xmax=end) ax.xaxis.set_major_locator(HourLocator(arange(0, 25, 6))) #ax.xaxis.set_minor_locator(HourLocator(drange(0, 25, 6))) ax.xaxis.set_major_formatter(DateFormatter('%H:%M')) #ax.xaxis.set_major_locator(HourLocator(interval=1)) #ax.xaxis.set_minor_locator(HourLocator(drange(0, 25, 6))) #ax.xaxis.set_major_formatter(DateFormatter('%H:%M')) ppt.hlines(0, start, end, colors='k') ax2 = ax.twinx() ax2.set_ylabel('Local Retail Price [USD/MW]') lns2 = ax2.plot(df_batterydata['clearing_price'], 'bx', label='Realtime Local Price') ax.set_xlim(xmin=start, xmax=end) ax.xaxis.set_major_locator(HourLocator(arange(0, 25, 6))) #ax.xaxis.set_minor_locator(HourLocator(drange(0, 25, 6))) ax.xaxis.set_major_formatter(DateFormatter('%H:%M')) #Legend lns = lns1 + lns2 labs = [l.get_label() for l in lns] L = ax.legend(lns, labs, bbox_to_anchor=(0.3, -0.4), loc='lower left', ncol=1) #L.get_texts()[0].set_text('Total system load') #L.get_texts()[1].set_text('Total unresponsive system load') ppt.savefig(results + '/battery_total_load.png', bbox_inches='tight') return
def __init__(self, master): self.master = master self.master.geometry("700x400+600+{:d}".format(200+PI_TOOLBAR+TK_HEADER)) self.master.title("Graphs Window") self.frame = Frame(self.master) # Load the local coordinates from file ABSPATH = os.path.abspath(os.path.dirname(sys.argv[0])) try: with open(ABSPATH+'/.lonlat.txt','r') as fileobj: coords = [] for line in fileobj: coords.append(line.rstrip()) except: print("You must create a file .lonlat.txt containing the coordinates to submit to NOAA") exit() self.lat = coords[1] self.lon = coords[0] # Create the NOAA object instance self.n = noaa.NOAA() self.have_forecast = False # Plot date formats self.alldays = DayLocator() self.quartdays = HourLocator(byhour=[0,6,12,18]) self.dayFormat = DateFormatter('%a %-m/%d')
def temp_ax_format(ax, tminmax, dates, utcoffset): ax.text(0.01, 0.92, sunrise_string(loc, dates[0], utcoffset), fontsize=10, transform=ax.transAxes) td = tminmax[1] - tminmax[0] if td >= 13.: ddegree = 3 elif td < 13. and td >= 5.: ddegree = 2 else: ddegree = 1 ax.set_yticks( np.arange(np.round(tminmax[0]) - 3, np.round(tminmax[1]) + 3, ddegree)) ax.set_ylim(np.round(tminmax[0]) - 3, np.round(tminmax[1]) + 3) ax.yaxis.set_major_formatter( FormatStrFormatter('%d' + u'\N{DEGREE SIGN}' + 'C')) # x axis lims, ticks, labels ax.set_xlim(dates[0], dates[-1]) ax.xaxis.set_minor_locator(HourLocator(np.arange(6, 25, 6))) # minor ax.xaxis.set_minor_formatter(DateFormatter("%Hh")) ax.get_xaxis().set_tick_params(which='minor', direction='out', pad=2, labelsize=6) ax.grid(alpha=0.2) # major weekdays not weekend in black ax.xaxis.set_major_locator(WeekdayLocator(byweekday=range(5))) ax.xaxis.set_major_formatter(DateFormatter(" %a\n %d %b")) plt.setp(ax.get_xticklabels(), ha="left") ax.get_xaxis().set_tick_params(which='major', direction='out', pad=10, labelsize=10) # major weekends in blue ax_weekend = ax.twiny() ax_weekend.set_xlim(dates[0], dates[-1]) ax_weekend.xaxis.set_major_locator(WeekdayLocator(byweekday=[5, 6])) ax_weekend.xaxis.set_major_formatter(DateFormatter(" %a\n %d %b")) ax_weekend.xaxis.tick_bottom() plt.setp(ax_weekend.get_xticklabels(), ha="left", color="C0") ax_weekend.get_xaxis().set_tick_params(which='major', direction='out', pad=10, labelsize=10) ax_weekend.grid(alpha=0.2) # remove labels at edges if dates[-1].hour < 13: # remove only if there is not enough space if dates[-1].weekday() > 4: # weekend ax_weekend.get_xticklabels()[-1].set_visible(False) else: # during the week ax.get_xticklabels()[-1].set_visible(False)
def images(datatype): db = TinyDB("sensor_log.json") Data = Query() data = db.search(Data.type == datatype) times = [ datetime.datetime.strptime(i[1]['time'], "%Y-%m-%d %H:%M:%S") for i in enumerate(data) ] datas = [i[1]['value'] for i in enumerate(data)] fig = Figure() ax = fig.add_subplot(111) ax.plot_date(times, datas, '-') ax.xaxis.set_major_locator(DayLocator()) ax.xaxis.set_major_formatter(DateFormatter("%m-%d %H:%M")) ax.xaxis.set_minor_locator(HourLocator()) ax.autoscale_view() ax.grid(True) fig.autofmt_xdate() canvas = FigureCanvas(fig) png_output = io.BytesIO() fig.savefig(png_output, format='png') response = make_response(png_output.getvalue()) response.headers['Content-Type'] = 'image/png' return response
def show_agg_avg_data(): path_to_data = "/home/mauk/Workspace/energy_prediction/data/prepared/aggregated_1415/agg121-p/use_vg_agg.csv" df = pd.read_csv(path_to_data) customdate = datetime.datetime(2016, 1, 1, 0, 0) y = df.iloc[:, 2] x = [ customdate + datetime.timedelta(minutes=15 * i) for i in range(len(y)) ] ax = plt.subplot() # removing top and right borders ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) # adds major gridlines ax.grid(color='grey', linestyle='-', linewidth=0.25, alpha=0.5) ax.plot(x, y, color='black', linewidth=0.6) ax.xaxis.set_major_locator(HourLocator(interval=3)) ax.xaxis.set_major_formatter(DateFormatter('%H:%M')) ax.xaxis.set_ticks([ customdate + datetime.timedelta(hours=i * 3) for i in range(int(24 / 3) + 1) ]) ax.set_xlabel("Time (15-min interval)") ax.set_ylabel("Average energy use [kWh]") plt.show()
def main(): print "start program" countTaxis() # a figure (chart) where we add the bar's and change the axis properties fig = figure() ax = fig.add_subplot(111) # set the width of the bar to interval-size barWidth = date2num(intervalDate + intervalDelta) - date2num(intervalDate) # add a bar with specified values and width ax.bar(date2num(barList.keys()), barList.values(), width=barWidth) # set the x-Axis to show the hours ax.xaxis.set_major_locator(HourLocator()) ax.xaxis.set_major_formatter(DateFormatter("%H:%M")) ax.xaxis.set_minor_locator(MinuteLocator()) ax.grid(True) xlabel('Zeit (s)') ylabel('Quantit' + u'\u00E4' + 't') title('Menge der Taxis im VLS-Gebiet') ax.autoscale_view() # shows the text of the x-axis in a way that it looks nice fig.autofmt_xdate() # display the chart show()
def draw_beautiful(x_plt, x0_plt, y_plt, plt_def): l = len(x_plt) t = np.linspace(0, 1, l - 2, endpoint=True) t = np.append([0, 0, 0], t) t = np.append(t, [1, 1, 1]) tck = [t, [x_plt, y_plt], 3] u3 = np.linspace(0, 1, (max(l * 2, 100)), endpoint=True) out = interpolate.splev(u3, tck) out[0] = np.array([ datetime.datetime.fromtimestamp(out[0][i]) for i in range(len(out[0])) ]) fig, ax = plt_def.subplots() ax.plot_date(x0_plt, y_plt, 'ro', label='Measured points', marker='o', markerfacecolor='green') ax.plot_date(out[0], out[1], 'b', linewidth=2.0, label='B-spline curve') ax.set_xlim(x0_plt[0], x0_plt[-1]) ax.xaxis.set_major_locator(HourLocator()) ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d %H:%M')) ax.fmt_xdata = DateFormatter('%Y-%m-%d %H:%M:%S') fig.autofmt_xdate() plt_def.legend(loc='best') plt_def.xlabel('Time (s)') plt_def.ylabel('Temperature (ºC)') plt_def.title('Temperature in object') plt_def.grid(True) plt_def.show()
def BatteryPlot(): with open("data/battery_log.txt", "r") as f: r = csv.reader(f) t_list = [] value_list = [] for row in r: t_list.append( datetime.datetime.strptime(row[0], '%Y-%m-%d %H:%M:%S.%f')) value_list.append(float(row[1])) f.close() if len(t_list) <= 1: return False t_list = date2num(t_list) fig, ax = plt.subplots() ax.plot_date(t_list, value_list, 'b-') ax.xaxis.set_major_locator(DayLocator()) ax.xaxis.set_minor_locator(HourLocator(np.arange(0, 25, 6))) ax.xaxis.set_major_formatter(DateFormatter('\n%m-%d')) ax.xaxis.set_minor_formatter(DateFormatter('%H:%M')) ax.fmt_xdata = DateFormatter('%Y-%m-%d %H:%M:%S') fig.autofmt_xdate() plt.title('Battery level') plt.ylabel('fuel level') plt.xlabel('time') plt.savefig('data/battery_plot.png') plt.clf() return True
def plot_val(var, start, end, path): """ Generate a graph of a given variable between the starting and the ending date. The graph is stored at the direction of the path, which can be a string or an IOString. """ a = get_values(var, start, end) a.vals = np.array(sorted(a.vals, key=lambda x: x[0])) x, y = a.vals[:, 0], a.vals[:, 1] fig, ax = plt.subplots() ax.plot_date(x, y, 'r-') ax.set_xlabel('Date') ax.set_ylabel(var['unit']) ax.xaxis.set_major_locator(DayLocator()) ax.xaxis.set_minor_locator(HourLocator(np.arange(0, 25, 6))) ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d')) ax.fmt_xdata = DateFormatter('%Y-%m-%d %H:%M:%S') fig.autofmt_xdate() plt.savefig(path, format='png') #print(get_single_value(v['T salon'], datetime.datetime.now()))
def datetime_plt(ax, x_dt, y, s=5, label=None, nmaj=8, Locator='Hour'): ax.plot_date(x_dt, y, markersize=s, label=label) #xmax = datetime.strptime(np.max(x_dt), '%Y-%m-%d %H:%M:%S') #xmin = datetime.strptime(row.t0,'%Y-%m-%d %H:%M:%S') xmax = np.max(x_dt) xmin = np.min(x_dt) run_hours = (xmax-xmin).total_seconds() / (60**2) interv_hours = int(run_hours // nmaj) run_minutes = run_hours * 60 interv_minutes = int(run_minutes // nmaj) if interv_hours == 0: interv_hours = 1 if interv_minutes == 0: interv_minutes = 1 if Locator == 'Hour': hours = HourLocator(interval=interv_hours) ax.xaxis.set_major_locator(hours) if Locator == 'Minute': minutes = MinuteLocator(interval=interv_minutes) ax.xaxis.set_major_locator(minutes) formatter = DateFormatter('%m-%d %H:%M') ax.xaxis.set_major_formatter(formatter) ax.xaxis.set_tick_params(rotation=30) return ax
def make_graph(humidities, dates_): firstdate = dates_[0] lastdate = dates_[-1] graph_title = "{} - {}".format(firstdate.strftime(DAY_FORMAT), lastdate.strftime(DAY_FORMAT)) debug_print("Making graph for {}".format(graph_title)) # create new plot fig, ax = plt.subplots() fig.set_size_inches(40.5, 20.5) fig.subplots_adjust(bottom=0.2) # configure axes ax.xaxis_date() ax.xaxis.set_major_locator(DayLocator()) ax.xaxis.set_minor_locator(HourLocator()) ax.xaxis.set_major_formatter(DateFormatter(DATE_FORMAT)) ax.yaxis.set_major_locator( plt.FixedLocator([ 10, 20, 30, 110, 120, 130, 140, 150, 160, 300, 350, 365, 380, 400, 430, 480 ])) ax.set_xlim(firstdate, lastdate) ax.set_ylim(10, 500) ax.autoscale_view() plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right') plt.xticks(rotation=15) # add data plt.plot(dates_, humidities, ".") plt.grid() plt.title(graph_title) return fig
def _animate(i: int): axes.clear() axes.xaxis.set_major_locator(HourLocator()) axes.xaxis.set_major_formatter(DateFormatter('%I:%M %p')) #axes.xaxis.set_minor_locator(MinuteLocator()) axes.set_ylim(-1, maxVal) axes.set_xlabel('Time', labelpad=12, fontdict={'size': 16}) axes.set_ylabel('#-of Messages Sent', labelpad=12, fontdict={'size': 16}) axes.set_title(title, pad=12) axes.tick_params(axis='x', which='major', labelsize=12, labelrotation=75, labelcolor='black') axes.tick_params(axis='y', which='major', labelsize=14, labelcolor='black') axes.plot(x[dataRange.getLow:(dataRange.getHigh + 1)], y[dataRange.getLow:(dataRange.getHigh + 1)], linestyle='-', color='tomato', linewidth=1.2, marker='o', markerfacecolor='red', markersize=5) #dataRange.setLow(dataRange.getHigh) dataRange.setHigh((dataRange.getHigh + 60))
def render_matplotlib_png(alert_router_id, points, start, end, step): """Render a graph PNG based on timeseries data using matplotlib. Args: alert_router_id: a alert_id-router_gid string points: timeseries traffic data points start: arrow object representing the start time of the alert end: arrow object reprsenting the end time of the alert step: the time period each entry in the timeseries data spans """ # every day days = DayLocator() # every hour hours = HourLocator(interval=9) delta = datetime.timedelta(seconds=step) # calculate x axis points based on step and start time dates = drange(start, end, delta) # using AGG fig = plt.figure(figsize=(10, 6)) ax = fig.add_subplot(111) ax.plot_date(dates, points, '.-') ax.grid(True) ax.xaxis.set_major_locator(days) ax.xaxis.set_minor_locator(hours) ax.xaxis.set_major_formatter(DateFormatter('%a')) ax.xaxis.set_minor_formatter(DateFormatter('%H:%M')) ax.set_xlabel('time') ax.set_ylabel('bps') ax.set_title('Router Traffic - {}'.format(alert_router_id)) fig.autofmt_xdate() fig.savefig('{}.png'.format(alert_router_id))
def plot_all_(df, axes): [d1, d2, d3, d4] = axes[0].plot(df[deb1]) axes[0].legend([d1, d2, d3, d4], deb1, loc='best') axes[0].set_title("Débit de fuite au joint 1 (Gamme Large)") axes[1].plot(df[deb2]) axes[1].set_title("Débit de fuite au joint 1 (Gamme Étroite)") axes[2].plot(df[tmp]) axes[2].set_title("Température eau joint 1 - 051PO") [t1, t2] = axes[3].plot(df[tmp2]) axes[3].legend([t1, t2], ["injection", "fuite"], loc='best') axes[3].set_title( "Température injection aux joints / Température fuites joint 1") axes[4].plot(df[deb3]) axes[4].set_title("Débit d'injection au joint") axes[4].set_facecolor("#d5e5e5") axes[5].plot(df[vit]) axes[5].set_title("Vitesse de la pompe") axes[5].set_facecolor("#d5e5e5") axes[6].plot(df[pre], 'b') axes[6].set_title("Pression (BAR)") axes[6].set_facecolor("#d1d1d1") axes[7].plot(df[pui], 'k') axes[7].set_title("Puissance Nominale (%)") axes[7].set_facecolor("#d1d1d1") axes[0].get_xaxis().set_ticks([]) hour_locator = HourLocator([0, 12]) axes[0].xaxis.set_major_locator(hour_locator) axes[0].xaxis.set_major_formatter(DateFormatter("%H:%M")) plt.tight_layout() plt.show()
def _mixing_layer_depth_timeseries(self): """Create a time series graph figure object of the mixing layer depth on the wind data date and the 6 days preceding it. """ fig = Figure((8, 3), facecolor='white') ax = fig.add_subplot(1, 1, 1) ax.set_position((0.125, 0.1, 0.775, 0.75)) predicate = np.logical_and( self.mixing_layer_depth['avg_forcing'].mpl_dates > date2num(self.config.data_date - datetime.timedelta(days=6)), self.mixing_layer_depth['avg_forcing'].mpl_dates <= date2num(self.config.data_date + datetime.timedelta(days=1))) mpl_dates = self.mixing_layer_depth['avg_forcing'].mpl_dates[predicate] dep_data = self.mixing_layer_depth['avg_forcing'].dep_data[predicate] ax.plot(mpl_dates, dep_data, color='magenta') ax.set_ylabel('Mixing Layer Depth [m]', color='magenta', size='x-small') # Add line to mark profile time profile_datetime = datetime.datetime.combine(self.config.data_date, datetime.time(12)) profile_datetime_line = ax.axvline(date2num(profile_datetime), color='black') ax.xaxis.set_major_locator(DayLocator()) ax.xaxis.set_major_formatter(DateFormatter('%j\n%d-%b')) ax.xaxis.set_minor_locator(HourLocator(interval=6)) for label in ax.get_xticklabels() + ax.get_yticklabels(): label.set_size('x-small') ax.set_xlim((int(mpl_dates[0]), math.ceil(mpl_dates[-1]))) ax.set_xlabel('Year-Day', size='x-small') fig.legend([profile_datetime_line], ['Profile Time'], loc='upper right', prop={'size': 'xx-small'}) return fig
def date_handler(time_data, ax, time_frame): ax.xaxis.set_major_locator(ticker.MaxNLocator(nbins=8)) if time_frame == 'year': ax.xaxis.set_major_formatter(DateFormatter('%Y')) elif time_frame == 'month': ax.xaxis.set_major_locator(MonthLocator(bymonth=range(1, 13), interval=_time_freq(time_data, 8, 30))) ax.xaxis.set_major_formatter(DateFormatter('%Y-%m')) elif time_frame == 'day': ax.xaxis.set_major_locator(DayLocator(bymonthday=range(1, 32), interval=_time_freq(time_data, 8, 1))) ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d')) elif time_frame == 'hour': ax.xaxis.set_major_locator(HourLocator(byhour=range(0, 24, 4))) ax.xaxis.set_major_formatter(DateFormatter('%H:%M')) elif time_frame == 'minute': ax.xaxis.set_major_locator(MinuteLocator(byminute=range(0, 60, 1), interval=60)) ax.xaxis.set_major_formatter(DateFormatter('%H:%M')) elif time_frame == 'second': ax.xaxis.set_major_locator(SecondLocator(bysecond=range(0, 60, 1), interval=10)) ax.xaxis.set_major_formatter(DateFormatter('%M:%S'))
def make_plot(self, output_file=None): # ------------------------------------------------------------------------- # Plot a day's weather # ------------------------------------------------------------------------- print(' Setting up plot for time range: {} to {}'.format( self.start.isoformat(), self.end.isoformat())) if self.today: print(' Will generate last hour plot for time range: {} to {}'.format( self.lhstart.isoformat(), self.lhend.isoformat())) self.dpi = self.kwargs.get('dpi', 72) self.fig = plt.figure(figsize=(20, 12), dpi=self.dpi) # self.axes = plt.gca() self.hours = HourLocator(byhour=range(24), interval=1) self.hours_fmt = DateFormatter('%H') self.mins = MinuteLocator(range(0, 60, 15)) self.mins_fmt = DateFormatter('%H:%M') self.plot_positions = [([0.000, 0.835, 0.700, 0.170], [0.720, 0.835, 0.280, 0.170]), ([0.000, 0.635, 0.700, 0.170], [0.720, 0.635, 0.280, 0.170]), ([0.000, 0.450, 0.700, 0.170], [0.720, 0.450, 0.280, 0.170]), ([0.000, 0.265, 0.700, 0.170], [0.720, 0.265, 0.280, 0.170]), ([0.000, 0.185, 0.700, 0.065], [0.720, 0.185, 0.280, 0.065]), ([0.000, 0.000, 0.700, 0.170], [0.720, 0.000, 0.280, 0.170]), ] self.plot_ambient_vs_time() self.plot_cloudiness_vs_time() self.plot_windspeed_vs_time() self.plot_rain_freq_vs_time() self.plot_safety_vs_time() self.plot_pwm_vs_time() self.save_plot(plot_filename=output_file)
def intervals(now): return { 'day': { 'start': now - timedelta(days=1), 'locator': HourLocator(byhour=Plotter.hour_locator(now)), 'formatter': DateFormatter('%H:%M'), 'width': 300, }, 'week': { 'start': now - timedelta(days=7), 'locator': DayLocator(bymonthday=Plotter.week_locator(now)), 'formatter': DateFormatter('%d'), 'width': 300, }, 'month': { 'start': now - timedelta(weeks=4), 'locator': DayLocator(interval=2), 'formatter': DateFormatter('%d'), 'width': 500, }, 'year': { 'start': now - timedelta(days=365), 'locator': MonthLocator(bymonthday=1, interval=1), 'formatter': DateFormatter('%m/%d'), 'width': 500, }, }
def temp_ax_format(ax,tminmax,dates): ax.text(0.01,0.92,sunrise_string(loc,dates[0]),fontsize=10,transform=ax.transAxes) ax.set_yticks(np.arange(np.round(tminmax[0])-3,np.round(tminmax[1])+3,3)) #TODO make automatic ax.set_ylim(np.round(tminmax[0])-3,np.round(tminmax[1])+3) #TODO make automatic ax.yaxis.set_major_formatter(FormatStrFormatter('%d'+u'\N{DEGREE SIGN}'+'C')) # x axis lims, ticks, labels ax.set_xlim(dates[0],dates[-1]) ax.xaxis.set_minor_locator(HourLocator(np.arange(0, 25, 6))) # minor ax.xaxis.set_minor_formatter(DateFormatter("%Hh")) ax.get_xaxis().set_tick_params(which='minor', direction='in',pad=-10,labelsize=6) ax.grid(alpha=0.15) ax.xaxis.set_major_locator(DayLocator()) # major ax.xaxis.set_major_formatter(DateFormatter(" %a\n %d %b")) for tick in ax.xaxis.get_majorticklabels(): tick.set_horizontalalignment("left") # remove labels at edges ax.get_xticklabels()[-1].set_visible(False) ax.get_xticklabels(which="minor")[-1].set_visible(False) ax.get_xticklabels(which="minor")[0].set_visible(False) # add vertical line after each sunday mondays = [d for d in dates if d.weekday() == 0 and d.hour == 0] for m in mondays: ax.plot([m,m],[-50,50],"k",lw=0.1)
def format_axes(self, ax=None): """ Summary: helper function for customizing graph display. Decription: * Create and set major/minor locators for graph's major/minor ticks. * Format dates/times of x axis. * Autoscale graph view. * Show grid on graph. """ # HourLocator creates major ticks every 2 hours in a 24 hour period hours = HourLocator(byhour=range(24), interval=2) ax.xaxis.set_major_locator(hours) # MinuteLocator creates minor ticks every 30 minutes in a 1 hour period minutes = MinuteLocator(byminute=range(60), interval=30) ax.xaxis.set_minor_locator(minutes) # Creates format 'Hour:MinutePeriod Month/Day/Year' x-axis ticks # Example: '5:30PM 9/23/15' time_fmt = DateFormatter('%H:%M%p %x') ax.xaxis.set_major_formatter(time_fmt) # Formats x-axis information 'Weekday Month Day Hour:MinutePeriod' # Example: 'Tuesday Sep 23 12:15PM' ax.fmt_xdata = DateFormatter('%A %b %d %H:%M%p') ax.autoscale_view() ax.grid(True)