def buildDeltaZoneGraph(start_limit=0, stop_limit=0, zones=0, clean=False, save_to_pdf=False): zone_names = [ "Curtailment", "Core Zone", "Zone 1", "Zone 1A", "Zone 2", "Zone 2A", "Zone 2B", "Zone 3", "Zone 4", "Zone 4A" ] file_name = "delta-zone-" + start_limit + "-" + stop_limit if clean: file_name += "-cleaned" df = pp.getSingleDataframe(start_limit, stop_limit, fromPickle=True, clean=clean, cleanGlitches=False) if start_limit != 0: start_limit = datetime.strptime(start_limit, '%Y-%m-%d').timestamp() if stop_limit != 0: stop_limit = datetime.strptime(stop_limit, '%Y-%m-%d').timestamp() # Adjust the amount of ticks to the data size if stop_limit - start_limit > 86400 * 8: tick_zoom = 24 elif stop_limit - start_limit > 86400 * 4: tick_zoom = 12 elif stop_limit - start_limit > 86400 * 2: tick_zoom = 6 elif stop_limit - start_limit > 86400: tick_zoom = 3 else: tick_zoom = 1 if zones != 0: zone_names = zones # Generate x,y data for the mesh plot curtailments = np.zeros(shape=(len(zone_names), len(df.index))) for i, zone in enumerate(zone_names): for j, status in enumerate(df[zone]): curtailments[i, j] = status curtailments = curtailments[:, :-1] # Generate x ticks for the mesh plot meshxticks_major = [] meshxticks_minor = [] for i, d in enumerate(df.index): if d.hour == 0 and d.minute == 0: meshxticks_major.append(i) elif d.hour % tick_zoom == 0 and d.minute == 0: meshxticks_minor.append(i) fig = plt.figure() # Bottom plot ax1 = fig.add_axes([0.1, 0.08, 0.9, 0.45]) delta = (df["Generation"] - df["Demand"]) #.rolling(3).mean() ax1.plot(df.index, delta, "k-", linewidth=1, alpha=0.8) plt.fill_between(df.index, delta, color="k", alpha=0.3) ax1.set_xlabel("Time") ax1.margins(x=0) ax1.set_ylabel("MegaWatt") ax1.set_ylim(-25, 25) ax1.set_yticks([-25, -20, -15, -10, -5, 0, 5, 10, 15, 20, 25]) ax1.grid(b=True, which="both", axis="y") ax1.xaxis.set_major_locator(mdates.DayLocator()) ax1.xaxis.set_minor_locator(mdates.HourLocator()) ax1.xaxis.set_major_formatter(mdates.DateFormatter("%b %d")) ax1.xaxis.set_minor_formatter(mdates.DateFormatter("%H:00")) for i, t in enumerate(ax1.xaxis.get_minor_ticks()): if i % 24 == 0: t.label.set_visible(False) if i % tick_zoom != 0: t.set_visible(False) ax1.tick_params(axis="x", which="minor") ax1.grid(b=True, which="major", axis="x", linestyle="-.") ax1.grid(b=True, which="minor", axis="x", linestyle="--") ax1.legend(["Generation relative to Demand"], loc=1, fancybox=True, framealpha=0.5) # Top plot cm = plt.get_cmap("OrRd") ax2 = fig.add_axes([0.1, 0.55, 0.9, 0.45]) ax2.pcolormesh(curtailments, alpha=1, cmap=cm, snap=True) ax2.set_xticks(meshxticks_major) ax2.set_xticks(meshxticks_minor, minor=True) ax2.xaxis.set_ticklabels([]) ax2.grid(b=True, which="major", axis="x", linestyle="-.") ax2.grid(b=True, which="minor", axis="x", linestyle="--") #ax2.set_ylabel("Zones") ax2.set_yticks(np.arange(len(zone_names)) + 0.5) ax2.set_yticks(np.arange(len(zone_names)), minor=True) ax2.set_yticklabels(zone_names, rotation=0, va="center") ax2.grid(b=True, which="minor", axis="y") custom_lines = [ Line2D([0], [0], color=cm(0), lw=4), Line2D([0], [0], color=cm(.5), lw=4), Line2D([0], [0], color=cm(1.), lw=4) ] ax2.legend(custom_lines, [ "No curtailment in zone", "Partial curtailment in zone", "Full stop in zone" ], loc=1, fancybox=True, framealpha=0.5) fig.autofmt_xdate(which="both") fig.set_size_inches(8, 4.5) plt.xticks(rotation=-60) if save_to_pdf: fig.savefig("./plots/" + file_name + ".pdf") else: plt.show() plt.clf()
Ebin = E.resample('60s').mean() Nbin = N.resample('60s').mean() Wbin = W.resample('60s').mean() Tbin = T.resample('1s').mean() TTbin = TT.resample('60s').mean() Sbin = S.resample('60s').mean() Dbin = D.resample('60s').mean() fs = 1 / 2.0 # sample rate raw, Hz fs_bin = 1 / 10.0 # sample rate binned, Hz # Remove "barotropic" currents (MATRIX IS TRANSPOSED!) Ebin = Ebin.sub(Ebin.mean(axis=1), axis=0) Nbin = Nbin.sub(Nbin.mean(axis=1), axis=0) days = dates.DayLocator() min15 = dates.HourLocator(interval=1) dfmt = dates.DateFormatter('%H:%M') min1 = dates.MinuteLocator(interval=15) # Cut timeseries Ebin = Ebin.loc[(Ebin.index >= XLIM[0]) & (Ebin.index <= XLIM[1])] Nbin = Nbin.loc[(Nbin.index >= XLIM[0]) & (Nbin.index <= XLIM[1])] Wbin = Wbin.loc[(Wbin.index >= XLIM[0]) & (Wbin.index <= XLIM[1])] Tbin = Tbin.loc[(Tbin.index >= XLIM[0]) & (Tbin.index <= XLIM[1])] TTbin = TTbin.loc[(TTbin.index >= XLIM[0]) & (TTbin.index <= XLIM[1])] Sbin = Sbin.loc[(Sbin.index >= XLIM[0]) & (Sbin.index <= XLIM[1])] Dbin = Dbin.loc[(Dbin.index >= XLIM[0]) & (Dbin.index <= XLIM[1])] # Compute SW properties N2 = sw.bfrq(Sbin.mean(), TTbin.mean(), TTbin.columns, 55)[0] zVecN2 = (zVecSBE[1:] + zVecSBE[:-1]) / 2
temp_dt = dateutil.parser.parse(str(i)) + time_add n.append(temp_dt) return (n) for k in traintimes: temp_dict = dict() temp_dict = convert_dates(traintimes[k]) traintimes[k] = temp_dict fig, ax = plt.subplots(figsize=figsize) # styling hours = mdates.HourLocator(interval=vertical_hour_ticks_interval) hours_fmt = mdates.DateFormatter(horizontal_axis_label_format) plt.title(title) # station labels generate station_names = list() station_pks = list() for elem in sorted(stations.items()): print(elem[0], " ::", elem[1]) station_names.append(elem[1]) station_pks.append(elem[0]) plt.yticks(station_pks) ax.set_yticklabels(station_names) ax.set_xlim(x_bounds)
import logging import datetime as dt import numpy as np import matplotlib.pyplot as plt import matplotlib.dates as mdates from ooidac.processing.ctd import ctd_data from configuration import PROC_LON_VAR, PROC_LAT_VAR, PROC_PRES_VAR, PROC_TIME_VAR from configuration import CONDUCTIVITY_SENSOR, TEMPERATURE_SENSOR, DEPTH_SENSOR from configuration import PRESSURE_SENSOR import ooidac.processing as process_dba from ooidac.data_classes import DbaData from ooidac.profiles import Profiles _days_formatter = mdates.DayLocator() _hours_formatter = mdates.HourLocator() _days_format = mdates.DateFormatter('%m/%d') _hours_format = mdates.DateFormatter('%H:%M') ctd_sensors = [ PROC_LAT_VAR, PROC_LON_VAR, PROC_PRES_VAR, TEMPERATURE_SENSOR, CONDUCTIVITY_SENSOR ] def plot_profiles_for_dba(dba_file, unfiltered=False): if not os.path.isfile(dba_file): logging.error('Invalid dba file specified: {:s}'.format(dba_file)) return logging.debug('Parsing dba file: {:s}'.format(dba_file))
y = zip(*data)[1] ## Print data plt.ioff() fig = plt.figure() ax1 = fig.add_subplot(111) ax1.set_title(u'Parvekelämpötila') ax1.set_xlabel('Aika') ax1.set_ylabel(u'Lampotila °C') #ax1.set_xticks(x) # Tickmark + label at every plotted point ax1.xaxis.set_minor_locator(mdates.HourLocator(byhour=range(0, 24, 3))) ax1.xaxis.set_major_locator(mdates.DayLocator()) ax1.xaxis.set_minor_formatter(mdates.DateFormatter('%H:%M')) ax1.xaxis.set_major_formatter(mdates.DateFormatter('\n%d/%m/%Y')) ax1.plot_date(x, y, ls='-', marker="") ax1.grid(b=True, which='major') ax1.grid(b=True, which='minor', linestyle='--') #fig.autofmt_xdate(rotation=70) fig.tight_layout() # add avg as horizontal line ax1.axhline(y=avg, color='k', linestyle='--', linewidth=1)
def saveplot(present: dict, whitelist: Whitelist, dt_min: datetime, dt_max: datetime, strict_xlim: bool = True): """ Save a plot of present names :param whitelist: Whitelist from whitelist_handler :param present: Dict of name:datetimes entries :param dt_min: Minimum datetime :param dt_max: Maximum datetime :param strict_xlim: """ fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(16, 9), gridspec_kw={'height_ratios': [6, 1]}) plt.subplots_adjust(bottom=0.05, top=0.95, hspace=0.15) # ax1.spines["top"].set_visible(False) # ax1.spines["right"].set_visible(False) # plt.title(dt_min.strftime('%d-%m-%Y %H:%M') + ' - ' + dt_max.strftime('%d-%m-%Y %H:%M')) # Configure the x-axis such that it takes dates. # fig.autofmt_xdate() ax1.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M')) ax1.xaxis.set_major_locator(mdates.HourLocator()) # if dt_max - dt_min <= datetime.timedelta(hours=4): # ax1.xaxis.set_major_locator(mdates.MinuteLocator(byminute=[0, 15, 30, 45])) # # elif dt_max - dt_min <= datetime.timedelta(hours=24): # ax1.xaxis.set_major_locator(mdates.HourLocator()) # # else: # # plt.gcf().autofmt_xdate(rotation=30, ha='center') # ax1.xaxis.set_major_locator(mdates.DayLocator()) # ax1.xaxis.set_minor_locator(mdates.HourLocator(byhour=12)) # ax1.xaxis.set_major_formatter(ticker.NullFormatter()) # ax1.xaxis.set_minor_formatter(mdates.DateFormatter('%A\n%d-%m-%y')) # plt.gca().xaxis.set_major_locator(mdates.HourLocator()) ax1.yaxis.set_ticks_position('left') ax1.xaxis.set_ticks_position('bottom') # For each name, plot its datetimes. # Each name get's a unique y coordinate. color = itertools.cycle(cm.tab10(np.linspace(0, 1, cm.tab10.N))) for i, id_ in enumerate(present): path_effects = [] if 'color' in whitelist.names[id_]: c = whitelist.names[id_]['color'] else: c = next(color) y = [i + 1, i + 1] lw = 15 if 'outline' in whitelist.names[id_]: path_effects.append(pe.withStroke(linewidth=lw, foreground='k')) lw -= 3 for a in present[id_]: # print(id_, a) ax1.plot(a, y, markersize=8, linewidth=lw, solid_capstyle='round', color=c, path_effects=path_effects) # Use the names as yticks ax1.set_yticks(range(1, len(present) + 1)) ax1.set_yticklabels([whitelist.names[id_]['name'] for id_ in present]) if strict_xlim or not present: ax1.set_xlim(dt_min, dt_max) ax1.set_ylim(0, len(present) + 1) ax1.grid() if not present: print('NO DATA') fig.text(0.5, 0.5, 'NO DATA', size=50, ha="center", va="bottom", color='#aaaaaa', weight='bold') ax2.text(-100, 250, 'Sponsored by', size=20, ha="right", va="bottom", color='#68686d', weight='bold') im = image.imread('./nedap.png') ax2.axis('off') ax2.imshow(im) # size = fig.get_size_inches()*300 # # plt.imshow(im, aspect='auto', zorder=-1) # print(size) # ax.figure.figimage(im, size[0]*0.5, 100, zorder=1) plt.savefig('slide_tmp.png', dpi=300) plt.close('all') print('Moving file') copy2('slide_tmp.png', 'slide.png') return
def plot_tg_results(tg, ylimit): # plot = plt.figure() if plot_VCDs: whattoplot1 = 'VCD(' + tg + ')' whattoplot = fitwindow + '.VCol(' + tg + ')' error = fitwindow + '.VErr(' + tg + ')' plottitle = inst + ' ' + tg + ' VCD ' + fitwindow + ' nm' ylabel = 'VCD (molec./cm^2)' ylim = ylimit f = a2.plot(x='Date_Time', y=whattoplot, yerr=error, fontsize=fontsize, figsize=figsize, style='r.', ylim=ylim, label='20deg') f.set_title(plottitle, fontsize=fontsize) f.set_xlabel('Date_Time', fontsize=fontsize) f.set_ylabel(ylabel, fontsize=fontsize) f.set_xlim(startdate, enddate) #f.xaxis.set_major_locator(x_label_format) f.legend(['20deg VCD'], bbox_to_anchor=legend_pos) f.xaxis.set_major_locator(dates.HourLocator(interval=6)) f.xaxis.set_major_formatter(hfmt) else: if tg == 'RMS': whattoplot = fitwindow + '.RMS' whattoplot1 = 'RMS' yerror = 0 elif tg == 'shift_tg': whattoplot = fitwindow + '.Shift(O4)' whattoplot1 = 'Shift' yerror = fitwindow + '.Err Shift(O4)' elif tg == 'shift_spec': whattoplot = fitwindow + '.Shift(Spectrum)' whattoplot1 = 'Shift' yerror = fitwindow + '.Err Shift(Spectrum)' else: whattoplot1 = 'SCD(' + tg + ')' whattoplot = fitwindow + '.SlCol(' + tg + ')' yerror = fitwindow + '.SlErr(' + tg + ')' plottitle = inst + ' ' + tg + ' dSCD ' + fitwindow ylabel = 'dSCD (molec./cm^2)' ylim = ylimit if show_error == True: f = a1.plot(x='Date_Time', y=whattoplot, fontsize=fontsize, figsize=figsize, style='y.', ylim=ylim, yerr=yerror, label='20deg') a3.plot(x='Date_Time', y=whattoplot, yerr=yerror, ax=f, style='b.', label='3deg', ylim=ylim) a5.plot(x='Date_Time', y=whattoplot, yerr=yerror, ax=f, style='g.', label='5deg', ylim=ylim) a10.plot(x='Date_Time', y=whattoplot, yerr=yerror, ax=f, style='c.', label='10deg', ylim=ylim) a20.plot(x='Date_Time', y=whattoplot, yerr=yerror, ax=f, style='m.', label='20deg', ylim=ylim) a30.plot(x='Date_Time', y=whattoplot, yerr=yerror, ax=f, style='r.', label='30deg', ylim=ylim) #a90.plot(x='Date_Time', y=whattoplot, yerr=yerror, ax=f, style='k.', # label='90deg', ylim=ylim) else: f = a1.plot(x='Date_Time', y=whattoplot, fontsize=fontsize, figsize=figsize, style='y.', ylim=ylim, label='20deg') a3.plot(x='Date_Time', y=whattoplot, ax=f, style='b.', label='3deg', ylim=ylim) a5.plot(x='Date_Time', y=whattoplot, ax=f, style='g.', label='5deg', ylim=ylim) a10.plot(x='Date_Time', y=whattoplot, ax=f, style='c.', label='10deg', ylim=ylim) a20.plot(x='Date_Time', y=whattoplot, ax=f, style='m.', label='20deg', ylim=ylim) a30.plot(x='Date_Time', y=whattoplot, ax=f, style='r.', label='30deg', ylim=ylim) #a90.plot(x='Date_Time', y=whattoplot, ax=f, style='k.', # label='90deg', ylim=ylim) f.set_title(plottitle, fontsize=fontsize) f.set_xlabel('Date_Time', fontsize=fontsize) f.set_ylabel(ylabel, fontsize=fontsize) f.set_xlim(startdate, enddate) #f.xaxis.set_major_locator(x_label_format) f.legend(['2deg', '3deg', '5deg', '10deg', '20deg', '30deg'], bbox_to_anchor=legend_pos) f.xaxis.set_major_locator(dates.HourLocator(interval=hourinterval)) f.xaxis.set_major_formatter(hfmt) if save_plot: fig = f.get_figure() fig.savefig(filepath + filetosave + whattoplot1 + str(start_day) + str(start_month) + str(an_year) + '_' + str(end_day) + str(end_month) + str(an_year) + '_' + fitwindow + '.pdf', bbox_inches='tight')
def make_plot(self, i): """Start the figure and plot permanent data""" # create canvas fig = plt.figure(figsize=(10,8)) # ax0 = fig.add_axes([0.05, 0.50, 0.42, 0.45]) ax1 = fig.add_axes([0.55, 0.82, 0.40, 0.13]) ax2 = fig.add_axes([0.55, 0.66, 0.40, 0.13]) ax3 = fig.add_axes([0.55, 0.50, 0.40, 0.13]) # bx1 = fig.add_axes([0.05, 0.33, 0.90, 0.10]) bx2 = fig.add_axes([0.05, 0.23, 0.90, 0.10]) bx3 = fig.add_axes([0.05, 0.13, 0.90, 0.10]) # cx0 = fig.add_axes([0.05, 0.03, 0.90, 0.10]) ax1.set_title(self.title) ax1.plot(self.time, self.Wspd, c="k") ax2.plot(self.time, self.Hs, c="k") ax3.plot(self.time, self.Tp, c="k") # ax1.set_ylabel("$U_{10}\,\mathrm{[m/s]}$") ax2.set_ylabel("$H_{m0}\,\mathrm{[m]}$") ax3.set_ylabel("$T_{p}\,\mathrm{[s]}$") # self.set_limits(self.Wspd, ax1) self.set_limits(self.Hs, ax2) self.set_limits(self.Tp, ax3) # for ax in (ax1, ax2, ax3): ax.yaxis.tick_right() ax.yaxis.set_major_locator(plt.MaxNLocator(4)) ax.yaxis.set_label_position("right") ax.yaxis.set_ticks_position("both") ax.xaxis.set_minor_locator(mdates.HourLocator(range(0,24,3))) ax.xaxis.set_major_locator(mdates.DayLocator(interval=1)) ax.xaxis.set_major_formatter(mdates.DateFormatter("%b %d\n%Y")) ax1.set_xticklabels(['']) ax2.set_xticklabels(['']) # # plot high frequency data try: self.get_high_frequency_data(i) # bx1.plot(self.t_wind, self.u_wind, color="0.9") bx2.plot(self.t_wind, self.v_wind, color="0.9") bx3.plot(self.t_wind, self.w_wind, color="0.9") # bx1.set_ylim((-20,20)) # bx2.set_ylim((-20,20)) # bx3.set_ylim((-5,5)) # n_smooth = 200 bx1.plot(self.t_wind[::n_smooth], self.u_wind[::n_smooth]) bx2.plot(self.t_wind[::n_smooth], self.v_wind[::n_smooth]) bx3.plot(self.t_wind[::n_smooth], self.w_wind[::n_smooth]) # bx1.set_ylabel("$u\,\mathrm{[m/s]}$") bx2.set_ylabel("$v\,\mathrm{[m/s]}$") bx3.set_ylabel("$w\,\mathrm{[m/s]}$") # cx0.plot(self.t_wave, self.Z[:,self.p.valid_wires_index]) cx0.legend(self.p.valid_wires, loc=0, ncol=6) cx0.set_ylabel("$\\eta\,\mathrm{[m]}$") # cx0.set_ylim((-2,2)) # for ax in (bx1, bx2, bx3, cx0): ax.yaxis.set_label_position("right") except: pass # plot wave spectrum self.plot_wave_spectrum(i, ax0) point1, = ax1.plot(self.time[i], self.Wspd[i], "oy", ms=3) point2, = ax2.plot(self.time[i], self.Hs[i], "oy", ms=3) point3, = ax3.plot(self.time[i], self.Tp[i], "oy", ms=3) return fig, ax0, ax1, ax2, ax3
# -*- coding: utf-8 -*- import numpy as np import pandas as pd from datetime import datetime import matplotlib.pyplot as plt import matplotlib.dates as mdates plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签 plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号 fig = plt.figure(figsize=(12, 6)) # 定义图并设置画板尺寸 fig.set(alpha=0.2) # 设定图表颜色alpha参数 # # fig.tight_layout() # 调整整体空白 # plt.subplots_adjust(bottom=0.06,top=0.94,left=0.08,right=0.94,wspace =0.36, hspace =0.5) # 设置作图范围、子图间距。 df_milano = pd.read_csv("milano_270615.csv") x1 = df_milano['day'].values x1 = [datetime.strptime(d, '%Y-%m-%d %H:%M:%S') for d in x1] y1 = df_milano['temp'] ax = fig.add_subplot(111) plt.xticks(rotation=70) ax.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M")) ax.xaxis.set_major_locator(mdates.HourLocator()) # ax.set_xticks(x1) # plt.gcf().autofmt_xdate() # 自动旋转日期标记 ax.plot(x1, y1, 'r') plt.show()
def _print_prediction(test_index, y_actual, y_forecast, fig_title, date_slice=None, whole=False): """ Print the forecasted vs. ground truth values into a graph. Parameters: test_index (Keras model): the model with the trained state. y_actual: original ground truth values. y_forecast: forecasted values. fig_title (string): name for figure and for file to print to. date_slice (tuple(int, int)): range of hours within the dataset to consider. whole (boolean): if to slice the dataset or consider it completely (separate option because of the need for axis lables change). """ # Prediction Figure plt.figure(figsize=(16, 12)) if not whole: if date_slice is None: date_slice = (24 * 10, 24 * 15) dts = list( map(lambda d: datetime.datetime.strptime(d, DATETIME_FORMAT), test_index)) fds = dates.date2num(dts) hfmt = dates.DateFormatter('%m/%d %Hh') ax = plt.gca() ax.plot(fds[date_slice[0]:date_slice[1]], y_actual[date_slice[0]:date_slice[1]], color='blue', linewidth=1, label='ground truth') ax.plot(fds[date_slice[0]:date_slice[1]], y_forecast[date_slice[0]:date_slice[1]], color='red', linewidth=1, label='prediction') if date_slice[1] - date_slice[0] < 25: ax.xaxis.set_major_locator(dates.HourLocator(interval=1)) else: ax.xaxis.set_major_locator(plt.MaxNLocator(24)) ax.xaxis.set_major_formatter(hfmt) plt.setp(ax.get_xticklabels(), rotation=80, fontsize=20) plt.setp(ax.get_yticklabels(), fontsize=20) else: ax = plt.gca() ax.xaxis.set_major_locator(plt.MaxNLocator(6)) ax.plot(y_actual, color='blue', linewidth=1, label='Ground Truth') ax.plot(y_forecast, color='red', linewidth=1, label='Prediction') plt.grid(True) plt.legend(prop={'size': 26}) if fig_title is not None: figfile_name = '{}.eps'.format(fig_title) plt.savefig(os.path.join(FIGS_DATA_DIR, figfile_name), quality=100, format='eps', pad_inches=0.25, bbox_inches='tight') else: plt.show()
def run_print_features(file_name='10001_aq_series.csv', columns=None, save_fig=False, fig_title=None, sliced=None): """ Plots the features of a dataset for a graphical view of the PDF of each variable. :param file_name: CSV file from which to load the variables time series. :type file_name: string :param columns: the columns from the CSV file to include in the picture. :type columns: string[] :param save_fig: if to save the plot to a picture file. :type save_fig: boolean :param fig_title: the name of the plot and file name if saved. :type fig_title: string :param sliced: index range to consider when plotting the time series :type sliced: tuple(int, int) """ dataset = get_time_series(file_name, columns) values = dataset.values # specify columns to plot groups = list(range(0, len(dataset.columns))) i = 1 # plot each column fig, ax = plt.subplots(len(groups), 1, sharex=True, figsize=(12, 6)) if sliced: dts = list( map(lambda d: datetime.datetime.strptime(d, DATETIME_FORMAT), dataset.index)) fds = dates.date2num(dts) # hfmt = dates.DateFormatter('%m/%d %Hh') hfmt = dates.DateFormatter('%Y/%m/%d') date_slice = sliced for group in groups: if sliced: ax[group].plot(fds[date_slice[0]:date_slice[1]], values[date_slice[0]:date_slice[1], group], label=dataset.columns[group]) # ax[group].xaxis.set_label('Date and hour') # ax[group].yaxis.set_label('# of Cars') if (date_slice[1] - date_slice[0]) <= 300: ax[group].xaxis.set_major_locator( dates.HourLocator(interval=6)) else: ax[group].xaxis.set_major_locator(plt.MaxNLocator(6)) ax[group].xaxis.set_major_formatter(hfmt) # plt.setp(ax[group].get_xticklabels(), rotation=80) else: ax[group].plot(values[:, group], label=dataset.columns[group]) ax[group].xaxis.set_label('Time') # ax[group].legend(frameon=False) ax[group].grid(True) col_name = dataset.columns[group] ax[group].set_title(col_name, fontsize=20, x=1.01, y=0.4, loc='right', ha='left') i += 1 if save_fig: if fig_title is None: fig_title = file_name.split('.csv')[0] else: fig_title = fig_title.lower().replace(' ', '_') figfile_name = '{}_{}_ATTRS.eps'.format( fig_title, 'WHOLE' if not sliced else 'SLICED') plt.savefig(os.path.join(FIGS_DATA_DIR, figfile_name), quality=100, format='eps', pad_inches=0.25, bbox_inches='tight') else: plt.show()
i = 4 h = 10 n = 150 df = pd.read_csv(f'csv_merged/Horário/Residential_{i}.csv', index_col='date') df.pop('weather') mm = media_movel(df, h) fig = plt.figure(figsize=(10, 5)) ax = fig.add_subplot(1, 1, 1) plt.plot(df.index[:n], df.energy_kWh[:n], 'k', label="Observado") plt.plot(mm.index[:n], mm.energy_kWh[:n], 'g', label=f"Média móvel {h}Hrs") ax.xaxis.set_major_locator(mdates.HourLocator(interval=n * 2)) fig.autofmt_xdate() plt.xlabel("Período") plt.ylabel("energia (kWh)") plt.title(f"Comparação com média móvel casa {i}") plt.legend() plt.savefig(f"Resultados/Comparações/MediaMovel{h}-casa{i}-{n}.png", bbox_inches='tight', dpi=400) plt.savefig(f"Resultados/Comparações/MediaMovel{h}-casa{i}-{n}.svg", bbox_inches='tight')
def buildModelGraph(start_limit=0, stop_limit=0, zones=0, filename="model-comparison", save_to_pdf=False): df_eday = pp.getEdayData() #Full DataSet, used for training try: df_full = pp.getSingleDataframe("2018-12-01", "2019-03-01", fromPickle=True) except FileNotFoundError: df_full = pp.getSingleDataframe("2018-12-01", "2019-03-01", fromPickle=False) df_full = df_full.join(df_eday, how="inner") df_full = pp.cleanData(df_full) df_full = pp.addReducedCol(df_full, clean=True) df_full = pp.removeGlitches(df_full) try: df = pp.getSingleDataframe(start_limit, stop_limit, fromPickle=True) except FileNotFoundError: df = pp.getSingleDataframe(start_limit, stop_limit, fromPickle=False) #df = df.join(df_eday, how="inner") df = pp.cleanData(df) df = pp.addReducedCol(df, clean=True) df = pp.removeGlitches(df) if start_limit != 0: start_limit = datetime.strptime(start_limit, '%Y-%m-%d').timestamp() if stop_limit != 0: stop_limit = datetime.strptime(stop_limit, '%Y-%m-%d').timestamp() # Adjust the amount of ticks to the data size if stop_limit - start_limit > 86400 * 8: tick_zoom = 24 elif stop_limit - start_limit > 86400 * 4: tick_zoom = 12 elif stop_limit - start_limit > 86400 * 2: tick_zoom = 6 elif stop_limit - start_limit > 86400: tick_zoom = 3 else: tick_zoom = 1 model_names, accs = desc.evaluateDataframe(df_full, df) accs = accs[:, :-1] # Generate x ticks for the mesh plot meshxticks_major = [] meshxticks_minor = [] for i, d in enumerate(df.index): if d.hour == 0 and d.minute == 0: meshxticks_major.append(i) elif d.hour % tick_zoom == 0 and d.minute == 0: meshxticks_minor.append(i) plt.xticks(rotation=-60) fig = plt.figure() # Bottom plot ax1 = fig.add_axes([0.10, 0.1, 0.9, 0.44]) delta = (df["Generation"] - df["Demand"]) #.rolling(3).mean() ax1.plot(df.index, delta, "k-", linewidth=1, alpha=0.8) plt.fill_between(df.index, delta, color="k", alpha=0.3) ax1.margins(x=0) ax1.set_ylabel("MegaWatt") ax1.set_ylim(-25, 25) ax1.set_yticks([-20, -10, 0, 10, 20]) ax1.grid(b=True, which="both", axis="y") ax1.xaxis.set_major_locator(mdates.DayLocator()) ax1.xaxis.set_minor_locator(mdates.HourLocator()) ax1.xaxis.set_major_formatter(mdates.DateFormatter("%b %d")) ax1.xaxis.set_minor_formatter(mdates.DateFormatter("%H:00")) for i, t in enumerate(ax1.xaxis.get_minor_ticks()): if i % 24 == 0: t.label.set_visible(False) if i % tick_zoom != 0: t.set_visible(False) ax1.tick_params(axis="x", which="minor") ax1.grid(b=True, which="major", axis="x", linestyle="-.") ax1.grid(b=True, which="minor", axis="x", linestyle="--") ax1.legend(["Generation relative to Demand"], loc=1) # Top plot cm = plt.get_cmap("binary") ax2 = fig.add_axes([0.10, 0.56, 0.9, 0.44]) ax2.pcolormesh(accs, alpha=1, cmap=cm, snap=True) ax2.set_xticks(meshxticks_major) ax2.set_xticks(meshxticks_minor, minor=True) ax2.xaxis.set_ticklabels([]) ax2.grid(b=True, which="major", axis="x", linestyle="-.") ax2.grid(b=True, which="minor", axis="x", linestyle="--") ax2.set_yticks(np.arange(len(model_names)) + 0.5) ax2.set_yticks(np.arange(len(model_names)), minor=True) ax2.set_yticklabels(model_names, rotation=0, fontsize="8", va="center") ax2.grid(b=True, which="minor", axis="y") custom_lines = [ Line2D([0], [0], color=cm(0), lw=4), Line2D([0], [0], color=cm(1.), lw=4) ] ax2.legend(custom_lines, ["No curtailment", "Curtailment"], loc=1) #plt.title("Generation relative to demand for all of Orkney. \nAccuracies for models: " + ", ".join(model_names)) fig.autofmt_xdate(which="both") fig.set_size_inches(8, 3) if save_to_pdf: fig.savefig("./plots/" + filename + ".pdf") else: plt.show() plt.clf()
def buildFirmNotFirmGraph(start_limit=0, stop_limit=0, zones=0, clean=False, save_to_pdf=False): zone_names = [ "Core Zone", "Zone 1", "Zone 1A", "Zone 2", "Zone 2A", "Zone 2B", "Zone 3", "Zone 4", "Zone 4A" ] file_name = "firm-not-firm-" + start_limit + "-" + stop_limit if clean: file_name += "-cleaned" try: df = pp.getSingleDataframe(start_limit, stop_limit, fromPickle=True, clean=clean, cleanGlitches=False) except FileNotFoundError: df = pp.getSingleDataframe(start_limit, stop_limit, fromPickle=False, clean=clean, cleanGlitches=False) if start_limit != 0: start_limit = datetime.strptime(start_limit, '%Y-%m-%d').timestamp() if stop_limit != 0: stop_limit = datetime.strptime(stop_limit, '%Y-%m-%d').timestamp() # Adjust the amount of ticks to the data size if stop_limit - start_limit > 86400 * 8: tick_zoom = 24 elif stop_limit - start_limit > 86400 * 4: tick_zoom = 12 elif stop_limit - start_limit > 86400 * 2: tick_zoom = 6 elif stop_limit - start_limit > 86400: tick_zoom = 3 else: tick_zoom = 1 if zones != 0: zone_names = zones # Generate x,y data for the mesh plot curtailments = np.zeros(shape=(len(zone_names), len(df.index))) for i, zone in enumerate(zone_names): for j, status in enumerate(df[zone]): curtailments[i, j] = status curtailments = curtailments[:, :-1] # Generate x ticks for the mesh plot meshxticks_major = [] meshxticks_minor = [] for i, d in enumerate(df.index): if d.hour == 0 and d.minute == 0: meshxticks_major.append(i) elif d.hour % tick_zoom == 0 and d.minute == 0: meshxticks_minor.append(i) fig = plt.figure() # Bottom plot ax1 = fig.add_axes([0.05, 0.18, 0.9, 0.40]) ax1.plot(df.index, df["ANM Generation"], "k-", linewidth=1, alpha=0.8) ax1.plot(df.index, df["Non-ANM Generation"], "b--", linewidth=1, alpha=0.8) #ax1.set_xlabel("Time") ax1.margins(x=0) #ax1.set_ylabel("MegaWatt") ax1.grid(b=True, which="both", axis="y") ax1.xaxis.set_major_locator(mdates.DayLocator()) ax1.xaxis.set_minor_locator(mdates.HourLocator()) ax1.xaxis.set_major_formatter(mdates.DateFormatter("%b %d")) ax1.xaxis.set_minor_formatter(mdates.DateFormatter("%H:00")) for i, t in enumerate(ax1.xaxis.get_minor_ticks()): if i % 24 == 0: t.label.set_visible(False) if i % tick_zoom != 0: t.set_visible(False) ax1.tick_params(axis="x", which="minor") ax1.grid(b=True, which="major", axis="x", linestyle="-.") ax1.grid(b=True, which="minor", axis="x", linestyle="--") ax1.legend(["ANM Generation", "Non-ANM Generation"], loc='upper center', bbox_to_anchor=(0.5, -0.20), ncol=2) #plt.xticks(rotation=-20) # Top plot cm = plt.get_cmap("OrRd") ax2 = fig.add_axes([0.05, 0.60, 0.90, 0.40]) ax2.pcolormesh(curtailments, alpha=1, cmap=cm, snap=True) ax2.set_xticks(meshxticks_major) ax2.set_xticks(meshxticks_minor, minor=True) ax2.xaxis.set_ticklabels([]) ax2.grid(b=True, which="major", axis="x", linestyle="-.") ax2.grid(b=True, which="minor", axis="x", linestyle="--") #ax2.set_ylabel("Zones") ax2.set_yticks(np.arange(len(zone_names)) + 0.5) ax2.set_yticks(np.arange(len(zone_names)), minor=True) ax2.set_yticklabels(["C", "1", "1A", "2", "2A", "2B", "3", "4", "4A"], rotation=0, va="center", fontsize="8") ax2.grid(b=True, which="minor", axis="y") custom_lines = [ Line2D([0], [0], color=cm(0), lw=4), Line2D([0], [0], color=cm(.5), lw=4), Line2D([0], [0], color=cm(1.), lw=4) ] #ax2.legend(custom_lines, ["No curtailment in zone","Partial curtailment in zone", "Full stop in zone"], loc=1, fancybox=True, framealpha=0.5) fig.autofmt_xdate(which="both") fig.set_size_inches(4.9, 3) if save_to_pdf: fig.savefig("./plots/" + file_name + ".pdf") else: plt.show() plt.clf()
def make_plot(fnum, x, ydata, ylab, ylab2, xlim=None, left_side_formatter=c2f_formatter, finer_ygrid=False): major_locator = mdates.HourLocator(interval=12) # every year minor_locator = mdates.HourLocator(interval=2) # every half hour x_major_fmt = mdates.DateFormatter('%H:%M') # Use a custom formatter function to mark noon special @ticker.FuncFormatter def x_formatter(x, pos): # Get the "stock" xtick label: xlab_default = x_major_fmt(x, pos) if xlab_default != "00:00": # No label return " " # Must be midnight! return mdates.DateFormatter('%A')(x) # Figure out which day return "noon" f, ax1 = plt.subplots(num=fnum, clear=True) f.set_size_inches(10, 6) # x comes in as np.datetime64, but older matplotlib versions # can't handle this. For those convert to python datetime: if StrictVersion(matplotlib.__version__) < StrictVersion('2.2.2'): print("Legacy python datetime for the x axis") x = x.astype('O') for y, lab in ydata: ax1.plot(x, y, label=lab) ax1.set_ylabel(ylab) if len(ydata) > 1: l = ax1.legend() ax1.spines['top'].set_visible(False) ax1.spines['right'].set_visible(False) # format the ticks ax1.xaxis.set_major_locator(major_locator) ax1.xaxis.set_major_formatter(x_formatter) ax1.xaxis.set_minor_locator(minor_locator) #f.autofmt_xdate() ax2 = ax1.twinx() ax2.set_ylabel(ylab2) plt.setp(ax1.xaxis.get_majorticklabels(), rotation=30, horizontalalignment='right') # Adjust x range if xlim is None: # round x range to nearest hours datemin = np.datetime64(d['datetime'][0], 'h') datemax = np.datetime64(d['datetime'][-1], 'h') + np.timedelta64( 1, 'h') else: datemin = xlim[0] datemax = xlim[1] if StrictVersion(matplotlib.__version__) < StrictVersion('2.2.2'): datemin = datemin.astype('O') datemax = datemax.astype('O') ax1.set_xlim(datemin, datemax) # Round the y axis similarly y_min, y_max = ax1.get_ylim() yt = ax1.get_yticks() ax1.set_ylim(yt[0], yt[-1]) # ax2 is empty, so the default y range is 0.0 to 1.0. # Set it to match such that the ticks line up: ax2.set_ylim(yt[0], yt[-1]) # Overwrite the tick decorator to convert C to F dynamically: if left_side_formatter is not None: ax1.yaxis.set_major_formatter(left_side_formatter) ax1.grid(b=True, which='major', color=(0.75, 0.75, 0.75), linestyle='-') ax1.grid(b=True, which='minor', color=(0.8, 0.8, 0.8), linestyle=':') if finer_ygrid: ax1.yaxis.set_minor_locator(ticker.AutoMinorLocator(4)) ax2.yaxis.set_minor_locator(ticker.AutoMinorLocator(4)) ax1.tick_params( which='minor', length=1.5, ) ax2.tick_params( which='minor', length=1.5, ) #f.tight_layout() return f, ax1
def plotUTMOffset(dRtk: dict, dfPos: pd.DataFrame, dfCrd: pd.DataFrame, dCrdLim: dict, logger: logging.Logger, showplot: bool = False): """ plotUTMOffset plots the offset NEU wrt to reference point """ cFuncName = colored(os.path.basename(__file__), 'yellow') + ' - ' + colored(sys._getframe().f_code.co_name, 'green') # select colors for E, N, U coordinate difference colors = [] colors.append([51 / 256., 204 / 256., 51 / 256.]) colors.append([51 / 256., 51 / 256., 255 / 256.]) colors.append([255 / 256., 51 / 256., 51 / 256.]) # what to plot crds2Plot = ['UTM.E', 'UTM.N', 'ellH', 'ns'] stdDev2Plot = ['sde', 'sdn', 'sdu'] annotateList = ['east', 'north', 'ellh', '#SV'] # find gaps in the data by comparing to mean value of difference in time dfPos['tDiff'] = dfPos['DT'].diff(1) dtMean = dfPos['tDiff'].mean() # look for it using location indexing dfPos.loc[dfPos['tDiff'] > dtMean, 'ns'] = np.nan amc.logDataframeInfo(df=dfPos, dfName='dfPos', callerName=cFuncName, logger=logger) # set up the plot plt.style.use('ggplot') # subplots fig, ax = plt.subplots(nrows=len(crds2Plot), ncols=1, sharex=True, figsize=(20.0, 16.0)) fig.suptitle('{syst:s} - {posf:s} - {date:s}'.format(posf=dRtk['info']['rtkPosFile'], syst=dRtk['syst'], date=dRtk['Time']['date'])) # make title for plot ax[0].annotate('{syst:s} - {date:s}'.format(syst=dRtk['syst'], date=dfPos['DT'].iloc[0].strftime('%d %b %Y')), xy=(0, 1), xycoords='axes fraction', xytext=(0, 0), textcoords='offset pixels', horizontalalignment='left', verticalalignment='bottom', weight='strong', fontsize='large') # copyright this ax[-1].annotate(r'$\copyright$ Alain Muls ([email protected])', xy=(1, 1), xycoords='axes fraction', xytext=(0, 0), textcoords='offset pixels', horizontalalignment='right', verticalalignment='bottom', weight='strong', fontsize='large') # subplots for coordinates display delta NEU for i, crd in enumerate(crds2Plot[:3]): axis = ax[i] # color for markers and alpha colors for error bars rgb = mpcolors.colorConverter.to_rgb(colors[i]) rgb_new = amutils.make_rgb_transparent(rgb, (1, 1, 1), 0.3) # plot coordinate differences and error bars axis.errorbar(x=dfPos['DT'], y=dfCrd[crd], yerr=dfPos[stdDev2Plot[i]], linestyle='None', fmt='o', ecolor=rgb_new, capthick=1, markersize=1, color=colors[i]) # set dimensions of y-axis axis.set_ylim([dCrdLim['min'], dCrdLim['max']]) axis.set_ylabel('{crd:s} [m]'.format(crd=crd, fontsize='large'), color=colors[i]) # # annotate each subplot with its reference position # if [dRtk['marker']['UTM.E'], dRtk['marker']['UTM.N'], dRtk['marker']['ellH']] == [np.NaN, np.NaN, np.NaN]: # # use the mean UTM/ellH position for the reference point # crdRef = dRtk['WAvg'][crd] # crdSD = dRtk['WAvg'][stdDev2Plot[i]] # annotatetxt = r'Mean: {refcrd:.3f}m ($\pm${stddev:.2f}m)'.format(refcrd=crdRef, stddev=crdSD) # else: # # we have a reference point # crdRef = dRtk['marker'][crd] # crdOffset = dRtk['marker'][crd] - dRtk['WAvg'][crd] # crdSD = dRtk['WAvg'][stdDev2Plot[i]] # annotatetxt = r'Ref: {crd:s} = {refcrd:.3f}m ({offset:.3f}m $\pm${stddev:.2f}m)'.format(crd=crd, refcrd=crdRef, stddev=crdSD, offset=crdOffset) annotatetxt = markerAnnotation(crd, stdDev2Plot[i]) # put annotation text axis.annotate(annotatetxt, xy=(1, 1), xycoords='axes fraction', xytext=(0, 0), textcoords='offset pixels', horizontalalignment='right', verticalalignment='bottom', weight='strong', fontsize='large') # title of sub-plot axis.set_title('{crd:s} offset'.format(crd=str.capitalize(annotateList[i]), fontsize='large')) # last subplot: number of satellites & PDOP for _, crd in enumerate(crds2Plot[3:4]): # plot #SVs on left axis axis = ax[-1] axis.set_ylim([0, 24]) axis.set_ylabel('#SVs [-]', fontsize='large', color='grey') # axis.set_xlabel('Time [sec]', fontsize='large') axis.fill_between(dfPos['DT'], 0, dfPos['ns'], alpha=0.5, linestyle='-', linewidth=3, color='grey', label='#SVs', interpolate=False) # plot PDOP on second y-axis axRight = axis.twinx() axRight.set_ylim([0, 15]) axRight.set_ylabel('PDOP [-]', fontsize='large', color='darkorchid') # plot PDOP value axRight.plot(dfPos['DT'], dfPos['PDOP'], linestyle='-', marker='.', markersize=1, color='darkorchid', label='PDOP') # set title axis.set_title('Visible satellites & PDOP', fontsize='large') # create the ticks for the time axis dtFormat = plot_utils.determine_datetime_ticks(startDT=dfPos['DT'].iloc[0], endDT=dfPos['DT'].iloc[-1]) if dtFormat['minutes']: axis.xaxis.set_major_locator(dates.MinuteLocator(byminute=range[1, 60, 5], interval=1)) else: axis.xaxis.set_major_locator(dates.HourLocator(interval=dtFormat['hourInterval'])) # every 4 hours axis.xaxis.set_major_formatter(dates.DateFormatter('%H:%M')) # hours and minutes axis.xaxis.set_minor_locator(dates.DayLocator(interval=1)) # every day axis.xaxis.set_minor_formatter(dates.DateFormatter('\n%d-%m-%Y')) axis.xaxis.set_tick_params(rotation=0) for tick in axis.xaxis.get_major_ticks(): # tick.tick1line.set_markersize(0) # tick.tick2line.set_markersize(0) tick.label1.set_horizontalalignment('center') # save the plot in subdir png of GNSSSystem amutils.mkdir_p(os.path.join(dRtk['info']['dir'], 'png')) pngName = os.path.join(dRtk['info']['dir'], 'png', os.path.splitext(dRtk['info']['rtkPosFile'])[0] + '-ENU.png') fig.savefig(pngName, dpi=fig.dpi) logger.info('{func:s}: created plot {plot:s}'.format(func=cFuncName, plot=colored(pngName, 'green'))) if showplot: plt.show(block=True) else: plt.close(fig) return
rem_sal = np.loadtxt(str(path)+str(fname)) else: if mouse in fname and stage3 in fname and condition2 in fname: rem_ivm = np.loadtxt(str(path)+str(fname)) # else: # if mouse in fname and stage3 in fname and condition3 in fname: # rem_day2 = np.loadtxt(str(path)+str(fname)) #time1 = md.datestr2num(timestamp1) #t1 = md.num2date(time1) #time2 = md.datestr2num(timestamp2) #t2 = md.num2date(time2) #time3 = md.datestr2num(timestamp3) #t3 = md.num2date(time3) hours = md.HourLocator(interval = 2) hoursFmt = md.DateFormatter('%H') fig = plt.figure(facecolor = 'w') ax = fig.add_subplot(111) ax1 = fig.add_subplot(311) wakesal_fig, = plt.plot(new_time1, wake_sal) wakeivm_fig, = plt.plot(new_time2, wake_ivm) #wakeday2_fig, = plt.plot(new_time3, wake_day2) ax1.xaxis.set_major_locator(hours) ax1.xaxis.set_major_formatter(hoursFmt) ax1.spines['top'].set_color('none') ax1.spines['right'].set_color('none') ax1.xaxis.set_ticks_position('bottom') ax1.yaxis.set_ticks_position('left')
def yeda6(fileloc1, fileloc2, filetype1, filetype2, ticker, df1, df2): init_tm = time() # Make dir fsplit = fileloc1.split('_') date = fsplit[len(fsplit) - 1][:-4] os.chdir(str(pathlib.Path.home()) + "/workspace/") dirpath = "/space/common/workspace/phase1_output/programother_out/matplotlib/" + filetype1 + "/" + filetype2 + "/" + date + "/" pathlib.Path(dirpath).mkdir(parents=True, exist_ok=True) ################# # Plotting time # ################# if(df1.empty or df2.empty): print("At date " + date + ", ticker" + ticker + " had no trade data\n") return k=20 gamma=1 df = k_gamma(df1, k, gamma ,"Trade Price") #f=interpolate.interp1d(df2.Time, df2["Trade Price"],kind="slinear") # ‘slinear’, ‘quadratic’ and ‘cubic’ refer to a spline interpolation of first, second or third order) #timenew=df1["Time"] #timenew=timenew[timenew<=max(df2["Time"]) & timenew>=min(df2["Time"])] #df2new=f(timenew) # pl.plot(df1["Time"],df2new,label=str(kind)) df1 = df[df["Outliers"] == 1] #df2.dropna(axis=0, how="any") #df1.dropna(axis=0, how="any") #df2["Time"][0]=df1["Time"][0] #df2["Time"][len(df2["Time"])-1]=df1["Time"][len(df1["Time"])-1] df2["Time"][0]=df["Time"][0] df2["Time"][len(df2["Time"])-1]=df["Time"][len(df["Time"])-1] f=interpolate.interp1d(df2["Time"], df2["Trade Price"],kind="zero") df2new=f(df1["Time"]) dfnew=f(df["Time"]) df1.Time = pd.to_datetime(df1.Time, unit='s') df2.Time = pd.to_datetime(df2.Time, unit='s') #df1["Time"] = pd.to_datetime(df1["Time"]) #df2["Time"] = pd.to_datetime(df2["Time"]) #df2["Time"][0]=df1["Time"][0] #df2["Time"][-1]=df1["Time"][-1] title_date = (df2["Time"][len(df2["Time"]) - 1]).strftime('%b %-d, %Y') #df2new=spline(df2["Time"], df2["Trade Price"],df1["Time"]) df1 = df1.set_index("Time") df1["Trade Price"] = df1["Trade Price"] - df2new #df = df.set_index("Time") #df["Trade Price"] = df["Trade Price"] - dfnew dfgroupby = df1[["Trade Price", "Contributor Id" ]].groupby("Contributor Id") #dfgroupby = df[["Trade Price", "Contributor Id" ]].groupby("Contributor Id") fig,ax = plt.subplots(figsize = (20,8)) dfgroupby["Trade Price"].plot(ax = ax, legend = True) #fig, ax = plt.subplots(figsize=(20,8)) #ax.plot(df1["Time"], df1["Trade Price"] - df2new, color = "red", linewidth = 1.0, linestyle = '-') #ax.plot(df["Time"], df["Trade Price"] - dfnew, color = "blue", linewidth = 1.0, linestyle = '-') ax.set_xlabel('Time (UTC)') ax.set_ylabel('Price') ax.set_title(ticker + " | " + "Spread | " + title_date) ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') #setting major locator alldays = mdates.HourLocator(interval = 1) # 3H interval ax.xaxis.set_major_locator(alldays) ax.xaxis.set_major_formatter(mdates.DateFormatter('%I %p')) #setting minor locator hoursLoc = mdates.HourLocator(interval=30) ax.xaxis.set_minor_locator(hoursLoc) ax.xaxis.set_minor_formatter(mdates.DateFormatter('%M')) ax.legend() imgname = ticker + "_" + "spread_" + filetype1 + "_" + filetype2 + "_" + date + ".png" fig.savefig(dirpath + imgname) print("Ticker", ticker, "at date", date, "finished in", time() - init_tm, "seconds")
ax1.eventplot(plot_data[i], colors=[cm.prism(color_distancer)], lineoffsets=i + 1, linelengths=1) color_distancer += 15 # shade night intervals for interval in nights: t0, t1 = interval ax1.axvspan(t0, t1, alpha=0.2, facecolor='gray') ax1 = plt.gca() # get the current axes # format of date displayed on the x axis xfmt = md.DateFormatter('%H:%M\n%m-%d-%y') ax1.xaxis.set_major_formatter(xfmt) # what hour ticks will be vivible (byhour) major_hour = md.HourLocator(byhour=x_tick_hours, interval=1) ax1.xaxis.set_major_locator(major_hour) # add second subplot to plot average intake(shares the same Xaxis timeline) ax2 = plt.subplot2grid((2, 1), (1, 0), sharex=ax1) plt.ylabel('Average pellet retrieval') # plot averages and standard error ax2.plot(avg_times, avg, color='k', linewidth=2.0) if std_err != -1: ax2.fill_between(avg_times, positive_std_err_plot, negative_std_err_plot, alpha=0.2, facecolor='gray', edgecolor="gray", linewidth=0.0,
lns3 = ax2.plot(hys7_y_sr, color='k', linewidth=1) ax1.set_xlim( [datetime.datetime(1970, 2, 5, 7), datetime.datetime(1970, 2, 5, 19)]) ax1.set_ylim([10, 30]) ax2.set_ylim([0, 3.75]) ax1.set_ylabel('Temp. [$^\circ$C]') ax1.grid() ax1.title.set_text(dic_simCase[simCase]['title']) MjLocator = range(7, 20) MjFormatter = '%H' ax1.xaxis.set_major_locator(md.HourLocator(byhour=MjLocator)) ax1.xaxis.set_major_formatter(md.DateFormatter(MjFormatter)) if simCase == 'woBooSwiSmo' or simCase == 'deterministic': P.setp(ax1.get_xticklabels(which='both'), visible=False) elif simCase == 'wBooSwiSmo': ax1.set_xlabel('Time [hr]') MiLocator = 13 MiFormatter = '\n%b %d' ax1.xaxis.set_minor_locator(md.HourLocator(byhour=MiLocator)) ax1.xaxis.set_minor_formatter(md.DateFormatter(MiFormatter)) for text in ax1.get_xminorticklabels(): text.set_fontsize(fontsize) lns = lns1 + lns2 + lns3
shr = np.abs(du / dz) + np.abs(dv / dz) return shr, z_ave #UU = np.array(EE) # total shear #VV = np.array(NN) UU = np.array(Elow) # low freq. shear VV = np.array(Nlow) Z = np.array(Ebin.columns) shr = shear(Z, UU, VV) #shr = shear(Z, Elow, Nlow) #### ----------- plot ------------ #### days = dates.DayLocator() hours6 = dates.HourLocator(interval=6) dfmt = dates.DateFormatter('%b %d') hours1 = dates.HourLocator(interval=1) rect_x = [time_zoom1, time_zoom2, time_zoom2, time_zoom1, time_zoom1] rect_y = [83, 83, 1, 1, 83] storm = pd.Timestamp('2010-03-01 12:00:00') fig = plt.figure(2) # Wind ax0 = plt.subplot2grid((9, 9), (0, 0), rowspan=1, colspan=8) df = pd.DataFrame(wind_mag) df = df.set_index('date_time') #ax0.plot(df.index, df.wind_mag)
def _genera_grafica(self, objenvios, start, stop): color_sequence = [ '#1f77b4', '#aec7e8', '#ff7f0e', '#ffbb78', '#2ca02c', '#98df8a', '#d62728', '#ff9896', '#9467bd', '#c5b0d5', '#8c564b', '#c49c94', '#e377c2', '#f7b6d2', '#7f7f7f', '#c7c7c7', '#bcbd22', '#dbdb8d', '#17becf', '#9edae5' ] fig, ax = plt.subplots(1, 1, figsize=(21, 7)) fig.subplots_adjust(left=.06, right=.90, bottom=.10, top=.90) fig.autofmt_xdate() # Formateo eje x xfmt = mdates.DateFormatter('%H:%m') ax.xaxis.set_major_locator(mdates.HourLocator()) ax.xaxis.set_minor_locator(mdates.MinuteLocator()) ax.xaxis.set_major_formatter(xfmt) # Eje y ax.yaxis.set_major_formatter(plt.FuncFormatter('{:.2f}C'.format)) # Removemos lineas grafica ax.spines['top'].set_visible(False) ax.spines['bottom'].set_visible(True) ax.spines['right'].set_visible(False) ax.spines['left'].set_visible(False) ax.get_xaxis().tick_bottom() ax.get_yaxis().tick_left() plt.grid(True, 'major', 'y', ls='--', lw=.5, c='k', alpha=.3) plt.grid(True, 'major', 'x', ls='--', lw=.3, c='k', alpha=.3) plt.tick_params(axis='both', which='both', bottom='off', top='off', labelbottom='on', left='off', right='off', labelleft='on') plt.autoscale(enable=True, axis='x', tight=None) plt.xlabel('Tiempo') plt.ylabel('Temperatura') fig.suptitle('Monitorizacion temperatura empresa: %s' % objenvios.company_id.name, fontsize=14, ha='center') cr = self.env.cr fechafin = fields.Datetime.to_string( fields.Datetime.from_string(stop) + datetime.timedelta(minutes=30)) fechaini = fields.Datetime.to_string( fields.Datetime.from_string(start) + datetime.timedelta(minutes=-30)) objdatos = self.env['tracksonda.loaddatson'] sql_family = """ select family as family from tracksonda where hub_id=%s group by family """ % objenvios.id cr.execute(sql_family) listfamilias = cr.dictfetchall() for familia in listfamilias: atrace = [] annotations = [] rowsensors = self.env['tracksonda'].search([ ('hub_id', '=', objenvios.id), ('family', '=', familia["family"]) ]) i = 0 for sonda in rowsensors: xy = [] # datos=objdatos.search([('tracksonda_id','=',sonda.id),('fecha','=',fecha.strftime("%Y-%m-%d"))]) datos = objdatos.search([('tracksonda_id', '=', sonda.id)]) sql = """SELECT tracksonda.name, to_char(tiempo,'yyyy-mm-dd hh24:mi') as hora, (tracksonda_loaddatson.temperature) as temp, res_company.name company FROM tracksonda_loaddatson INNER JOIN tracksonda ON tracksonda_loaddatson.tracksonda_id = tracksonda.id INNER JOIN res_company ON tracksonda.company_id = res_company.id WHERE to_char(tiempo,'yyyy-mm-dd hh24:mi') >=%s and to_char(tiempo,'yyyy-mm-dd hh24:mi') <=%s and tracksonda.id=%s order by 2""" cr.execute(sql, (fechaini[:16], fechafin[:16], sonda.id)) regist = cr.dictfetchall() valores = [] horas_x = [] temperatura_y = [] i = 0 pvarx = [] pvary = [] for row in regist: # horas_x.append(int(row['hora'][11:13])) horas_x.append(self._horaZona(row['hora'], "C")) temperatura_y.append(round(row['temp'], 2)) valores.append([ self._horaZona(row['hora'], "C"), round(row['temp'], 2) ]) # valores.append([(row['hora']),round(row['temp'],2)]) # valores.append([int(row['hora'][11:13]),round(row['temp'],2)]) if len(valores) != 0: max_idx = np.argmax(temperatura_y) max_valy = temperatura_y[max_idx] max_valx = horas_x[max_idx] min_idx = np.argmin(temperatura_y) min_valy = temperatura_y[min_idx] min_valx = horas_x[min_idx] media_y = round(np.average(temperatura_y), 2) amedia_y = np.empty(len(horas_x)) amedia_y.fill(media_y) dhoras_x = [dateutil.parser.parse(s) for s in horas_x] print "Generando sonda %s" % sonda.name plt.plot(dhoras_x, temperatura_y, lw=1, color=color_sequence[i], label=sonda.name) i = +1 plt.plot(dhoras_x, amedia_y, lw=1, color=color_sequence[i], label="Media %s " % sonda.name) i = +1 normalizaname = objenvios.name.replace(" ", "_") image_save = get_module_path("appcc") + ("/static/img/%s.png" % normalizaname) image_path = get_module_resource('appcc', 'static/img', '%s.png' % normalizaname) leg = ax.legend(loc='upper right', bbox_to_anchor=(1, 1), shadow=True, ncol=1) leg.set_title("Sondas", prop={'size': 10}) plt.savefig(image_save, bbox_inches='tight') if image_path: archivo = open(image_path, 'rb').read().encode('base64') else: archivo = None return archivo
import matplotlib.dates as md import pandas as pd from matplotlib import rcParams import matplotlib.colors as colors from ace_parse import * # Plotting preferences rcParams['xtick.direction'] = 'in' rcParams['ytick.direction'] = 'in' rcParams.update({'font.size': 14}) rcParams['axes.titlepad'] = 14 rcParams['xtick.major.pad'] = '10' rcParams['ytick.major.pad'] = '10' myFmt = md.DateFormatter('%H') rule = md.HourLocator(interval=1) # Data locations #d_loc='/Users/heather/ICECAPS-ACE/' #calfile = '/Users/heather/Desktop/Summit_May_2019/Instruments/CLASP/CLASP-cal-Feb2019/calibration-unit-F-Feb2019.mat' d_loc = '/home/fluxtower/' calfile = '/home/fluxtower/CLASP-ICECAP-ACE/CLASP-cal-Feb2019/calibration-unit-F-Feb2019.mat' # Plot times #d1=dt.datetime(2019,9,11,0,0) #d2=dt.datetime(2019,9,12,0,0) d2 = dt.datetime.utcnow() d1 = d2 - dt.timedelta(hours=24)
if __name__ == "__main__": cbpdb = CoinbaseProCandleDatabase(db_file) df = cbpdb.get_dataframe("ETH-USD", 60, max_count=None) strategy = MACDStrategy() for i in range(1, len(df)+1): df_step = df.head(i) strategy.execute(df_step) import matplotlib import matplotlib.pyplot as plt import matplotlib.dates as mdates hours = mdates.HourLocator() macd, macd_signal, macd_hist = MACD(df.close, fastperiod=12, slowperiod=26, signalperiod=9) df.timestamp = [dateutil.parser.parse(ts_str) for ts_str in df.timestamp] time_line = list(df.timestamp) plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S')) plt.gca().xaxis.set_major_locator(mdates.MinuteLocator(interval=15)) fig, axs = plt.subplots(nrows=2, sharex=True)
def draw_klines(df: pd.DataFrame, main_chart_lines=None, sub_chart_lines=None, *, annotate=False, to_file=None): """ 基础画图方法 :param df: dataframe->基础需包含['datetime', 'open', 'close', 'high', 'low', 'volume'], 可选trades: List[Dict],若有则会添加交易标识Dict中包含字段['price', 'size', 'direction'] :param extra_lines: 添加想要的线 :param to_file: 生成图片,默认None为不生成 :return: """ import matplotlib.pyplot as plt import mpl_finance as mpf from matplotlib import ticker import matplotlib.dates as mdates columns = ['datetime', 'open', 'close', 'high', 'low', 'volume'] if not set(df.columns).issuperset(columns): raise Exception(f'请包含{columns}字段') data = df.loc[:, columns] data_mat = data.values.T xdate = data['datetime'].tolist() def mydate(x, pos): try: return xdate[int(x)] except IndexError: return '' data_len = len(data_mat[2]) fig, [ax1, ax2, ax3] = plt.subplots(3, 1, sharex=True) fig.set_figheight(300 / 72) fig.set_figwidth(1200 / 72) ax1.set_position([0.1, 0.4, 0.8, 0.55]) ax2.set_position([0.1, 0.2, 0.8, 0.15]) ax3.set_position([0.1, 0.05, 0.8, 0.1]) ax1.set_title('KLine', fontsize='large', fontweight='bold') ax2.set_title('MACD') ax3.set_title('Volume') mpf.candlestick2_ochl(ax1, data_mat[1], data_mat[2], data_mat[3], data_mat[4], colordown=DEFALUT_CHART_COLOR['barDown'], colorup=DEFALUT_CHART_COLOR['barUp'], width=min(120 / data_len, 1), alpha=1) mpf.volume_overlay(ax3, data_mat[1], data_mat[2], data_mat[5], colordown=DEFALUT_CHART_COLOR['barDown'], colorup=DEFALUT_CHART_COLOR['barUp'], width=min(120 / data_len, 1), alpha=1) ax1.grid(True) ax2.grid(True) ax3.grid(True) ax1.xaxis.set_major_formatter(ticker.FuncFormatter(mydate)) ax1.xaxis.set_major_locator(mdates.HourLocator()) ax1.xaxis.set_major_locator( mdates.MinuteLocator(byminute=[0, 15, 30, 45], interval=1)) ax1.xaxis.set_major_locator(ticker.MaxNLocator(8)) if main_chart_lines: for l in main_chart_lines: ax1.add_line(l) else: import talib l = range(data_len) for p in [5, 10, 30, 60]: ma_line = mpf.Line2D(l, talib.MA(data_mat[2].astype(float), p), color=DEFALUT_CHART_COLOR[f'ma{p}'], linewidth=120 / data_len) ax1.add_line(ma_line) if sub_chart_lines: for l in main_chart_lines: ax2.add_line(l) else: import talib dif, dea, macd = talib.MACDEXT(data_mat[2].astype(float), fastperiod=12, fastmatype=1, slowperiod=26, slowmatype=1, signalperiod=9, signalmatype=1) macd = macd * 2 l = range(data_len) dif_line = mpf.Line2D(l, dif, color=DEFALUT_CHART_COLOR['dif'], linewidth=120 / data_len) dea_line = mpf.Line2D(l, dea, color=DEFALUT_CHART_COLOR['dea'], linewidth=120 / data_len) ax2.add_line(dif_line) ax2.add_line(dea_line) mpf.candlestick2_ochl(ax2, [0] * len(macd), macd, np.where(macd >= 0, macd, 0), np.where(macd < 0, macd, 0), colordown=DEFALUT_CHART_COLOR['barDown'], colorup=DEFALUT_CHART_COLOR['barUp'], width=120 / data_len, alpha=0.7) if 'trades' in df.columns: trades_long_x = [] trades_long_y = [] trades_long_s = [] trades_short_x = [] trades_short_y = [] trades_short_s = [] size_ratio = 100 * 120 / data_len for i, (_, tl) in enumerate(df.trades.iteritems()): if isinstance(tl, Iterable): long_n = 0 short_n = 0 total_long = 0 total_short = 0 for t in tl: if t['direction'] == 'long': trades_long_x.append(i) trades_long_y.append(t['price']) trades_long_s.append(t['size'] * size_ratio) long_n += t['size'] total_long += t['price'] elif t['direction'] == 'short': trades_short_x.append(i) trades_short_y.append(t['price']) trades_short_s.append(t['size'] * size_ratio) short_n += t['size'] total_short += t['price'] else: if annotate and long_n: avg_long_price = total_long / long_n ax1.annotate(f'+{long_n}@{avg_long_price:.1f}', xy=(i, avg_long_price), xytext=(i + 0.1, avg_long_price - 0.1)) if annotate and short_n: avg_short_price = total_short / short_n ax1.annotate(f'-{short_n}@{avg_short_price:.1f}', xy=(i, avg_short_price), xytext=(i + 0.1, avg_short_price + 0.1)) else: ax1.scatter(trades_long_x, trades_long_y, s=trades_long_s, color=DEFALUT_CHART_COLOR['longMark'], marker='^', alpha=0.8, linewidths=0, zorder=2) ax1.scatter(trades_short_x, trades_short_y, s=trades_short_s, color='', edgecolors=DEFALUT_CHART_COLOR['shortMark'], marker='v', alpha=0.8, linewidths=1, zorder=2) if to_file: try: fig.savefig(to_file, dpi=DEFALUT_CHART_PARAMS['dpi'], figsize=(120, 30)) except Exception as e: print('errSaveFig:', e) return fig
def TemperatureHumidityGraph(source, days, delay): print("TemperatureHumidityGraph source:%s days:%s" % (source, days)) print("sleeping seconds:", delay) time.sleep(delay) print("TemperatureHumidityGraph running now") # blink GPIO LED when it's run GPIO.setup(18, GPIO.OUT) GPIO.output(18, True) time.sleep(0.2) GPIO.output(18, False) # now we have get the data, stuff it in the graph try: print("trying database") db = mdb.connect('localhost', 'root', DATABASEPASSWORD, 'WeatherPi') cursor = db.cursor() query = "SELECT TimeStamp, bmp180Temperature, outsideTemperature, outsideHumidity, insideHumidity FROM WeatherData where now() - interval %i hour < TimeStamp" % ( days * 24) print "query=", query cursor.execute(query) result = cursor.fetchall() t = [] u = [] v = [] x = [] z = [] fig = pyplot.figure() for record in result: t.append(record[0]) u.append(record[1]) v.append(record[2]) x.append(record[3]) z.append(record[4]) print("count of t=", len(t)) if (len(t) == 0): return #dts = map(datetime.datetime.fromtimestamp, s) #fds = dates.date2num(dts) # converted # matplotlib date format object hfmt = dates.DateFormatter('%m/%d-%H') ax = fig.add_subplot(111) ax.xaxis.set_major_locator(dates.HourLocator(interval=6)) ax.xaxis.set_major_formatter(hfmt) pylab.xticks(rotation='vertical') pyplot.subplots_adjust(bottom=.3) pylab.plot(t, v, color='g', label="Outside Temp (C)", linestyle="-", marker=".") pylab.plot(t, u, color='r', label="Inside Temp (C)", linestyle="-", marker=".") pylab.xlabel("Hours") pylab.ylabel("degrees C") pylab.legend(loc='upper left') pylab.axis([min(t), max(t), 0, 40]) ax2 = pylab.twinx() pylab.ylabel("% ") pylab.plot(t, x, color='y', label="Outside Hum %", linestyle="-", marker=".") pylab.plot(t, z, color='b', label="Inside Hum %", linestyle="-", marker=".") pylab.axis([min(t), max(t), 0, 100]) pylab.legend(loc='lower left') pylab.figtext(.5, .05, ("Environmental Statistics Last %i Days" % days), fontsize=18, ha='center') #pylab.grid(True) pyplot.setp(ax.xaxis.get_majorticklabels(), rotation=70) ax.xaxis.set_major_formatter(dates.DateFormatter('%m/%d-%H')) pyplot.show() pyplot.savefig( "/home/pi/WeatherPiRasPiConnectServer/static/TemperatureHumidityGraph.png" ) except mdb.Error, e: print "Error %d: %s" % (e.args[0], e.args[1])
def set(self, stationId, dateFrom=datetime.today(), dateTo=datetime.today(), statisticType='D'): self.stationId = stationId self.statisticType = statisticType self.data = self.db.getData(stationId, dateFrom, dateTo, statisticType) # Clear statistic self.ax.clear() # Set label left self.ax.set_ylabel('temperature') # Set min max date datemin = np.datetime64(dateFrom, statisticType) datemax = np.datetime64(dateTo, statisticType) self.ax.set_xlim(datemin, datemax) # Enable grid self.ax.grid(True) # Set horizontal grid and labels years = mdates.YearLocator() # every year months = mdates.MonthLocator() # every month days = mdates.DayLocator() # every days hours = mdates.HourLocator() # every hours if self.statisticType == 'Y': # format the ticks year self.ax.xaxis.set_major_locator(years) self.ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y')) self.ax.xaxis.set_minor_locator(months) self.ax.set_xlabel('Date Years') elif self.statisticType == 'm': # format the ticks month self.ax.xaxis.set_major_locator(months) self.ax.xaxis.set_major_formatter(mdates.DateFormatter('%m')) self.ax.xaxis.set_minor_locator(months) self.ax.set_xlabel('Date Months') else: # format the ticks day self.ax.xaxis.set_major_locator(days) self.ax.xaxis.set_major_formatter(mdates.DateFormatter('%d')) self.ax.xaxis.set_minor_locator(hours) self.ax.set_xlabel('Date Days') # Print Temperatures if self.data: dateArray = [] tempArray = [] for key, tempList in self.data.temperatures.items(): if tempList: for temp in tempList: dateArray.append(temp.date) tempArray.append(temp.temperature) self.ax.plot(dateArray, tempArray) self.canvas.draw() self.ax.clear()
def PowerVoltageGraph(source,days,delay): print("PowerVoltageGraph source:%s days:%s delay:%i" % (source,days,delay)) print("sleeping :",delay) time.sleep(delay) print("PowerVoltageGraph running now") # blink GPIO LED when it's run GPIO.setup(18, GPIO.OUT) GPIO.output(18, True) time.sleep(0.2) GPIO.output(18, False) # now we have get the data, stuff it in the graph try: print("trying database") db = mdb.connect('localhost', 'root', config.MySQL_Password, 'ProjectCuracao2'); cursor = db.cursor() query = "SELECT TimeStamp, solarVoltage, batteryVoltage, loadVoltage FROM PowerSystem where now() - interval %i hour < TimeStamp" % (days*24) cursor.execute(query) result = cursor.fetchall() t = [] s = [] u = [] v = [] #x = [] for record in result: t.append(record[0]) s.append(record[1]) u.append(record[2]) v.append(record[3]) #x.append(record[4]) fig = pyplot.figure() print ("count of t=",len(t)) #print (t) if (len(t) == 0): return #dts = map(datetime.datetime.fromtimestamp, t) #print dts #fds = dates.date2num(t) # converted # matplotlib date format object hfmt = dates.DateFormatter('%m/%d-%H') fig = pyplot.figure() fig.set_facecolor('white') ax = fig.add_subplot(111,axisbg = 'white') #ax.vlines(fds, -200.0, 1000.0,colors='w') ax.xaxis.set_major_locator(dates.HourLocator(interval=6)) ax.xaxis.set_major_formatter(hfmt) ax.set_ylim(bottom = -200.0) pyplot.xticks(rotation='vertical') pyplot.subplots_adjust(bottom=.3) pylab.plot(t, s, color='b',label="Solar",linestyle="-",marker=".") pylab.plot(t, u, color='r',label="Battery",linestyle="-",marker=".") pylab.plot(t, v, color='g',label="Load",linestyle="-",marker=".") #pylab.plot(t, x, color='m',label="Power Eff",linestyle="-",marker=".") pylab.xlabel("Hours") pylab.ylabel("Voltage V") pylab.legend(loc='upper left') if (max(u) > max(s)): myMax = max(u)+ 100.0 else: myMax = max(s) pylab.axis([min(t), max(t), min(u), myMax]) pylab.figtext(.5, .05, ("ProjectCuracao2 Power Voltage Last %i Days" % days),fontsize=18,ha='center') pyplot.setp( ax.xaxis.get_majorticklabels(), rotation=70) pylab.grid(True) pyplot.show() try: pyplot.savefig("/home/pi/RasPiConnectServer/static/PowerVoltageGraph.png",facecolor=fig.get_facecolor()) except: pyplot.savefig("/home/pi/SDL_Pi_ProjectCuracao2/static/PowerVoltageGraph.png",facecolor=fig.get_facecolor()) except mdb.Error, e: print "Error %d: %s" % (e.args[0],e.args[1])
def light_curve_peak_match_subtract(light_curve_to_subtract_from_df, light_curve_to_subtract_with_df, estimated_time_of_peak, max_seconds_shift=1800, plot_path_filename=None, verbose=False, logger=None): """Align the peak of a second light curve to the first, scale its magnitude to match, and subtract it off. Inputs: light_curve_to_subtract_from_df [pd DataFrame]: A pandas DataFrame with a DatetimeIndex and a column for irradiance. light_curve_to_subtract_with_df [pd DataFrame]: A pandas DataFrame with a DatetimeIndex and a column for irradiance. estimated_time_of_peak [metatime]: The estimated time that the peak should occur. This could come from, e.g., GOES/XRS. Optional Inputs: max_seconds_shift [int]: The maximum allowed time shift in seconds to get the peaks to match. plot_path_filename [str]: Set to a path and filename in order to save the summary plot to disk. Default is None, meaning the plot will not be saved to disk. verbose [bool]: Set to log the processing messages to disk and console. Default is False. logger [JpmLogger]: A configured logger from jpm_logger.py. If set to None, will generate a new one. Default is None. Outputs: light_curve_corrected_df [pd DataFrame]: A pandas DataFrame with the same format as light_curve_to_subtract_from_df but with the resultant peak match and subtraction performed. Returns np.nan if the peaks couldn't be found. seconds_shift [float]: The number of seconds that light_curve_to_subtract_with_df was shifted to get its peak to match light_curve_to_subtract_from_df. Returns np.nan if the peaks couldn't be found. scale_factor [float]: The multiplicative factor applied to light_curve_to_subtract_with_df to get its peak to match light_curve_to_subtract_from_df. Returns np.nan if the peaks couldn't be found. Optional Outputs: None Example: light_curve_corrected_df, seconds_shift, scale_factor = light_curve_peak_match_subtract(light_curve_to_subtract_from_df, light_curve_to_subtract_with_df, estimated_time_of_peak, plot_path_filename='./', verbose=True) """ # Prepare the logger for verbose if verbose: if not logger: logger = JpmLogger(filename='light_curve_peak_match_subtract_log', path='/Users/jmason86/Desktop/') logger.info("Running on event with light curve start time of {0}.".format(light_curve_to_subtract_from_df.index[0])) # Drop NaNs since peakutils can't handle them light_curve_to_subtract_from_df = light_curve_to_subtract_from_df.dropna() light_curve_to_subtract_with_df = light_curve_to_subtract_with_df.dropna() # Detrend and find the peaks that are >= 95% of the max irradiance within if verbose: logger.info("Detrending light curves.") if (light_curve_to_subtract_from_df['irradiance'].values < 0).all(): light_curve_to_subtract_from_df.iloc[0] = 1 # Else can crash peakutils.baseline base_from = peakutils.baseline(light_curve_to_subtract_from_df) detrend_from = light_curve_to_subtract_from_df - base_from indices_from = peakutils.indexes(detrend_from.values.squeeze(), thres=0.95) if (light_curve_to_subtract_with_df['irradiance'].values < 0).all(): light_curve_to_subtract_with_df.iloc[0] = 1 # Else can crash peakutils.baseline base_with = peakutils.baseline(light_curve_to_subtract_with_df) detrend_with = light_curve_to_subtract_with_df - base_with indices_with = peakutils.indexes(detrend_with.values.squeeze(), thres=0.95) if len(indices_from) == 0: if verbose: logger.warning('Could not find peak in light curve to subtract from.') return np.nan, np.nan, np.nan if len(indices_with) == 0: if verbose: logger.warning('Could not find peak in light curve to subtract with.') return np.nan, np.nan, np.nan # Identify the peak closest to the input estimated peak time (e.g., from GOES/XRS) if verbose: logger.info("Identifying peaks closest to initial guess in light curves.") peak_index_from = indices_from[closest(light_curve_to_subtract_from_df.index[indices_from], estimated_time_of_peak)] if len(indices_with) == 0: import pdb pdb.set_trace() peak_index_with = indices_with[closest(light_curve_to_subtract_with_df.index[indices_with], estimated_time_of_peak)] index_shift = peak_index_from - peak_index_with # Compute how many seconds the time shift corresponds to seconds_shift = (light_curve_to_subtract_from_df.index[peak_index_from] - light_curve_to_subtract_with_df.index[peak_index_with]).total_seconds() # Fail if seconds_shift > max_seconds_shift isTimeShiftValid = True if abs(seconds_shift) > max_seconds_shift: if verbose: logger.warning("Cannot do peak match. Time shift of {0} seconds is greater than max allowed shift of {1} seconds.".format(seconds_shift, max_seconds_shift)) isTimeShiftValid = False # Shift the subtract_with light curve in time to align its peak to the subtract_from light curve if isTimeShiftValid: if verbose: logger.info("Shifting and scaling the light curve to subtract with.") shifted_with = light_curve_to_subtract_with_df.shift(index_shift) # Scale the subtract_with light curve peak irradiance to match the subtract_from light curve peak irradiance scale_factor = (detrend_from.values[peak_index_from] / shifted_with.values[peak_index_with + index_shift])[0] shifted_scaled_with = shifted_with * scale_factor light_curve_corrected_df = light_curve_to_subtract_from_df - shifted_scaled_with if verbose: if light_curve_corrected_df.isnull().values.sum() > 1: logger.warning("%s points were shifted to become NaN." % light_curve_corrected_df.isnull().values.sum()) logger.info("Light curve peak matching and subtraction complete.") if plot_path_filename: from jpm_number_printing import latex_float seconds_shift_string = '+' if seconds_shift >= 0 else '' seconds_shift_string += str(int(seconds_shift)) if isTimeShiftValid: scale_factor_string = latex_float(scale_factor) plt.style.use('jpm-transparent-light') from matplotlib import dates plt.clf() fig, ax = plt.subplots() plt.plot(light_curve_to_subtract_from_df.index.values, light_curve_to_subtract_from_df.values, c='limegreen') plt.tick_params(axis='x', which='minor', labelbottom='off') plt.xlabel(estimated_time_of_peak) plt.ylabel('Irradiance [%]') fmtr = dates.DateFormatter("%H:%M:%S") ax.xaxis.set_major_formatter(fmtr) ax.xaxis.set_major_locator(dates.HourLocator()) if isTimeShiftValid: plt.title('I: $\\times$' + scale_factor_string + ', t: ' + seconds_shift_string + ' s', color='tomato') shifted_scaled_with.plot(c='tomato', label='subtract with', ax=ax) light_curve_corrected_df.plot(c='darkgrey', label='result', ax=ax) else: plt.title('t: ' + seconds_shift_string + ' s > max allowed {0} s'.format(max_seconds_shift), color='tomato') plt.plot(light_curve_to_subtract_with_df.index.values, light_curve_to_subtract_with_df.values, c='tomato') plt.scatter(light_curve_to_subtract_from_df.index[peak_index_from], light_curve_to_subtract_from_df.values[peak_index_from], c='black') if isTimeShiftValid: plt.scatter(shifted_scaled_with.index[peak_index_with + index_shift], shifted_scaled_with.values[peak_index_with + index_shift], c='black') ax.legend(['subtract from', 'subtract with', 'result'], loc='best') else: plt.scatter(light_curve_to_subtract_with_df.index[peak_index_with], light_curve_to_subtract_with_df.values[peak_index_with], c='black') ax.legend(['subtract from', 'subtract with'], loc='best') path = os.path.dirname(plot_path_filename) if not os.path.exists(path): os.makedirs(path) plt.savefig(plot_path_filename) if verbose: logger.info("Summary plot saved to %s" % plot_path_filename) if isTimeShiftValid: return light_curve_corrected_df, seconds_shift, scale_factor else: return np.nan, seconds_shift, np.nan
#colorbar(img2_1).set_label('velocity [m/s]') #colorbar(img2_2).set_label('velocity [m/s]') else: print("ERROR: Ort (flach_or_tief) nicht feststellbar") #Preferences of the plots #figure 1 #set the xticks (dateformat and tick location) hfmt = mdates.DateFormatter('%d %b') for row in range(3): for column in range(2): axarr1[row,column].xaxis.set_major_locator(mdates.DayLocator()) axarr1[row,column].xaxis.set_minor_locator(mdates.HourLocator(byhour = [0,6,12,18],interval = 1)) axarr1[row,column].xaxis.set_major_formatter(hfmt) axarr1[row,column].set_ylim(bottom = 0, top = 80) print("xlim:",row,column,axarr1[row,column].get_xlim()) axarr1[row,column].invert_yaxis() axarr2[row,column].xaxis.set_major_locator(mdates.DayLocator()) axarr2[row,column].xaxis.set_minor_locator(mdates.HourLocator(byhour = [0,6,12,18],interval = 1)) axarr2[row,column].xaxis.set_major_formatter(hfmt) axarr2[row,column].set_ylim(bottom = 0, top = 90) axarr2[row,column].invert_yaxis()