def plot_prediction_true_res(y_pred): """ Output: Plot of Model Prediction vs. True """ # y_trues = pd.DataFrame(lym.hold_set['avg_price_est']) y_preds = pd.DataFrame(y_pred, index=lym.hold_set.index, columns=['y_pred']) data = pd.concat([lym.hold_set, y_preds], axis=1) # print data.columns cities = [ 'city_denver', 'city_sf', 'city_seattle', 'city_ny', 'city_chicago' ] cartypes = ['ride_type_lyft', 'ride_type_lyft_line', 'ride_type_lyft_plus'] for city in cities: for cartype in cartypes: plt.cla() if city != 'city_chicago': sub_data = data[(data[city] == 1) & (data[cartype] == 1)] #.resample('10T') else: sub_data = data[(data[cities[0]] == 0) & (data[cities[1]] == 0) & (data[cities[2]] == 0) & (data[cities[3]] == 0) & (data[cartype] == 1)] #.resample('10T') fig, ax = plt.subplots(2, 1, figsize=(20, 10)) ax[0].plot_date(sub_data.index.to_pydatetime(), sub_data['avg_est_price'].values, 'o--', label='true data') ax[0].plot_date(sub_data.index.to_pydatetime(), 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])) data['resid'] = data['avg_est_price'] - data['y_pred'] if city != 'city_chicago': resid = data[(data[city] == 1) & (data[cartype] == 1)][ 'resid'] #.resample('10T') else: resid = data[(data[cities[0]] == 0) & (data[cities[1]] == 0) & (data[cities[2]] == 0) & (data[cities[3]] == 0) & (data[cartype] == 1)]['resid'] #.resample('10T') ax[1].plot_date(resid.index.to_pydatetime(), 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')
user1.to_csv('aaplTest.csv') # print(type(user1.HA_Open[3])) # for i in range(len(user1)): # for j in range(len(user1[i])): # print(user1[i][j], end=' ') # print() #tx_data['InvoiceDate'] = pd.to_datetime(tx_data['InvoiceDate']).dt.date date1 = "2020-12-17" date2 = "2021-01-11" #print(type(date1)) -> str mondays = WeekdayLocator(MONDAY) # major ticks on the mondays alldays = DayLocator() # minor ticks on the days weekFormatter = DateFormatter('%b %d') # e.g., Jan 12 dayFormatter = DateFormatter('%b %d') #dayFormatter = DateFormatter('%d') # e.g., 12 quotes = pd.read_csv('aaplTest.csv', index_col=1, parse_dates=True, infer_datetime_format=True) # select desired range of dates quotes = quotes[(quotes.index >= date1) & (quotes.index <= date2)] fig, ax = plt.subplots() fig.subplots_adjust(bottom=0.2)
def pandas_candlestick_ohlc(dat, stick="day", otherseries=None): """ :param dat: pandas DataFrame object with datetime64 index, and float columns "Open", "High", "Low", and "Close", likely created via DataReader from "yahoo" :param stick: A string or number indicating the period of time covered by a single candlestick. Valid string inputs include "day", "week", "month", and "year", ("day" default), and any numeric input indicates the number of trading days included in a period :param otherseries: An iterable that will be coerced into a list, containing the columns of dat that hold other series to be plotted as lines This will show a Japanese candlestick plot for stock data stored in dat, also plotting other series if passed. """ mondays = WeekdayLocator(MONDAY) # major ticks on the mondays alldays = DayLocator() # minor ticks on the days dayFormatter = DateFormatter('%d') # e.g., 12 # Create a new DataFrame which includes OHLC data for each period specified by stick input transdat = dat.loc[:, ["Open", "High", "Low", "Close"]] if (type(stick) == str): if stick == "day": plotdat = transdat stick = 1 # Used for plotting elif stick in ["week", "month", "year"]: if stick == "week": transdat["week"] = pd.to_datetime(transdat.index).map( lambda x: x.isocalendar()[1]) # Identify weeks elif stick == "month": transdat["month"] = pd.to_datetime(transdat.index).map( lambda x: x.month) # Identify months transdat["year"] = pd.to_datetime(transdat.index).map( lambda x: x.isocalendar()[0]) # Identify years grouped = transdat.groupby(list(set( ["year", stick]))) # Group by year and other appropriate variable plotdat = pd.DataFrame({ "Open": [], "High": [], "Low": [], "Close": [] }) # Create empty data frame containing what will be plotted for name, group in grouped: plotdat = plotdat.append( pd.DataFrame( { "Open": group.iloc[0, 0], "High": max(group.High), "Low": min(group.Low), "Close": group.iloc[-1, 3] }, index=[group.index[0]])) if stick == "week": stick = 5 elif stick == "month": stick = 30 elif stick == "year": stick = 365 elif (type(stick) == int and stick >= 1): transdat["stick"] = [ np.floor(i / stick) for i in range(len(transdat.index)) ] grouped = transdat.groupby("stick") plotdat = pd.DataFrame({ "Open": [], "High": [], "Low": [], "Close": [] }) # Create empty data frame containing what will be plotted for name, group in grouped: plotdat = plotdat.append( pd.DataFrame( { "Open": group.iloc[0, 0], "High": max(group.High), "Low": min(group.Low), "Close": group.iloc[-1, 3] }, index=[group.index[0]])) else: raise ValueError( 'Valid inputs to argument "stick" include the strings "day", "week", "month", "year", or a positive integer' ) # Set plot parameters, including the axis object ax used for plotting fig, ax = plt.subplots() fig.subplots_adjust(bottom=0.2) if plotdat.index[-1] - plotdat.index[0] < pd.Timedelta('730 days'): weekFormatter = DateFormatter('%b %d') # e.g., Jan 12 ax.xaxis.set_major_locator(mondays) ax.xaxis.set_minor_locator(alldays) else: weekFormatter = DateFormatter('%b %d, %Y') ax.xaxis.set_major_formatter(weekFormatter) ax.grid(True) # Create the candelstick chart mpf.candlestick_ohlc(ax, list( zip(list(date2num(plotdat.index.tolist())), plotdat["Open"].tolist(), plotdat["High"].tolist(), plotdat["Low"].tolist(), plotdat["Close"].tolist())), colorup="black", colordown="red", width=stick * .4) # Plot other series (such as moving averages) as lines if otherseries != None: if type(otherseries) != list: otherseries = [otherseries] dat.loc[:, otherseries].plot(ax=ax, lw=1.3, grid=True) ax.xaxis_date() ax.autoscale_view() plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right') plt.show()
alpha=0.2,hist_kwds={'bins':50}) # alpha - darker = more density # hist_kwds - more bins - 50 # 'trends in diagnose, behavior of changes correlated pair stocks | GM more correlated to ford' # ---- Plot candle sticks plots (short period is better) from matplotlib.finance import candlestick_ohlc from matplotlib.dates import DateFormatter, date2num, WeekdayLocator, DayLocator, MONDAY ford_reset = ford.loc['2017-01'].reset_index() # get all Jan values and reset index ford_reset.head() ford_reset['date_ax'] = ford_reset['Date'].apply(lambda date: date2num(date)) ford_reset.head() # Create list of tuples list_of_cols = ['date_ax','Open','High','Low','Close'] ford_values = [tuple(vals) for vals in ford_reset[list_of_cols].values] mondays = WeekdayLocator(MONDAY) # Major ticks on the Monday alldays = DayLocator() # minor ticks on that days weekFormatter = DateFormatter('%b %d') # e.g. Jan 12 dayFormatter = DateFormatter('%d') # e.g. 12 # Plotting fig, ax = plt.subplots() fig.subplots_adjust(bottom=0.2) # set index scales ax.xaxis.set_major_locator(mondays) ax.xaxis.set_minor_locator(alldays) ax.xaxis.set_major_formatter(weekFormatter) candlestick_ohlc(ax,ford_values,width=0.1,colorup='g',colordown='r')
def pandas_candlestick_ohlc(dat, stick="day", otherseries=None): """ :param dat: pandas DataFrame object with datetime64 index, and float columns "open", "high", "low", and "close" :param stick: A string or number indicating the period of time covered by a single candlestick. Valid string inputs include "day", "week", "month", and "year", ("day" default) :param otherseries: An iterable that will be coerced into a list, containing the columns of dat that hold other series to be plotted as lines """ mondays = WeekdayLocator(MONDAY) # major ticks on the mondays alldays = DayLocator( ) # Make ticks on occurrences of each day of the month,minor ticks dayFormatter = DateFormatter('%d') # e.g., 12 transdat = dat.loc[:, ["open", "high", "low", "close"]] if (type(stick) == str): if stick == "day": plotdat = transdat stick = 1 # Used for plotting, defaul to 1 for day-plot elif stick in ["week", "month", "year"]: if stick == "week": transdat["week"] = pd.to_datetime(transdat.index).map( lambda x: x.isocalendar()[1]) # Identify weeks elif stick == "month": transdat["month"] = pd.to_datetime(transdat.index).map( lambda x: x.month) # Identify months transdat["year"] = pd.to_datetime(transdat.index).map( lambda x: x.isocalendar()[0]) # Identify years grouped = transdat.groupby(list(set( ["year", stick]))) # Group by year and other appropriate variable plotdat = pd.DataFrame({ "open": [], "high": [], "low": [], "close": [] }) # Create empty data frame containing what will be plotted for name, group in grouped: plotdat = plotdat.append( pd.DataFrame( { "open": group.iloc[0, 0], "high": max(group.high), "low": min(group.low), "close": group.iloc[-1, 3] }, index=[group.index[0]])) if stick == "week": stick = 5 elif stick == "month": stick = 30 elif stick == "year": stick = 365 else: raise ValueError( 'Valid inputs to argument "stick" include the strings "day", "week", "month", "year", or a positive integer' ) # Set plot parameters, including the axis object ax used for plotting fig, ax = plt.subplots() fig.subplots_adjust(bottom=0.2) if plotdat.index[-1] - plotdat.index[0] < pd.Timedelta('365 days'): weekFormatter = DateFormatter('%b %d, %Y') # e.g., Jan 12,2007 ax.xaxis.set_major_locator(mondays) ax.xaxis.set_minor_locator(alldays) ax.xaxis.set_major_formatter(weekFormatter) else: weekFormatter = DateFormatter('%b %d, %Y') ax.xaxis.set_major_locator(mondays) ax.xaxis.set_minor_locator(alldays) ax.xasix.set_major_formatter(weekFormatter) ax.grid(True) # Create the candelstick chart candlestick_ohlc(ax, list( zip(list(date2num(plotdat.index.tolist())), plotdat["open"].tolist(), plotdat["high"].tolist(), plotdat["low"].tolist(), plotdat["close"].tolist())), colorup="red", colordown="green", width=stick * .4) # Plot other series (such as moving averages) as lines if otherseries != None: if type(otherseries) != list: otherseries = [otherseries] dat.loc[:, otherseries].plot(ax=ax, lw=1.3, grid=True) ax.xaxis_date() ax.autoscale_view() plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right') plt.show() return fig
def candleLinePlots(candleData, candleTitle='a', **kwargs): Date = [date2num(date) for date in candleData.index] candleData.loc[:, 'Date'] = Date listData = [] for i in range(len(candleData)): a = [candleData.Date[i],\ candleData.Open[i],candleData.High[i],\ candleData.Low[i],candleData.Close[i]] listData.append(a) # 如 果 不 定 長 參 數 無 取 值 , 只 畫 蠟 燭 圖 ax = plt.subplot() # 如 果 不 定 長 參 數 有 值 , 則 分 成 兩 個 子 圖 flag = 0 if kwargs: if kwargs['splitFigures']: ax = plt.subplot(211) ax2 = plt.subplot(212) flag = 1 for key in kwargs: if key == 'title': ax2.set_title(kwargs[key]) if key == 'ylabel': ax2.set_ylabel(kwargs[key]) if key == 'grid': ax2.grid(kwargs[key]) if key == 'Data': plt.sca(ax) if flag: plt.sca(ax2) #一維數據 if kwargs[key].ndim == 1: plt.plot(kwargs[key],\ color='k',\ label=kwargs[key].name) plt.legend(loc='best') #二維數據有兩個columns elif all([kwargs[key].ndim==2,\ len(kwargs[key].columns)==2]): plt.plot(kwargs[key].iloc[:, 0], color='k', label=kwargs[key].iloc[:, 0].name) plt.plot(kwargs[key].iloc[:,1],\ linestyle='dashed',\ label=kwargs[key].iloc[:,1].name) plt.legend(loc='best') #二維數據有3個columns elif all([kwargs[key].ndim==2,\ len(kwargs[key].columns)==3]): plt.plot(kwargs[key].iloc[:, 0], color='k', label=kwargs[key].iloc[:, 0].name) plt.plot(kwargs[key].iloc[:, 1], linestyle='dashed', label=kwargs[key].iloc[:, 1].name) plt.bar(left=kwargs[key].iloc[:, 2].index, height=kwargs[key].iloc[:, 2], color='r', label=kwargs[key].iloc[:, 2].name) plt.legend(loc='best') mondays = WeekdayLocator(MONDAY) weekFormatter = DateFormatter('%y %b %d') ax.xaxis.set_major_locator(mondays) ax.xaxis.set_minor_locator(DayLocator()) ax.xaxis.set_major_formatter(weekFormatter) plt.sca(ax) candlestick_ohlc(ax,listData, width=0.7,\ colorup='r',colordown='g') ax.set_title(candleTitle) plt.setp(ax.get_xticklabels(),\ rotation=20,\ horizontalalignment='center') ax.autoscale_view() return (plt.show())
def plot_factor_info(factor_name, the_dates, signature, signature_avg, signature_std, \ signature_th04, signature_th04_avg, signature_th04_std,\ signature_th06, signature_th06_avg, signature_th06_std,\ signature_th08, signature_th08_avg, signature_th08_std,\ scores, scores_avg, scores_std, \ basepath, TIME_SLOTS_PER_DAY, plot_scores=True, \ whole_series_std_dev=None, \ whole_series_std_dev_th04=None, \ whole_series_std_dev_th06=None, \ whole_series_std_dev_th08=None): ''' Plot all the cluster signatures in a single plot ''' max_figs_per_line = 4 if plot_scores: max_rows_per_page = 7 else: max_rows_per_page = 5 f = plt.figure() f.set_size_inches(10 * max_figs_per_line, 4 * max_rows_per_page) ax = plt.subplot(max_rows_per_page, 1, 1) base_line, = ax.plot_date(x=the_dates, y=signature, fmt="-", linewidth=2) ax.xaxis.grid(True, which='major', color='gray', linestyle='-', alpha=1.0) ax.xaxis.grid(True, which='minor', color='gray', linestyle='--', alpha=0.4) ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d (%a)')) ax.xaxis.set_major_locator(WeekdayLocator(byweekday=MONDAY)) ax.xaxis.set_minor_locator(DayLocator()) ax.set_xlim([the_dates[0], the_dates[-1]]) ax.set_xlabel('Signature') ax.xaxis.set_label_coords(0.5, 0.96) for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] + ax.get_xticklabels() + ax.get_yticklabels()): item.set_fontsize(18) if whole_series_std_dev is not None: base_c = base_line.get_color() ax.fill_between(the_dates, np.array(signature) - np.array(whole_series_std_dev), np.array(signature) + np.array(whole_series_std_dev), alpha=0.4, edgecolor=base_c, facecolor=base_c) ax = plt.subplot(max_rows_per_page, 1, 2) base_line, = ax.plot_date(x=the_dates, y=signature_th04, fmt="-", linewidth=2) ax.xaxis.grid(True, which='major', color='gray', linestyle='-', alpha=1.0) ax.xaxis.grid(True, which='minor', color='gray', linestyle='--', alpha=0.4) ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d (%a)')) ax.xaxis.set_major_locator(WeekdayLocator(byweekday=MONDAY)) ax.xaxis.set_minor_locator(DayLocator()) ax.set_xlim([the_dates[0], the_dates[-1]]) ax.set_xlabel('Signature with th. 0.4') ax.xaxis.set_label_coords(0.5, 0.96) for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] + ax.get_xticklabels() + ax.get_yticklabels()): item.set_fontsize(18) if whole_series_std_dev is not None: base_c = base_line.get_color() ax.fill_between( the_dates, np.array(signature_th04) - np.array(whole_series_std_dev_th04), np.array(signature_th04) + np.array(whole_series_std_dev_th04), alpha=0.4, edgecolor=base_c, facecolor=base_c) ax = plt.subplot(max_rows_per_page, 1, 3) ax.plot_date(x=the_dates, y=signature_th06, fmt="-", linewidth=2) ax.xaxis.grid(True, which='major', color='gray', linestyle='-', alpha=1.0) ax.xaxis.grid(True, which='minor', color='gray', linestyle='--', alpha=0.4) ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d (%a)')) ax.xaxis.set_major_locator(WeekdayLocator(byweekday=MONDAY)) ax.xaxis.set_minor_locator(DayLocator()) ax.set_xlim([the_dates[0], the_dates[-1]]) ax.set_xlabel('Signature with th. 0.6') ax.xaxis.set_label_coords(0.5, 0.96) for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] + ax.get_xticklabels() + ax.get_yticklabels()): item.set_fontsize(18) if whole_series_std_dev is not None: base_c = base_line.get_color() ax.fill_between( the_dates, np.array(signature_th06) - np.array(whole_series_std_dev_th06), np.array(signature_th06) + np.array(whole_series_std_dev_th06), alpha=0.4, edgecolor=base_c, facecolor=base_c) ax = plt.subplot(max_rows_per_page, 1, 4) ax.plot_date(x=the_dates, y=signature_th08, fmt="-", linewidth=2) ax.xaxis.grid(True, which='major', color='gray', linestyle='-', alpha=1.0) ax.xaxis.grid(True, which='minor', color='gray', linestyle='--', alpha=0.4) ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d (%a)')) ax.xaxis.set_major_locator(WeekdayLocator(byweekday=MONDAY)) ax.xaxis.set_minor_locator(DayLocator()) ax.set_xlim([the_dates[0], the_dates[-1]]) ax.set_xlabel('Signature with th. 0.8') ax.xaxis.set_label_coords(0.5, 0.96) for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] + ax.get_xticklabels() + ax.get_yticklabels()): item.set_fontsize(18) if whole_series_std_dev is not None: base_c = base_line.get_color() ax.fill_between( the_dates, np.array(signature_th08) - np.array(whole_series_std_dev_th08), np.array(signature_th08) + np.array(whole_series_std_dev_th08), alpha=0.4, edgecolor=base_c, facecolor=base_c) ax = plt.subplot(max_rows_per_page, max_figs_per_line, 17) plotAverageSeries(signature_avg, signature_std, TIME_SLOTS_PER_DAY, ax=ax) ax.set_xlabel('Average Signature') ax.xaxis.set_label_coords(0.5, 0.96) for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] + ax.get_xticklabels() + ax.get_yticklabels()): item.set_fontsize(18) ax = plt.subplot(max_rows_per_page, max_figs_per_line, 18) plotAverageSeries(signature_th04_avg, signature_th04_std, TIME_SLOTS_PER_DAY, ax=ax) ax.set_xlabel('Average Signature with th. 0.4') ax.xaxis.set_label_coords(0.5, 0.96) for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] + ax.get_xticklabels() + ax.get_yticklabels()): item.set_fontsize(18) ax = plt.subplot(max_rows_per_page, max_figs_per_line, 19) plotAverageSeries(signature_th06_avg, signature_th06_std, TIME_SLOTS_PER_DAY, ax=ax) ax.set_xlabel('Average Signature with th. 0.6') ax.xaxis.set_label_coords(0.5, 0.96) for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] + ax.get_xticklabels() + ax.get_yticklabels()): item.set_fontsize(18) ax = plt.subplot(max_rows_per_page, max_figs_per_line, 20) plotAverageSeries(signature_th08_avg, signature_th08_std, TIME_SLOTS_PER_DAY, ax=ax) ax.set_xlabel('Average Signature with th. 0.8') ax.xaxis.set_label_coords(0.5, 0.96) for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] + ax.get_xticklabels() + ax.get_yticklabels()): item.set_fontsize(18) if plot_scores: ax = plt.subplot(max_rows_per_page, 1, 6) ax.plot_date(x=the_dates, y=scores, fmt="-", linewidth=2) ax.xaxis.grid(True, which='major', color='gray', linestyle='-', alpha=1.0) ax.xaxis.grid(True, which='minor', color='gray', linestyle='--', alpha=0.4) ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d (%a)')) ax.xaxis.set_major_locator(WeekdayLocator(byweekday=MONDAY)) ax.xaxis.set_minor_locator(DayLocator()) ax.set_xlim([the_dates[0], the_dates[-1]]) ax.set_xlabel('Scores') ax.xaxis.set_label_coords(0.5, 0.96) for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] + ax.get_xticklabels() + ax.get_yticklabels()): item.set_fontsize(18) ax = plt.subplot(max_rows_per_page, max_figs_per_line, 25) plotAverageSeries(scores_avg, scores_std, TIME_SLOTS_PER_DAY, ax=ax) ax.set_xlabel('Average Scores') ax.xaxis.set_label_coords(0.5, 0.96) for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] + ax.get_xticklabels() + ax.get_yticklabels()): item.set_fontsize(18) plt.tight_layout() if plot_scores: plt.savefig('%s%s_signatures_and_scores.pdf' % (basepath, factor_name)) else: plt.savefig('%s%s_signatures.pdf' % (basepath, factor_name)) plt.close()
import datetime from pylab import * from matplotlib.dates import MONDAY, SATURDAY from matplotlib.finance import quotes_historical_yahoo from matplotlib.dates import MonthLocator, WeekdayLocator, DateFormatter from matplotlib.ticker import FormatStrFormatter # the start and end date range for the financial plots date1 = datetime.date(2003, 1, 1) date2 = datetime.date(2004, 4, 12) # the tick locators and formatters mondays = WeekdayLocator(MONDAY) # every monday months = MonthLocator() # every month monthsFmt = DateFormatter('%b %d') # looks like May 01 dollarFmt = FormatStrFormatter('$%0.2f') # dollars! # get some financial data from the finance module quotes = quotes_historical_yahoo('INTC', date1, date2) if not quotes: raise SystemExit # failsafe # extract the date and opening prices from the quote tuples dates = [q[0] for q in quotes] opens = [q[1] for q in quotes] # plot_date will choose a default date ticker and formatter ax = subplot(111) plot_date(dates, opens, markeredgecolor='k') # but we'll override the default with our custom locators and # formatters
basearr = np.zeros((len(thevals), 2)) bigarr = np.zeros((len(datestrings), len(thevals), 2)) for k,numpv in enumerate(thevals): basefile = ('basesamp_num%02d.npy'%(numpv)) bdata = np.load(basefile) basearr[k,0] = np.mean(bdata[0:250]) basearr[k,1] = np.std(bdata[0:250]) for j,thisdate in enumerate(datestrings): datafile = ('rbsamp_%s_num%02d.npy'%(thisdate,numpv)) thedata = np.load(datafile) bigarr[j,k,0] = np.mean(thedata[0:250]) bigarr[j,k,1] = np.std(thedata[0:250]) formatter = DateFormatter('%m/%d/%y') loc = WeekdayLocator(byweekday=TU, interval=1) with PdfPages('baseline_bleaching_compare_20191126c.pdf') as pdf: for k,numpv in enumerate(thevals): fig = plt.figure(figsize=(10,8)) ax = plt.subplot(1, 1, 1) ax.set_title('Hawaii Weekly Refl.') ax.xaxis.set_major_locator(loc) ax.xaxis.set_major_formatter(formatter) ax.xaxis.set_tick_params(rotation=30, labelsize=7) plt.errorbar(thedates, np.repeat(basearr[k,0], len(thedates)), yerr=np.repeat(basearr[k,1], len(thedates)), fmt='-b', capsize=5, label='Baseline') plt.errorbar(thedates, bigarr[:,k,0], yerr=bigarr[:,k,1], fmt='--r', capsize=5, label='PV %d'%(numpv)) plt.legend(loc='lower right') pdf.savefig(fig) plt.close()
def pandas_candlestick_ohcl(dat,stick ="day", otherseries = None): """ :param dat:pandas DataFrame object with datetime64 index, and float columns "Open","Hign","Low", and "Close", likely created via DataReader from "yahoo" :param stick: A string or number indicating the period of time covered by a single candlestick. Valid inputs are "Day","Week","Month","Year" ("Day" = default) and any numerical input dictates the number of trading days in a period :param otherseries: An iterable that will be coerced in to a list containing the columns of data that hold other series to be plotted as lines. """ mondays = WeekdayLocator(MONDAY) #for all the major ticks on mondays alldays = DayLocator() #and minor ticks for every other day dayFormatter = DateFormatter('%d') #Create a new DataFrame which includes OHLC data for each data period specified by the stick input transdat = dat.loc[:,["Open","High","Low","Close"]] if(type(stick)==str): if stick =="day": plotdat = transdat stick = 1 elif stick in["week","month","year"]: if stick == "week": transdat["week"] = pd.to_datetime(transdat.index).map(lambda x: x.isocalendar()[1]) elif stick =="month": transdat["month"] = pd.to_datetime(transdat.index).map(lambda x: x.month()[1]) transdat["year"] = pd.to_datetime(transdat.index).map(lambda x: x.isocalendar()[0]) grouped = transdat.groupby(list(set(["year",stick]))) plotdat = pd.DataFrame({"Open":[],"High":[],"Low":[],"Close":[]}) #Creates empty data frame for name, group in grouped: plotdat = plotdat.append(pd.DataFrame({"Open":group.iloc[0,0], "High":max(group.High), "Low":min(group.Low), "Close":group.iloc[-1,3]}, index = [group.index[0]])) if stick =="week":stick = 5 elif stick =="month":stick = 30 elif stick =="year":stick = 365 elif(type(stick) == int and stick >= 1): transdat["stick"]=[np.floor(i / stick) for i in range(len(transdat.index))] grouped = transdat.groupby("stick") plotdat = pd.DataFrame({"Open":[],"High":[],"Low":[],"Close":[]}) for name, group in grouped: plotdat = plotdat.append(pd.DataFrame({"Open":group.iloc[0,0], "High":max(group.High), "Low":min(group.Low), "Close":group.iloc[-1,3]}, index=[group.index[0]])) else: raise ValueError("Valid arguments to include 'stick' include the strings 'day','month','year' or a valid integer") #Set plot parameters, including the axis object ax which is used for plotting fig, ax = plt.subplots() fig.subplots_adjust(bottom = 0.2) if plotdat.index[-1] - plotdat.index[0] < pd.Timedelta('730 days'): weekFormatter = DateFormatter('%b','%d') # for example January 5th ax.xaxis.set_major_locator(mondays) ax.xaxis.set_minor_locator(alldays) else: weekFormatter = DateFormatter('%b','%d','%Y') ax.xaxis.set_major_formatter(weekFormatter) ax.grid(True) #Create the candlestick chart candlestick_ohlc(ax, list(zip(list(date2num(plotdat.index.tolist())), plotdat["Open"].tolist(), plotdat["High"].tolist(), plotdat["Low"].tolist(), plotdat["Close"].tolist())), colorup = "black", colordown = "red", width = stick*.4) #Plot other series(ex.Moving averages) as lines if otherseries != None: if type(otherseries) != list: otherseries = [otherseries] dat.log[:,otherseries].plot(ax = ax, lw = 1.3, grid = True) ax.axis_date() ax.autoscale_view() plt.setp(plt.gcal().getxticklabels(), rotation=45, horizontalalignment = 'right') plt.show()
def plotTIC(msData, addViolin=True, addBatchShading=False, addLineAtGaps=False, colourByDetectorVoltage=False, logy=False, title='', withExclusions=True, savePath=None, figureFormat='png', dpi=72, figureSize=(11, 7)): """ Visualise TIC for all or a subset of features coloured by either dilution value or detector voltage. With the option to shade by batch. .. note:: addViolin and colourByDetectorVoltage are mutually exclusive. :param MSDataset msData: Dataset object :param bool addViolin: If ``True`` adds violin plots of TIC distribution pre and post correction split by sample type :param bool addBatchShading: If ``True`` shades plot according to sample batch :param bool addLineAtGaps: If ``True`` adds line where acquisition time is greater than double the norm :param bool colourByDetectorVoltage: If ``True`` colours points by detector voltage, else colours by dilution :param bool logy: If ``True`` plot y on a log scale :param str title: Title for the plot :param bool withExclusions: If ``False``, discard masked features from the sum :param savePath: If ``None`` plot interactively, otherwise save the figure to the path specified :type savePath: None or str :param str figureFormat: If saving the plot, use this format :param int dpi: Plot resolution :param figureSize: Dimensions of the figure :type figureSize: tuple(float, float) """ # Check inputs if (addViolin) & (colourByDetectorVoltage): raise ValueError( 'addViolin and colourByDetectorVoltage cannot both be True') sns.set_color_codes(palette='deep') fig = plt.figure(figsize=figureSize, dpi=dpi) gs = gridspec.GridSpec(1, 5) if addViolin: ax = plt.subplot(gs[0, :-1]) ax2 = plt.subplot(gs[0, -1]) else: ax = plt.subplot(gs[0, :-1]) # Load toolbox wide color scheme if 'sampleTypeColours' in msData.Attributes.keys(): sTypeColourDict = copy.deepcopy(msData.Attributes['sampleTypeColours']) for stype in SampleType: if stype.name in sTypeColourDict.keys(): sTypeColourDict[stype] = sTypeColourDict.pop(stype.name) else: sTypeColourDict = { SampleType.StudySample: 'b', SampleType.StudyPool: 'g', SampleType.ExternalReference: 'r', SampleType.MethodReference: 'm', SampleType.ProceduralBlank: 'c', 'Other': 'grey' } # Mask features with inf values tempFeatureMask = numpy.sum(numpy.isfinite(msData.intensityData), axis=0) tempFeatureMask = tempFeatureMask < msData.intensityData.shape[0] tempFeatureMask = (tempFeatureMask == False) if withExclusions: tempFeatureMask = numpy.logical_and(tempFeatureMask, msData.featureMask) # Define sample types SSmask = (msData.sampleMetadata['SampleType'].values == SampleType.StudySample) & ( msData.sampleMetadata['AssayRole'].values == AssayRole.Assay) SPmask = (msData.sampleMetadata['SampleType'].values == SampleType.StudyPool) & ( msData.sampleMetadata['AssayRole'].values == AssayRole.PrecisionReference) ERmask = (msData.sampleMetadata['SampleType'].values == SampleType.ExternalReference) & ( msData.sampleMetadata['AssayRole'].values == AssayRole.PrecisionReference) LRmask = (msData.sampleMetadata['SampleType'].values == SampleType.StudyPool) & ( msData.sampleMetadata['AssayRole'].values == AssayRole.LinearityReference) # X axis limits for formatting minX = msData.sampleMetadata['Acquired Time'].loc[ msData.sampleMetadata['Run Order'] == min( msData.sampleMetadata['Run Order'][SSmask | SPmask | ERmask | LRmask])].values maxX = msData.sampleMetadata['Acquired Time'].loc[ msData.sampleMetadata['Run Order'] == max( msData.sampleMetadata['Run Order'][SSmask | SPmask | ERmask | LRmask])].values delta = maxX - minX days = delta.astype('timedelta64[D]') days = days / numpy.timedelta64(1, 'D') if days < 7: loc = WeekdayLocator(byweekday=(MO, TU, WE, TH, FR, SA, SU)) else: loc = WeekdayLocator(byweekday=(MO, SA)) formatter = DateFormatter('%d/%m/%y') tic = numpy.sum(msData.intensityData[:, tempFeatureMask == True], axis=1) # If colouring by detector voltage if colourByDetectorVoltage: # Generate sample change in detector voltage detectorDiff = msData.sampleMetadata[[ 'Detector', 'Run Order' ]].sort_values(by='Run Order')['Detector'].diff().sort_index() detectorDiff[0] = 0 # no detector diff for first sample cMax = max(abs(detectorDiff)) # colorbar symmetrical around 0 cMin = -cMax # Ensure 'Acquired Time' is datetime.datetime, if it's already a datetime it will trigger an AttributeError try: acqTime = numpy.array([ xtime.to_pydatetime() for xtime in msData.sampleMetadata['Acquired Time'].tolist() ]) except AttributeError: acqTime = numpy.array( msData.sampleMetadata['Acquired Time'].tolist()) # Plot TIC for different sample types, colored by change in detector voltage if cMax != 0: if sum(SSmask != 0): sc = ax.scatter(acqTime[SSmask], tic[SSmask], marker='o', c=detectorDiff[SSmask], cmap=plt.cm.get_cmap('bwr'), vmin=cMin, vmax=cMax, label='Study Sample', edgecolors='grey') if sum(SPmask != 0): sc = ax.scatter(acqTime[SPmask], tic[SPmask], marker='v', s=30, linewidth=0.9, c=detectorDiff[SPmask], cmap=plt.cm.get_cmap('bwr'), vmin=cMin, vmax=cMax, label='Study Pool', edgecolors='grey') if sum(ERmask != 0): sc = ax.scatter(acqTime[ERmask], tic[ERmask], marker='^', s=30, linewidth=0.9, c=detectorDiff[ERmask], cmap=plt.cm.get_cmap('bwr'), vmin=cMin, vmax=cMax, label='External Reference', edgecolors='grey') if sum(LRmask != 0): sc = ax.scatter(acqTime[LRmask], tic[LRmask], marker='s', c=detectorDiff[LRmask], cmap=plt.cm.get_cmap('bwr'), vmin=cMin, vmax=cMax, label='Linearity Reference', edgecolors='grey') # For the specific case where there is no detector voltage and colorscale collapses else: if sum(SSmask != 0): sc = ax.scatter(acqTime[SSmask], tic[SSmask], marker='o', c='w', cmap=plt.cm.get_cmap('bwr'), vmin=cMin, vmax=cMax, label='Study Sample', edgecolors='grey') if sum(SPmask != 0): sc = ax.scatter(acqTime[SPmask], tic[SPmask], marker='v', s=30, linewidth=0.9, c='w', cmap=plt.cm.get_cmap('bwr'), vmin=cMin, vmax=cMax, label='Study Pool', edgecolors='grey') if sum(ERmask != 0): sc = ax.scatter(acqTime[ERmask], tic[ERmask], marker='^', s=30, linewidth=0.9, c='w', cmap=plt.cm.get_cmap('bwr'), vmin=cMin, vmax=cMax, label='External Reference', edgecolors='grey') if sum(LRmask != 0): sc = ax.scatter(acqTime[LRmask], tic[LRmask], marker='s', c='w', cmap=plt.cm.get_cmap('bwr'), vmin=cMin, vmax=cMax, label='Linearity Reference', edgecolors='grey') # Colour by sample type else: # Plot TIC for different sample types if sum(SSmask != 0): ax.plot_date(msData.sampleMetadata.loc[SSmask, 'Acquired Time'].values, tic[SSmask], c=sTypeColourDict[SampleType.StudySample], fmt='o', ms=6, label='Study Sample') # c='y', if sum(SPmask != 0): ax.plot_date(msData.sampleMetadata.loc[SPmask, 'Acquired Time'].values, tic[SPmask], c=sTypeColourDict[SampleType.StudyPool], fmt='v', ms=8, label='Study Pool') # c='m', if sum(ERmask != 0): ax.plot_date(msData.sampleMetadata.loc[ERmask, 'Acquired Time'].values, tic[ERmask], c=sTypeColourDict[SampleType.ExternalReference], fmt='^', ms=8, label='External Reference') if sum(LRmask != 0): ax.plot_date(msData.sampleMetadata.loc[LRmask, 'Acquired Time'].values, tic[LRmask], c=sTypeColourDict[SampleType.MethodReference], fmt='s', ms=6, label='Linearity Reference') # Shade by automatically defined batches (if required) if addBatchShading: sampleMask = SSmask | SPmask | ERmask # Unique batches if 'Correction Batch' in msData.sampleMetadata.columns: batches = (numpy.unique( msData.sampleMetadata.loc[sampleMask, 'Correction Batch']. values[~numpy.isnan(msData.sampleMetadata.loc[ sampleMask, 'Correction Batch'].values)])).astype(int) else: batches = (numpy.unique(msData.sampleMetadata.loc[ sampleMask, 'Batch'].values[~numpy.isnan(msData.sampleMetadata.loc[ sampleMask, 'Batch'].values)])).astype(int) # Define the colours (different for each batch) and get axis y-limits cmap = plt.get_cmap('gnuplot') colors = [cmap(i) for i in numpy.linspace(0, 1, len(batches) + 1)] ymin, ymax = ax.get_ylim() colIX = 1 # Add shading for each batch for i in batches: # Create rectangle x coordinates start = msData.sampleMetadata[ msData.sampleMetadata['Run Order'] == min( msData.sampleMetadata[ msData.sampleMetadata['Correction Batch'].values == i] ['Run Order'])]['Acquired Time'] end = msData.sampleMetadata[ msData.sampleMetadata['Run Order'] == max( msData.sampleMetadata[ msData.sampleMetadata['Correction Batch'].values == i] ['Run Order'])]['Acquired Time'] # Convert to matplotlib date representation start = mdates.date2num(start) end = mdates.date2num(end) # Plot rectangle rect = Rectangle((start, ymin), end - start, abs(ymin) + abs(ymax), color=colors[colIX], alpha=0.4, label='Batch %d' % (i), zorder=0) ax.add_patch(rect) colIX = colIX + 1 else: # Still might need the batch information even if not using it for batch shading sampleMask = SSmask | SPmask | ERmask if 'Correction Batch' in msData.sampleMetadata.columns: batches = (numpy.unique( msData.sampleMetadata.loc[sampleMask, 'Correction Batch']. values[~numpy.isnan(msData.sampleMetadata.loc[ sampleMask, 'Correction Batch'].values)])).astype(int) else: batches = (numpy.unique(msData.sampleMetadata.loc[ sampleMask, 'Batch'].values[~numpy.isnan(msData.sampleMetadata.loc[ sampleMask, 'Batch'].values)])).astype(int) # Add violin plot of data distribution (if required) if addViolin: sampleMasks = list() palette = {} if sum(SSmask) > 0: sampleMasks.append(('SS', SSmask)) palette['SS'] = sTypeColourDict[SampleType.StudySample] if sum(SPmask) > 0: sampleMasks.append(('SP', SPmask)) palette['SP'] = sTypeColourDict[SampleType.StudyPool] if sum(ERmask) > 0: sampleMasks.append(('ER', ERmask)) palette['ER'] = sTypeColourDict[SampleType.ExternalReference] if sum(LRmask) > 0: sampleMasks.append(('LR', LRmask)) palette['LR'] = sTypeColourDict[SampleType.MethodReference] limits = ax.get_ylim() _violinPlotHelper(ax2, tic, sampleMasks, None, 'Sample Type', palette=palette, ylimits=limits, logy=logy) sns.despine(trim=True, ax=ax2) # Annotate figure ax.set_xlabel('Acquisition Date') ax.set_ylabel('TIC') ax.set_xlim(minX, maxX) ax.set_xticklabels(ax.xaxis.get_majorticklabels(), rotation=45) ax.xaxis.set_major_locator(loc) ax.xaxis.set_major_formatter(formatter) try: ax.set_ylim(ymin, ymax) except: pass if logy: ax.set_yscale('symlog', nonposy='clip') else: ax.ticklabel_format(style='sci', axis='y', scilimits=(0, 0)) if colourByDetectorVoltage and cMax != 0: cbaxes = fig.add_axes([ 0.81, 0.15, 0.03, 0.64 - (len(batches) * 0.04) ]) # shorter color bar as more batches are present cbar = plt.colorbar(sc, cax=cbaxes) cbar.set_label('Change in Detector Voltage') leg = ax.legend(loc='upper left', bbox_to_anchor=(1, 1)) elif addViolin == False: ax.legend(loc='upper left', bbox_to_anchor=(1, 1)) fig.suptitle(title) # Save or output if savePath: try: plt.savefig(savePath, bbox_extra_artists=(leg, ), bbox_inches='tight', format=figureFormat, dpi=dpi) except UnboundLocalError: plt.savefig(savePath, bbox_inches='tight', format=figureFormat, dpi=dpi) plt.close() else: plt.show()
#print (c.fetchone()) temperature = c.fetchall() c.execute('SELECT pressure FROM bme280_data;') #print (c.fetchone()) pressure = c.fetchall() c.execute('SELECT humidity FROM bme280_data;') #print (c.fetchone()) humidity = c.fetchall() print(type(humidity[0])) #plt.plot_date(temperature, pressure, '.') #plt.scatter(pressure, humidity) loc = WeekdayLocator(byweekday=(MO, TU, WE, TH, FR, SA, SU,)) formatter = DateFormatter('%Y-%m-%d') locator = AutoDateLocator() formatter = ConciseDateFormatter(locator) fig, (ax1) = plt.subplots(1, sharex='all') fig.autofmt_xdate(rotation=90) color = 'tab:red' ax1.plot_date(timestamp, temperature, fmt='o', color=color) ax1.tick_params(axis='y', labelcolor=color) ax1.xaxis.set_major_locator(DayLocator()) ax1.xaxis.set_major_formatter(formatter) #ax1.xaxis.set_major_locator() #ax1.xaxis.set_minor_locator(AutoMinorLocator())
def weekFormatter(): weekFmt = DateFormatter('%b %d') mondays = WeekdayLocator(MONDAY) alldays = DayLocator() allweeks = WeekdayLocator() return mondays, alldays, allweeks, weekFmt
def main(): #----------------------------------------------------------------------------------------- # Initializations for FTIR #----------------------------------------------------------------------------------------- loc = 'fl0' gasName = ['co', 'c2h2', 'c2h6', 'ch4', 'nh3', 'hcn', 'h2co', 'hcooh'] ver = [ 'Current_v3', 'Current_v2', 'Current_v2', 'Current_WP', 'Current_v2', 'Current_v1', 'Current_v8', 'Current_v3' ] # Name of retrieval version to process ctlF = [ 'sfit4_v3.ctl', 'sfit4_v2.ctl', 'sfit4_v2.ctl', 'sfit4_3.ctl', 'sfit4_v2.ctl', 'sfit4.ctl', 'sfit4_v8.ctl', 'sfit4_v3.ctl' ] #gasName = ['h2co', ] #ver = ['Current_v8' ] # Name of retrieval version to process #ctlF = ['sfit4_v8.ctl'] #---------------------- # First Level Retrieval Directory #---------------------- retDir = '/data1/ebaumer/' + loc.lower() dataPath = '/data/iortega/results/' + loc.lower() + '/data/' saveFlg = True pltDir = '/data/iortega/pbin/tropGases/fig/' pltFile = pltDir + 'printCAM_v2.pdf' #------ # Flags #------ errorFlg = True # Flag to process error data fltrFlg = True # Flag to filter the data byYrFlg = False # Flag to create plots for each individual year in date range szaFlg = True # Flag to filter based on min and max SZA dofFlg = True # Flag to filter based on min DOFs pcNegFlg = True # Flag to filter profiles with negative partial columns tcNegFlg = True # Flagsag to filter profiles with negative total columns tcMMFlg = False # Flag to filter based on min and max total column amount cnvrgFlg = True # Flag to filter profiles that did not converge rmsFlg = True # Flag to filter based on max RMS chiFlg = False # Flag to filter based on max CHI_2_Y mnthFlg = False # Flag to filter based on mnths = [6, 7, 8] maxRMS = [ 1.5, 0.6, 1.0, 0.3, 1.0, 1.5, 0.5, 1.5 ] # Max Fit RMS to filter data. Data is filtered according to <= maxrms minDOF = [1.0, 0.5, 0.5, 0.9, 0.5, 0.5, 0.5, 0.5] # Min DOFs for filtering #maxRMS = [0.5] # Max Fit RMS to filter data. Data is filtered according to <= maxrms #minDOF = [0.5] minSZA = 0.0 # Min SZA for filtering maxSZA = 90.0 # Max SZA for filtering maxCHI = 2.0 # Max CHI_y_2 value maxTC = 5.0E24 # Max Total column amount for filtering minTC = 0.0 # Min Total column amount for filtering sclfct = 1.0E9 # Scale factor to apply to vmr plots (ppmv=1.0E6, ppbv=1.0E9, etc) sclfctName = 'ppbv' # Name of scale factor for labeling plots pColsFlg = True pCols = [1.6, 8.0] #--ALTITUDE TO CALCULATE PARTIAL COLUMNS AND WEIGHTED VMR #----------------------------------------------------------------------------------------- # Initializations for CAM-CHEM #----------------------------------------------------------------------------------------- #dataDirCAM = '/data1/ancillary_data/'+loc.lower()+'/' #fileCAM = 'CAM_chem_fmerra_FSDSSOA_2deg_2000_2014_extra_Boulder.nc' #dataDirCAM = '/net/modeling1/data16a/buchholz/CAM_chem_output/CAM_chem_fmerra2_FCSD_1deg_Boulder/' # dataDirCAM = '/net/modeling1/data16a/buchholz/CAM_chem_output/CAM_chem_fmerra2_FCSD_1deg_Boulder_finn/' # #fileCAM = 'CAM_chem_fmerra2_FCSD_1deg_Boulder_2009_2017.nc' fileCAM = 'CAM_chem_fmerra2_FCSD_1deg_FINN_Boulder_2009_2017.nc' sLat = 40.4 #--LATITUDE OF BOULDER sLon = -105.24 #--LONGITUDE OF BOULDER interpFlg = False #---------------------- # Date range to process #---------------------- iyear = 2010 imnth = 1 iday = 1 fyear = 2017 fmnth = 12 fday = 31 #----------------------------# # # # --- START --- # # # #----------------------------# if saveFlg: pdfsav = PdfPages(pltFile) #------------------------------------------------- # -- Read CAM-CHEM -- #------------------------------------------------- DataCAM = dm.CAMClass(dataDirCAM, fileCAM, outFname='', saveFlg=False) DataCAM.ReadOutputCAM(gasName, pCols, sLat, sLon, interpFlg=interpFlg) #DataCAM.PltCAM() # DatesCAM = np.asarray(DataCAM.CAM['dates']) # PrfCAM = np.asarray(DataCAM.CAM['GasPrf_'+gasName[0]][:,:,0,0])*1e9 # mnthlyVals = mf.mnthlyAvg(PrfCAM, DatesCAM, dateAxis=0, meanAxis=0) # vmrPCAM_smt_mnth = mnthlyVals['mnthlyAvg'] # vmrPCAM_smt_STDmnth = mnthlyVals['std'] # DatesCAM_mnth = mnthlyVals['dates'] # print mnthlyVals['dates'] #exit() #user_input = raw_input('Press any key to exit >>> ') #sys.exit() #--------------------------------- # -- Read FTS -- #--------------------------------- fts = cfts.FTSClass(gasName, retDir, ctlF, ver, iyear, imnth, iday, fyear, fmnth, fday) fts.ReadFTS(fltrFlg=fltrFlg, sclfct=sclfct, sclname=sclfctName, mnthFltr=mnths, mnthFltFlg=mnthFlg, errFlg=errorFlg, minSZA=minSZA, maxSZA=maxSZA, maxRMS=maxRMS, minTC=minTC, maxTC=maxTC, minDOF=minDOF, maxCHI=maxCHI, dofFlg=dofFlg, rmsFlg=rmsFlg, tcFlg=tcNegFlg, pcFlg=pcNegFlg, szaFlg=szaFlg, cnvrgFlg=cnvrgFlg, chiFlg=chiFlg, tcMMflg=tcMMFlg, pColsFlg=pColsFlg, pCols=pCols) #--------------------------------- # #--------------------------------- GasVer = [] for (g, v) in izip(gasName, ver): GasVer.append(g + '_' + v) #-------------- for (g, v) in izip(gasName, ver): if DataCAM.CAM['GasFlg_' + g.lower()]: gv = g + '_' + v #--------------------------------- # Defining variables (CAM-CHEM) #--------------------------------- altCAM = np.asarray( DataCAM.CAM['midpoints'][0, :, DataCAM.CAM['indsLoc'][0], DataCAM.CAM['indsLoc'][1]]) DatesCAM = np.asarray(DataCAM.CAM['dates']) PrfCAM = np.asarray( DataCAM.CAM['GasPrf_' + g][:, :, DataCAM.CAM['indsLoc'][0], DataCAM.CAM['indsLoc'][1]]) * 1e9 AirmassCAM = np.asarray( DataCAM.CAM['AIRMASS_' + g][:, :, DataCAM.CAM['indsLoc'][0], DataCAM.CAM['indsLoc'][1]]) pressCAM = np.asarray( DataCAM.CAM['pressure'][:, :, DataCAM.CAM['indsLoc'][0], DataCAM.CAM['indsLoc'][1]]) #--------------------------------- # Defining variables (FTIR) #--------------------------------- altFTS = fts.alt[gv] DatesFTS = fts.dates[gv] PrfFTS = fts.rPrfVMR[gv] AirmassFTS = fts.Airmass[gv] aPrfFTS = fts.aPrfVMR[gv] akVMRFTS = fts.avkVMR[gv] vmrPFTS = fts.vmrP[gv] aPrfFTSMean = np.mean(aPrfFTS, axis=0) akVMRFTSMean = np.mean(akVMRFTS, axis=0) #---------------------------- # Monthly - FTIR #---------------------------- mnthlyVals = mf.mnthlyAvg(PrfFTS, DatesFTS, dateAxis=0, meanAxis=0) PrfFTS_mnth = mnthlyVals['mnthlyAvg'] DatesFTS_mnth = mnthlyVals['dates'] mnthlyVals = mf.mnthlyAvg(AirmassFTS, DatesFTS, dateAxis=0, meanAxis=0) AirmassFTS_mnth = mnthlyVals['mnthlyAvg'] mnthlyVals = mf.mnthlyAvg(PrfFTS, DatesFTS, dateAxis=0, meanAxis=0) PrfFTS_mnth = mnthlyVals['mnthlyAvg'] mnthlyVals = mf.mnthlyAvg(akVMRFTS, DatesFTS, dateAxis=0, meanAxis=0) akVMRFTS_mnth = mnthlyVals['mnthlyAvg'] mnthlyVals = mf.mnthlyAvg(vmrPFTS, DatesFTS, dateAxis=0, meanAxis=0) vmrPFTS_mnth = mnthlyVals['mnthlyAvg'] vmrPFTS_STDmnth = mnthlyVals['std'] #---------------------------- # Altitude Interpolation #---------------------------- PrfCAM_int = interpolate.interp1d(altCAM, PrfCAM, axis=1, fill_value='extrapolate', bounds_error=False)(altFTS) AirmassCAM_int = interpolate.interp1d(altCAM, AirmassCAM, axis=1, fill_value='extrapolate', bounds_error=False)(altFTS) pressCAM_int = interpolate.interp1d(altCAM, pressCAM, axis=1, fill_value='extrapolate', bounds_error=False)(altFTS) #---------------------------- # Smoothing #---------------------------- PrfCAM_smt = np.zeros((len(DatesCAM), len(altFTS))) for itime in range(len(DatesCAM)): PrfCAM_smt[itime, :] = aPrfFTSMean + np.dot( akVMRFTSMean, (PrfCAM_int[itime, :] - aPrfFTSMean)) AirmassFTSMean = np.mean(AirmassFTS, axis=0) AirmassFTSstd = np.std(AirmassFTS, axis=0) #---------------------------- # Weighted VMR #---------------------------- inds = np.where((altFTS >= pCols[0]) & (altFTS <= pCols[1]))[0] vmrPCAM_smt = np.average(PrfCAM_smt[:, inds], axis=1, weights=pressCAM_int[:, inds]) vmrPCAM_int = np.average(PrfCAM_int[:, inds], axis=1, weights=pressCAM_int[:, inds]) vmrPCAM = np.average(PrfCAM[:, inds], axis=1, weights=pressCAM[:, inds]) #--------------------------------- # Plot : Prf Mean #--------------------------------- PrfmeanCAM = np.mean(PrfCAM, axis=0) prfSTDCAM = np.std(PrfCAM, axis=0) PrfmeanCAM_int = np.mean(PrfCAM_int, axis=0) prfSTDCAM_int = np.std(PrfCAM_int, axis=0) PrfmeanCAM_smt = np.mean(PrfCAM_smt, axis=0) prfSTDCAM_smt = np.std(PrfCAM_smt, axis=0) prfMeanFTS = np.mean(PrfFTS_mnth, axis=0) prfSTDFTS = np.std(PrfFTS_mnth, axis=0) #---------------------------- # Monthly - CAM-Chem #--------------------------- mnthlyVals = mf.mnthlyAvg(vmrPCAM_smt, DatesCAM, dateAxis=0, meanAxis=0) vmrPCAM_smt_mnth = mnthlyVals['mnthlyAvg'] vmrPCAM_smt_STDmnth = mnthlyVals['std'] DatesCAM_mnth = mnthlyVals['dates'] mnthlyVals = mf.mnthlyAvg(vmrPCAM, DatesCAM, dateAxis=0, meanAxis=0) vmrPCAM_mnth = mnthlyVals['mnthlyAvg'] vmrPCAM_STDmnth = mnthlyVals['std'] #---------------------------- # Print in Dat #---------------------------- with open(dataPath + g + '_CAM-CHEM_v2_finn.dat', 'w') as fopen: YYYYMMDD = np.asarray([ '{0:4d}-{1:02d}-{2:02d}'.format(d.year, d.month, d.day) for d in DatesCAM_mnth ]) fopen.write( 'Index, YYYY-MM-DD, wVMRsmth, wVMRsmth_std, wVMR, wVMR_std \n' ) strFormat = '{0:d}, {1:>10s}, {2:.3E}, {3:.3E}, {4:.3E}, {5:.3E}\n' for i, sngTime in enumerate(YYYYMMDD): fopen.write( strFormat.format( (i + 1), YYYYMMDD[i], vmrPCAM_smt_mnth[i], vmrPCAM_smt_STDmnth[i], vmrPCAM_mnth[i], vmrPCAM_STDmnth[i])) #---------------------------- # #---------------------------- xmin = dt.date(iyear, imnth, iday) xmax = dt.date(fyear, fmnth, fday) clmap = 'jet' cm = plt.get_cmap(clmap) dayLc = DayLocator() yearsLc = YearLocator() monthLc = MonthLocator() mondays = WeekdayLocator(MONDAY) #DateFmt = DateFormatter('%b %d') DateFmt = DateFormatter('%Y') #--------------------------------- # Plot : Averaging Kernel Smoothing Function (row of avk) #--------------------------------- fig = plt.figure(figsize=(9, 9)) gs = gridspec.GridSpec(1, 2, width_ratios=[3, 1]) ax = plt.subplot(gs[0]) axb = plt.subplot(gs[1]) cm = plt.get_cmap(clmap) cNorm = colors.Normalize(vmin=np.min(altFTS), vmax=np.max(altFTS)) scalarMap = mplcm.ScalarMappable(norm=cNorm, cmap=clmap) scalarMap.set_array(altFTS) #ax.set_color_cycle([scalarMap.to_rgba(x) for x in altFTS]) ax.set_prop_cycle( cycler('color', [scalarMap.to_rgba(x) for x in altFTS])) for i in range(len(altFTS)): ax.plot(akVMRFTSMean[i, :], altFTS) ax.set_ylabel('Altitude [km]', fontsize=14) ax.set_xlabel('Averaging Kernels', fontsize=14) ax.grid(True) cbar = fig.colorbar(scalarMap, orientation='vertical') cbar.set_label('Altitude [km]', fontsize=14) ax.set_title(g.upper() + ' Averaging Kernels Scale Factor', fontsize=14) ax.tick_params(labelsize=14) axb.plot(np.sum(akVMRFTSMean, axis=0), altFTS, color='k') axb.grid(True) axb.set_xlabel('Averaging Kernel Area', fontsize=14) axb.tick_params(axis='x', which='both', labelsize=8) #axb.tick_params(labelsize=14) if saveFlg: pdfsav.savefig(fig, dpi=200) else: plt.show(block=False) #--------------------------------- # Plot : Prf #--------------------------------- fig, ax = plt.subplots(figsize=(7, 9)) ax.plot(prfMeanFTS, altFTS, linewidth=2.0, color='k', label='FTIR') ax.scatter(prfMeanFTS, altFTS, facecolors='white', s=60, color='k') ax.fill_betweenx(altFTS, prfMeanFTS - prfSTDFTS, prfMeanFTS + prfSTDFTS, alpha=0.5, color='k') ax.plot(PrfmeanCAM, altCAM, linewidth=2.0, color='red', label='CAM-Chem') ax.scatter(PrfmeanCAM, altCAM, facecolors='white', s=60, color='red') ax.fill_betweenx(altCAM, PrfmeanCAM - prfSTDCAM, PrfmeanCAM + prfSTDCAM, alpha=0.5, color='red') ax.plot(PrfmeanCAM_int, altFTS, linewidth=2.0, color='green', label='CAM-Chem - FTS grid') ax.scatter(PrfmeanCAM_int, altFTS, facecolors='white', s=60, color='green') ax.fill_betweenx(altFTS, PrfmeanCAM_int - prfSTDCAM_int, PrfmeanCAM_int + prfSTDCAM_int, alpha=0.5, color='green') ax.plot(PrfmeanCAM_smt, altFTS, linewidth=2.0, color='blue', label='CAM-Chem - Smoothed') #ax.scatter(PrfmeanCAM_smt,altFTS, facecolors='white', s=60, color='blue') #ax.fill_betweenx(altFTS,PrfmeanCAM_smt-prfSTDCAM_smt,PrfmeanCAM_smt+prfSTDCAM_smt,alpha=0.5,color='blue') ax.set_title('Mean Profile of ' + g.upper(), fontsize=14) ax.set_ylabel('Altitude [km]', fontsize=14) ax.set_xlabel('VMR [ppb$_v$]', fontsize=14) ax.grid(True, which='both') ax.tick_params(labelsize=14) ax.legend(prop={'size': 12}) ax.set_ylim(0, 20) ax.set_xlim(0, 200) if saveFlg: pdfsav.savefig(fig, dpi=200) #plt.savefig(pltDir+'Time_Series_all.pdf', bbox_inches='tight') else: plt.show(block=False) #--------------------------------- # Plot : Monthly averages of total columns #--------------------------------- fig, ax = plt.subplots(1, figsize=(10, 6), sharex=True) ax.plot(DatesFTS_mnth, vmrPFTS_mnth, color='k', label='FTIR') ax.scatter(DatesFTS_mnth, vmrPFTS_mnth, facecolors='white', s=60, color='k') ax.fill_between(DatesFTS_mnth, vmrPFTS_mnth - vmrPFTS_STDmnth, vmrPFTS_mnth + vmrPFTS_STDmnth, alpha=0.25, zorder=1) ax.plot(DatesCAM_mnth, vmrPCAM_mnth, color='red', linewidth=3.0, label='CAM-Chem') #ax.scatter(DatesCAM, vmrPCAM, facecolors='white', s=60, color='red') ax.plot(DatesCAM_mnth, vmrPCAM_smt_mnth, color='blue', linewidth=3.0, label='CAM-Chem smoothed') #ax.scatter(DatesCAM, vmrPCAM_smt, facecolors='white', s=60, color='blue') ax.grid(True) #ax.set_ylabel('Partial Column\n[molecules$\cdot$cm$^{-2}$]',fontsize=14) ax.set_ylabel('Weighted VMR [ppb]', fontsize=14) ax.set_title(g.upper() + ' Partial Column [Monthly average], ' + str(altFTS[inds[-1]]) + '[km] - ' + str(altFTS[inds[0]]) + '[km]', multialignment='center', fontsize=14) ax.tick_params(labelsize=14) ax.set_xlabel('Year', fontsize=14) ax.xaxis.set_minor_locator(monthLc) ax.xaxis.set_major_formatter(DateFmt) ax.legend(prop={'size': 12}) #ax.set_xlim(xmin, xmax) if saveFlg: pdfsav.savefig(fig, dpi=200) #plt.savefig(pltDir+'Time_Series_all.pdf', bbox_inches='tight') else: plt.show(block=False) if saveFlg: pdfsav.close() else: user_input = raw_input('Press any key to exit >>> ') exit()
def render_plots(to_show, to_date=None, dt='*', size=5, dpi=200, image_path=None): if to_date: end, _ = raw_vaccination_data(to_date) end.set_index([area_name, date_col], inplace=True) else: end = None indexed, data_dt = raw_vaccination_data(dt) indexed.set_index([area_name, date_col], inplace=True) rows, cols = to_show.shape fig, axes = plt.subplots(rows, cols, figsize=(size * cols, size * rows), gridspec_kw={'hspace': 0.4}, dpi=dpi) fig.set_facecolor('white') for r in range(rows): for c in range(cols): name, type_ = to_show[r, c] ax = axes[r, c] if isinstance(axes, np.ndarray) else axes area = indexed.loc[name].sort_index() by_publish = f'cumPeopleVaccinated{type_}DoseByPublishDate' area[by_publish].plot( ax=ax, drawstyle="steps-post", label='Total by Report Date', color='red', title=f'{name} - {type_} Dose as reported {data_dt:%d %b}') if end is not None: area_end = end.loc[name] ax.set_xlim( area_end.index.min() - timedelta(days=1), area_end.index.max() + timedelta(days=1), ) ax.set_ylim(0, area_end[by_publish].max() * 1.02) for ax in axes.flat if isinstance(axes, np.ndarray) else [axes]: ax.legend(loc='upper left') _, ymax = ax.get_ylim() xaxis = ax.axes.get_xaxis() yaxis = ax.axes.get_yaxis() if ymax > 1_000_000: yaxis.set_major_formatter( FuncFormatter(lambda y, pos: f"{y / 1_000_000:.1f}m")) elif ymax > 1_000: yaxis.set_major_formatter( FuncFormatter(lambda y, pos: f"{y / 1_000:.0f}k")) xaxis.set_major_locator(WeekdayLocator(6)) xaxis.set_major_formatter(DateFormatter('%d %b')) plt.setp(xaxis.get_majorticklabels(), rotation=-90, horizontalalignment='center') xaxis.label.set_visible(False) if image_path: plt.savefig(image_path / f'{data_dt}.png', bbox_inches='tight') plt.close()
def finance(data, parameters, output): #data # (Year, month, day) tuples suffice as args for quotes_historical_yahoo date1 = (2016, 8, 1) date2 = (2016, 9, 9) sticker = '' with open(data) as f: f_csv = csv.reader(f) headers = next(f_csv) for row in f_csv: sticker = row[0] date1 = literal_eval(row[1]) date2 = literal_eval(row[2]) #parameters param = "" figsize = (8, 6) param1 = "" if 'figsize' in parameters.keys(): figsize = eval(parameters['figsize']) fig, ax = plt.subplots(figsize=(11, 5)) if 'title' in parameters.keys(): ax.set_title(parameters['title']) if 'witdth' in parameters.keys(): param = param + ",width=" + str(parameters['width']) if 'colorup' in parameters.keys(): param = param + ",colorup='" + parameters['colorup'] + "'" if 'colordown' in parameters.keys(): param = param + ",colordown='" + parameters['colordown'] + "'" if 'rotation' in parameters.keys(): param1 = param1 + ",rotation=" + str(parameters['rotation']) if 'horizontalalignment' in parameters.keys(): param1 = param1 + ",horizontalalignment='" + parameters[ 'horizontalalignment'] + "'" mondays = WeekdayLocator(MONDAY) # major ticks on the mondays alldays = DayLocator() # minor ticks on the days weekFormatter = DateFormatter('%b %d') # e.g., Jan 12 dayFormatter = DateFormatter('%d') # e.g., 12 quotes = quotes_historical_yahoo_ohlc(sticker, date1, date2) if len(quotes) == 0: raise SystemExit fig.subplots_adjust(bottom=0.2) ax.xaxis.set_major_locator(mondays) ax.xaxis.set_minor_locator(alldays) ax.xaxis.set_major_formatter(weekFormatter) #ax.xaxis.set_minor_formatter(dayFormatter) #plot_day_summary(ax, quotes, ticksize=3) exec("candlestick_ohlc(ax, quotes, width=0.6" + param + ")") ax.xaxis_date() ax.autoscale_view() exec("plt.setp(plt.gca().get_xticklabels()" + param1 + ")") # adding horizontal grid lines plt.grid(True, linestyle='--', linewidth=0.5) savefig(output, format='svg')
from mpl_finance import candlestick_ohlc import matplotlib.dates as mdates from matplotlib.dates import MO, TU, WE, TH, FR, DateFormatter, WeekdayLocator, YearLocator import matplotlib.ticker as mticker from dateutil.rrule import DAILY, rrule, MO, TU, WE, TH, FR def daterange(start_date, end_date): return rrule(DAILY, dtstart=start_date, until=end_date, byweekday=(MO, TU, WE, TH, FR)) mondays = YearLocator(1) # major ticks on the mondays alldays = WeekdayLocator(byweekday=MO, interval=1) # minor ticks on the days weekFormatter = DateFormatter('%m/%d/%y') # e.g., Jan 12 dayFormatter = DateFormatter('%I:%M%p') # e.g., 12 file_name = "MSFT.csv" """ winter = 1 spring = 2 summer = 3 autumn = 4 """ DummyYear = 2000 # StockOverFlow :) im laxy seasons = [(1, (date(DummyYear, 1, 1), date(DummyYear, 3, 20))), (2, (date(DummyYear, 3, 21), date(DummyYear, 6, 20))), (3, (date(DummyYear, 6, 21), date(DummyYear, 9, 22))), (4, (date(DummyYear, 9, 23), date(DummyYear, 12, 20))),
print 'Total spending: {0:.1f}'.format(cum_values[-1]) print 'Total budget: {0:.1f}'.format(cum_budget[-1]) # plot fig = plt.figure() fig.suptitle('Save to thrive', fontsize=20) ax = fig.add_subplot(111) # ax.plot(total_keys, total_values, 'g-') ax.bar(total_keys, total_values, edgecolor='none', align='center', color='green') avg = [np.mean(total_values[:i+1]) for i in xrange(len(total_values))] ax.plot(total_keys, avg, 'g-o', markeredgecolor='none', markerfacecolor='white', linewidth=2) for tl in ax.get_yticklabels(): tl.set_color('green') # fig.autofmt_xdate() ax.xaxis.set_major_locator(WeekdayLocator(SATURDAY)) # major ticks on Saturdays ax.xaxis.set_minor_locator(DayLocator()) # minor ticks on every days week_formatter = DateFormatter('%y %b %d') ax.xaxis.set_major_formatter(week_formatter) ax.grid(True) ax.xaxis_date() ax.autoscale_view() plt.setp(ax.get_xticklabels(), rotation=45, horizontalalignment='right') ax2 = ax.twinx() ax2.plot(total_keys, cum_values, 'r-', linewidth=2) for tl in ax2.get_yticklabels(): tl.set_color('red') ax2.plot(total_keys, cum_budget, 'r--', linewidth=2) ax2.xaxis.set_major_locator(WeekdayLocator(SATURDAY)) # major ticks on Saturdays ax2.xaxis.set_minor_locator(DayLocator()) # minor ticks on every days
# Extraemos las cotizaciones para las fechas siguientes fecha1 = (2014, 10, 13) fecha2 = (2014, 11, 13) # Vamos a la web y nos traemos la información de la cotización valores = quotes_historical_yahoo_ohlc('AAPL', fecha1, fecha2) if len(valores) == 0: raise SystemExit # Definimos el aspecto de la gráfica fig, ax = plt.subplots() fig.subplots_adjust(bottom=0.2) # Ponemos marcas mayores para los lunes lunes = WeekdayLocator(MONDAY) ax.xaxis.set_major_locator(lunes) # Ponemos marcas menores para el resto de días resto_dias = DayLocator() ax.xaxis.set_minor_locator(resto_dias) # Damos formato a los días formato_semana = DateFormatter('%b %d') # e.g., Ene 12 ax.xaxis.set_major_formatter(formato_semana) ax.xaxis_date() candlestick_ohlc(ax, valores, width=0.6) ax.autoscale_view() plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')
async def plot_karma(karma_dict: Dict[str, List[KarmaChange]]) -> (str, str): # Error if there's no input data if len(karma_dict) == 0: return "", "" # Matplotlib preamble plt.clf() plt.rcParams.update({"figure.autolayout": True}) fig, ax = plt.subplots(figsize=(8, 6)) # Get the earliest and latest karma values fo earliest_karma = utc.localize(datetime.utcnow()).astimezone( timezone("Europe/London")) latest_karma = utc.localize(datetime(1970, 1, 1)).astimezone( timezone("Europe/London")) for key, changes in karma_dict.items(): earliest_karma = (changes[0].local_time if changes[0].local_time < earliest_karma else earliest_karma) latest_karma = (changes[-1].local_time if changes[-1].local_time >= latest_karma else latest_karma) karma_timeline = latest_karma - earliest_karma # Determine the right graph tick positioning if karma_timeline <= timedelta(hours=1): date_format = DateFormatter("%H:%M %d %b %Y") date_locator_major = MinuteLocator(interval=15) date_locator_minor = MinuteLocator() elif karma_timeline <= timedelta(hours=6): date_format = DateFormatter("%H:%M %d %b %Y") date_locator_major = HourLocator() date_locator_minor = MinuteLocator(interval=15) elif karma_timeline <= timedelta(days=14): date_format = DateFormatter("%d %b %Y") date_locator_major = DayLocator() date_locator_minor = HourLocator(interval=6) elif karma_timeline <= timedelta(days=30): date_format = DateFormatter("%d %b %Y") date_locator_major = WeekdayLocator() date_locator_minor = DayLocator() elif karma_timeline <= timedelta(days=365): date_format = DateFormatter("%B %Y") date_locator_major = MonthLocator() date_locator_minor = WeekdayLocator(interval=2) else: date_format = DateFormatter("%Y") date_locator_major = YearLocator() date_locator_minor = MonthLocator() # Transform the karma changes into plottable values for karma, changes in karma_dict.items(): scores = [k.score for k in changes] time = [] time = date2num([k.local_time for k in changes]) # Plot the values ax.xaxis.set_major_locator(date_locator_major) ax.xaxis.set_minor_locator(date_locator_minor) ax.xaxis.set_major_formatter(date_format) ax.grid(b=True, which="minor", color="0.9", linestyle=":") ax.grid(b=True, which="major", color="0.5", linestyle="--") ax.set( xlabel="Time", ylabel="Karma", xlim=[ time[0] - ((time[-1] - time[0]) * 0.05), time[-1] + ((time[-1] - time[0]) * 0.05), ], ) (line, ) = ax.plot_date(time, scores, "-", xdate=True) line.set_label(karma) # Create a legend if more than 1 line and format the dates if len(karma_dict.keys()) > 1: ax.legend() fig.autofmt_xdate() # Save the file to disk and set the right permissions filename = ("".join(karma_dict.keys()) + "-" + str(hex(int(datetime.utcnow().timestamp()))).lstrip("0x") + ".png").replace(" ", "") path = CONFIG.FIG_SAVE_PATH / filename fig.savefig(path, dpi=240, transparent=False) os.chmod(path, 0o644) return filename, path
# Using converters parameter, convert the string dates into Python datetime objects yen_data = pd.read_csv( 'http://research.stlouisfed.org/fred2/data/EXJPUS.txt', skiprows=28, index_col=0, delim_whitespace=True, converters={0: lambda x: datetime.strptime(x, "%Y-%m-%d")}) ############## Creation of Top Chart ########### fig, axes = plt.subplots( 2, 1) # Create a grid of 2 rows and 1 column of charts (2 charts total) # Let's define x-axis date interval sizes: year, month, week, or day year = YearLocator() month = MonthLocator(bymonth=range(1, 13), bymonthday=1, interval=1) week = WeekdayLocator(byweekday=dateutil.rrule.MO) # Every MOnday day = DayLocator(bymonthday=range(1, 32), interval=1) axes[0] = fig.add_axes([0, 1.3, 1.5, 1]) # left, bottom, width, height # Let's define x-axis formatting axes[0].xaxis.set_major_locator(year) axes[0].xaxis.grid(which='major') # Let's define y-axis formatting y_major_ticks = MultipleLocator(50) axes[0].yaxis.set_major_locator(y_major_ticks) axes[0].yaxis.grid(which='major') # Now plot the data... axes[0].plot_date(yen_data.index, yen_data.values, 'r')
def pandas_candlestick_ohlc(dat, days='day', adj=False, otherseries=None): mondays = WeekdayLocator(MONDAY) all_days = DayLocator() fields = ['Open', 'High', 'Low', 'Close'] transdat = dat.loc[:, fields] # Selects single column or subset of columns by label transdat.columns = pd.Index(['Open', 'High', 'Low', 'Close']) if (type(days) == str): if days == "day": plotdat = transdat days = 1 elif days in ['week', 'month', 'year']: if days == 'week': transdat['week'] = pd.to_datetime(transdat.index).map( lambda x: x.isocalender()[1] ) #Return a 3-tuple, (ISO year, ISO week number, ISO weekday). elif days == 'month': transdat['month'] = pd.to_datetime( transdat.index).map(lambda x: x.month) transdat['year'] = pd.to_datetime( transdat.index).map(lambda x: x.isocalendar()[0]) grouped = transdat.groupby(list({'year', days})) plotdat = pd.DataFrame({ "Open": [], "High": [], "Low": [], "Close": [] }) for name, group in grouped: plotdat = plotdat.append( pd.DataFrame( { 'Open': group.iloc[0, 0], 'High': max(group.High), 'Low': min(group.Low), 'Close': group.iloc[-1, 3] }, index=[group.index[0]])) if days == 'week': days = 5 elif days == 'month': days = 30 elif days == 'year': days = 365 elif (type(days) == int and days >= 1): transdat['stick'] = [ pd.np.floor(i / days) for i in range(len(transdat.index)) ] grouped = transdat.groupby('stick') plotdat = pd.DataFrame({ "Open": [], "High": [], "Low": [], "Close": [] }) for name, group in grouped: plotdat = plotdat.append( pd.DataFrame( { 'Open': group.iloc[0, 0], 'High': max(group.High), 'Low': min(group.Low), 'Close': group.iloc[-1, 3] }, index=[group.index[0]])) else: raise ValueError( 'Valid inputs - "days" include the strings or a positive integer') # Set plot parameters, including the axis object ax used for plotting fig, ax = plt.subplots() fig.subplots_adjust(bottom=0.2) if plotdat.index[-1] - plotdat.index[0] < pd.Timedelta('730 days'): weekFormatter = DateFormatter('%b %d') # for example Jan 12 ax.xaxis.set_major_locator(mondays) ax.xaxis.set_minor_locator(all_days) else: weekFormatter = DateFormatter('%b %d, %Y') ax.xaxis.set_major_formatter(weekFormatter) ax.grid(True) # Create the candelstick chart candlestick_ohlc(ax, list( zip(list(date2num(plotdat.index.tolist())), plotdat["Open"].tolist(), plotdat["High"].tolist(), plotdat["Low"].tolist(), plotdat["Close"].tolist())), colorup="black", colordown="red", width=days * .4) # Plot other series (such as moving averages) as lines if otherseries != None: if type(otherseries) != list: otherseries = [otherseries] dat.loc[:, otherseries].plot(ax=ax, lw=1.3, grid=True) ax.xaxis_date() ax.autoscale_view() plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right') plt.show()
def drawPic(df, code, name): mondays = WeekdayLocator(MONDAY) # 主要刻度 alldays = DayLocator() # 次要刻度 # weekFormatter = DateFormatter('%b %d') # 如:Jan 12 mondayFormatter = DateFormatter('%m-%d-%Y') # 如:2-29-2015 dayFormatter = DateFormatter('%d') # 如:12 xx = [] t = 0 for DAT, DD in df.iterrows(): t = t + 10 xx.append(t) # -------------------------------------------------------------------- plt.figure(4) # 创建图表2 ax1 = plt.subplot(411) # 在图表2中创建子图1 ax2 = plt.subplot(412) # 在图表2中创建子图2 ax3 = plt.subplot(413) # 在图表2中创建子图2 ax4 = plt.subplot(414) # 在图表2中创建子图2 # --------------------------------------------(子图表1) plt.sca(ax1) ax1.xaxis.set_major_locator(mondays) ax1.xaxis.set_minor_locator(alldays) ax1.xaxis.set_major_formatter(mondayFormatter) _candlestick(ax1, df, width=0.6, colorup='r', colordown='g') ax1.xaxis_date() plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right') """ plt.plot( xx, df['ma5'], linewidth = 0.5 ) plt.plot( xx, df['ma10'], linewidth = 0.5 ) plt.plot( xx, df['ma20'], linewidth = 0.5 ) plt.plot( xx, df['ma30'], linewidth = 0.5 ) plt.plot( xx, df['ma40'], linewidth = 0.5 ) plt.plot( xx, df['ma60'], linewidth = 0.5 ) plt.plot( xx, df['ma89'], linewidth = 0.5 ) plt.plot( xx, df['ma144'], linewidth = 0.5 ) plt.plot( xx, df['ma233'], linewidth = 0.5 ) plt.plot( xx, df['ma377'], linewidth = 0.5 ) plt.plot( xx, df['ma610'], linewidth = 0.5 ) """ # ax.grid(True) # --------------------------------------------(子图表2) plt.sca(ax2) # plt.plot( xx[len(xx)-500:len(xx)], df['j1'].tail(500), linewidth = 0.5 ) plt.plot(xx, df['j1'], linewidth=0.5) plt.plot(xx, df['e1'], linewidth=0.5) plt.plot(xx, df['j2'], linewidth=0.5) plt.plot(xx, df['e2'], linewidth=0.5) plt.plot(xx, df['j3'], linewidth=0.5) plt.plot(xx, df['e3'], linewidth=0.5) plt.plot(xx, df['j4'], linewidth=0.5) plt.plot(xx, df['e4'], linewidth=0.5) plt.sca(ax3) plt.plot(xx, df['x1'], linewidth=0.5) plt.plot(xx, df['x2'] * 7, linewidth=0.5) plt.plot(xx, df['x3'] * 30, linewidth=0.5) plt.plot(xx, df['x4'] * 80, linewidth=0.5) plt.sca(ax4) plt.plot(xx, df['gain'], linewidth=0.5) plt.show()
def init_plot(x_vals, today=None): fig, ax = plt.subplots(1) plt.ylabel("index rizika") min_x = min(x_vals) max_x = max(x_vals) if today is None: today = max_x if isinstance(min_x, date): # format only for dates plt.xlabel("datum") interval = math.ceil((max_x - min_x).days / (13 * 7)) locator = WeekdayLocator(byweekday=WE, interval=interval) formatter = AutoDateFormatter(locator) minor_locator = WeekdayLocator(byweekday=WE) ax.xaxis.set_major_locator(locator) ax.xaxis.set_major_formatter(formatter) ax.xaxis.set_minor_locator(minor_locator) ax.set_xlim(min_x, max_x) ax.grid(which='major', axis='x', linestyle=':', lw=.5) ax.set_ylim(0, 100) ax.margins(0) ax.set_yticks([0, 20, 40, 60, 75, 100]) colors = [ "black", "forestgreen", "gold", "darkorange", "crimson", "indigo" ] for ytick, color in zip(ax.get_yticklabels(), colors): plt.setp(ytick, color=color) ax.set_yticks([10, 30, 50, 70, 80, 90], minor=True) # PES levels # ax.axhline(100, color='indigo', linestyle='--') ax.axhline(75, color='crimson', linestyle='--') ax.axhline(60, color='darkorange', linestyle='--') ax.axhline(40, color='gold', linestyle='--') ax.axhline(20, color='forestgreen', linestyle='--') fig.autofmt_xdate() plt.setp(ax.get_xticklabels(), fontsize='small') fig.text(0.02, 0.02, "{:s}".format(SRC_LINK), fontsize='xx-small', color='gray') fig.text(0.73, 0.95, "{:s}".format('aportováno: '), fontsize='small', color='gray') fig.text(0.85, 0.95, "{:s}".format(today.strftime("%d.%m.%Y")), fontsize='small', fontweight='bold', color='gray') fig.text(0.95, 0.02, "CC0", fontsize='small', color='gray') return fig, ax
def main(data): x = data.dates y = data.price X, Y = linear_regression(x, y)# for linear regression uncomment this line p, q, r = higher_regression(x, y, 2)# for quadratic regression uncomment this line a, b, c, d = higher_regression(x, y, 3)# for cubic regression uncomment this line print '\nRegression Model for price prediction of %s' % (str(data).replace("dataset.",'')) print '\nRegression Model::\n\t a = %f\tb = %f' % (X, Y) print 'Regression Equation::\n\t predicted price = %f * date + (%f)' % (X, Y) print loc = WeekdayLocator(byweekday = SU, interval=1) formatter = DateFormatter('%y-%m-%d') date1 = datetime.date( 2013, 4, 1 ) date2 = datetime.date( 2015, 3, 1 ) delta = datetime.timedelta(days=7) dates = drange(date1, date2, delta) price1 = [] price2 = [] price3 = [] for date in dates: price_calc1 = X*date + Y # for linear regression uncomment this line price_calc2 = p + q*date + r*date*date # for quadratic regression uncomment this line price_calc3 = a + b*date + c*date*date + d*date*date*date # for cubic regression uncomment this line price1.append(price_calc1) price2.append(price_calc2) price3.append(price_calc3) Title = "Graph plot of %s\nIndex\n<original data black> <linear green>\n<quadratic blue> <cubic red>" % (str(data).replace("dataset.",'')) fig, ax = plt.subplots() ax.set_title(Title) plt.plot_date(dates, price1, '-', color='green', label='Linear Regression Fit Line', markersize=10) plt.plot_date(dates, price2, '-', color='blue', label='Quardatic Regression Fit Line', markersize=10) plt.plot_date(dates, price3, '-', color='red', label='Cubic Regression Fit Line', markersize=10) plt.plot_date(x, y, '.', color='black', markersize=10) ax.xaxis.set_major_locator(loc) ax.xaxis.set_major_formatter(formatter) ax.xaxis.set_label("Date") ax.yaxis.set_label("Price") labels = ax.get_xticklabels() plt.setp(labels, rotation=45, fontsize=12) qstn = raw_input("Enter date? (y/n):: ") if qstn == 'y': yr = int(raw_input("Enter Date (YYYY):: ")) mnt = int(raw_input("Enter Date (MM):: ")) day = int(raw_input("Enter Date (DD):: ")) date_x = date2num(datetime.date(yr, mnt, day)) price_x1 = X*date_x + Y # for linear regression uncomment this line price_x2 = p + q*date_x + r*date_x*date_x # for quadratic regression uncomment this line price_x3 = a + b*date_x + c*date_x*date_x + d*date_x*date_x*date_x # for cubic regression uncomment this line avg_price = (price_x1 + price_x2 + price_x3) / 3 print "\nLinear Regression predicted price of %s on %s is %f.\n" % (str(data).replace("dataset.",''), str(num2date(date_x))[:10], price_x1) print "\nQuardatic Regression predicted price of %s on %s is %f.\n" % (str(data).replace("dataset.",''), str(num2date(date_x))[:10], price_x2) print "\nCubic Regression predicted price of %s on %s is %f.\n" % (str(data).replace("dataset.",''), str(num2date(date_x))[:10], price_x3) print "\nAverage predicted price of %s on %s is %f.\n" % (str(data).replace("dataset.",''), str(num2date(date_x))[:10], avg_price) plt.plot_date(date_x, price_x1, 'o', color='green', markersize=10) plt.plot_date(date_x, price_x2, 'o', color='blue', markersize=10) plt.plot_date(date_x, price_x3, 'o', color='red', markersize=10) plt.plot_date(date_x, avg_price, '*', color='violet', markersize=10) plt.show() else: plt.show()
def pandas_candlestick_ohlc(self, share, fechasNum, window, stick="day", otherseries=["20d", "50d"], adj=False): """ :param share: pandas DataFrame object with datetime64 index, and float columns "Open", "High", "Low", and "Close", likely created via DataReader from "yahoo" :param stick: A string or number indicating the period of time covered by a single candlestick. Valid string inputs include "day", "week", "month", and "year", ("day" default), and any numeric input indicates the number of trading days included in a period :param adj: A boolean indicating whether to use adjusted prices :param otherseries: An iterable that will be coerced into a list, containing the columns of share that hold other series to be plotted as lines This will show a Japanese candlestick plot for stock data stored in share, also plotting other series if passed. """ share["20d"] = np.round( share["Adj. Close"].rolling(window=20, center=False).mean(), 2) share["50d"] = np.round( share["Adj. Close"].rolling(window=50, center=False).mean(), 2) mondays = WeekdayLocator(MONDAY) # major ticks on the mondays alldays = DayLocator() # minor ticks on the days dayFormatter = DateFormatter('%d') # e.g., 12 # Create a new DataFrame which includes OHLC data for each period specified by stick input fields = ["Open", "High", "Low", "Close"] if adj: fields = ["Adj. " + s for s in fields] transdat = share.loc[:, fields] transdat.columns = pd.Index(["Open", "High", "Low", "Close"]) if (type(stick) == str): if stick == "day": plotdat = transdat stick = 1 # Used for plotting elif stick in ["week", "month", "year"]: if stick == "week": transdat["week"] = pd.to_datetime(transdat.index).map( lambda x: x.isocalendar()[1]) # Identify weeks elif stick == "month": transdat["month"] = pd.to_datetime(transdat.index).map( lambda x: x.month) # Identify months transdat["year"] = pd.to_datetime(transdat.index).map( lambda x: x.isocalendar()[0]) # Identify years grouped = transdat.groupby(list(set( ["year", stick]))) # Group by year and other appropriate variable plotdat = pd.DataFrame({ "Open": [], "High": [], "Low": [], "Close": [] }) # Create empty data frame containing what will be plotted for name, group in grouped: plotdat = plotdat.append( pd.DataFrame( { "Open": group.iloc[0, 0], "High": max(group.High), "Low": min(group.Low), "Close": group.iloc[-1, 3] }, index=[group.index[0]])) if stick == "week": stick = 5 elif stick == "month": stick = 30 elif stick == "year": stick = 365 elif (type(stick) == int and stick >= 1): transdat["stick"] = [ np.floor(i / stick) for i in range(len(transdat.index)) ] grouped = transdat.groupby("stick") plotdat = pd.DataFrame({ "Open": [], "High": [], "Low": [], "Close": [] }) # Create empty data frame containing what will be plotted for name, group in grouped: plotdat = plotdat.append( pd.DataFrame( { "Open": group.iloc[0, 0], "High": max(group.High), "Low": min(group.Low), "Close": group.iloc[-1, 3] }, index=[group.index[0]])) else: raise ValueError( 'Valid inputs to argument "stick" include the strings "day", "week", "month", "year", or a positive integer' ) # Set plot parameters, including the axis object ax used for plotting fig = Figure(figsize=(7.5, 4.5), dpi=100) self.ax = fig.add_subplot(111) self.ax.clear() self.ax.grid(True) # Create the candelstick chart candlestick_ohlc(self.ax, list( zip(list(date2num(plotdat.index.tolist())), plotdat["Open"].tolist(), plotdat["High"].tolist(), plotdat["Low"].tolist(), plotdat["Close"].tolist())), colorup="black", colordown="red", width=stick * .4) # Plot other series (such as moving averages) as lines if otherseries != None: if type(otherseries) != list: otherseries = [otherseries] share.loc[:, otherseries].plot(ax=self.ax, lw=1.3, grid=True) self.ax.xaxis_date() self.ax.autoscale_view() plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right') # estadis = Estadisticas() # desviEstandar = estadis.desviacionEstadar(share["Adj. Close"].data.obj) # media = estadis.regresionLineal(share,fechasNum) # mediaMasDE = estadis.cambiarValorPrecios(media.copy(),desviEstandar*2) # mediaMenosDE = estadis.cambiarValorPrecios(media.copy(),-desviEstandar*2) # ax.plot(plotdat.index.tolist(),media,"r",label = "regression") # ax.plot(plotdat.index.tolist(),mediaMasDE,"b",label = "+DE") # ax.plot(plotdat.index.tolist(),mediaMenosDE,"b",label = "-DE") #navigation tool if self.grafico: self.canvas.get_tk_widget().destroy() if ~self.grafico: self.canvas = FigureCanvasTkAgg(fig, master=window) self.canvas.get_tk_widget().pack() self.toolbar = NavigationToolbar2Tk(self.canvas, window) self.canvas._tkcanvas.pack(side=TOP, fill=BOTH, expand=True) self.canvas.draw() self.grafico = True
def rain_ax_format(ax, dates, rain_explanation, snow, dsize=78, dstring=(2, 0, 45)): """Set up the rain ax.""" ax.set_xlim(dates[0], dates[-1]) ax.set_ylim(-0.5, 2.5) ax.set_yticks([]) # ax.xaxis.grid(alpha=0.2) # ax.xaxis.set_major_locator(WeekdayLocator(byweekday=range(7))) # ax.set_xticklabels([]) ax.xaxis.set_minor_locator(HourLocator(np.arange(0, 25, 6))) # minor ax.xaxis.set_major_locator(WeekdayLocator(byweekday=range(5))) ax.get_xaxis().set_tick_params(which="minor", direction="in") ax.get_xaxis().set_tick_params(which="major", direction="in") ax.set_xticklabels([]) bg_rect = Rectangle( (0.84, 0), 0.16, 1.0, linewidth=0.3, edgecolor="k", facecolor="w", transform=ax.transAxes, zorder=2, ) ax.add_patch(bg_rect) # distinguish between rain and snow if np.all(snow): # only snowfall ax.scatter( [0.917, 0.917], [0.25, 0.5], dsize * 0.6, color=rain_explanation, transform=ax.transAxes, marker=(6, 2, 0), zorder=3, ) ax.text( 0.992, 0.70, "snowfall", fontsize=8, fontweight="bold", transform=ax.transAxes, ha="right", ) ax.text(0.992, 0.45, "very likely", fontsize=8, transform=ax.transAxes, ha="right") ax.text(0.992, 0.2, "less likely", fontsize=8, transform=ax.transAxes, ha="right") elif np.all(~snow): # only rainfall ax.scatter( [0.917, 0.917], [0.25, 0.5], dsize, color=rain_explanation, transform=ax.transAxes, marker=droplet(rot=-30), zorder=3, ) ax.text( 0.992, 0.70, "rainfall", fontsize=8, fontweight="bold", transform=ax.transAxes, ha="right", ) ax.text(0.992, 0.45, "very likely", fontsize=8, transform=ax.transAxes, ha="right") ax.text(0.992, 0.2, " less likely", fontsize=8, transform=ax.transAxes, ha="right") else: # mix of snow and rain ax.scatter( [0.919, 0.919], [0.16, 0.41], dsize, color=rain_explanation, transform=ax.transAxes, marker=droplet(rot=-30), zorder=3, ) ax.scatter( [0.914, 0.914], [0.24, 0.49], dsize * 0.42, color=rain_explanation, transform=ax.transAxes, marker=(6, 2, 0), zorder=3, ) ax.text( 0.992, 0.61, "snow or\n rainfall", fontsize=8, fontweight="bold", transform=ax.transAxes, ha="right", ) ax.text(0.992, 0.4, "very likely", fontsize=8, transform=ax.transAxes, ha="right") ax.text(0.992, 0.15, " less likely", fontsize=8, transform=ax.transAxes, ha="right") ax.text(0.84, 0.75, "\u25B8 heavy", fontsize=8, transform=ax.transAxes, ha="left") ax.text(0.84, 0.43, "\u25B8 medium", fontsize=8, transform=ax.transAxes, ha="left") ax.text(0.84, 0.11, "\u25B8 light", fontsize=8, transform=ax.transAxes, ha="left")
def weeklyplot(path): d = os.path.join(path, 'wifi') if not os.path.exists(d): os.makedirs(d) fname = os.path.join(d, 'weeklyconxn.pdf') pp = PdfPages(fname) c = 0 #count for figures for root, dirs, files in os.walk(path): filelist = [] dict_ = defaultdict(list) for name in files: filelist.append(os.path.join(root, name)) filelist.sort(key=os.path.getmtime) for filename in filelist: try: log = open(filename, 'r') except IOError: print 'File doesnot exit' break for line in log: data = line.split() n = len(data) if n < 10 or data[0].startswith('01') or data[0].startswith( '12'): # print line + filename continue else: tag = data[5] if tag.startswith('PhoneLab-WiFiReceiver' ) and data[8].startswith('BSSID'): newdate = data[0] + '-12 ' + data[1] t = datetime.strptime(newdate, '%m-%d-%y %H:%M:%S.%f') dict_[t.date()].append(data[9]) log.close() c = c + 1 device = root.split( '/') #to get name of the device from the given path if len(dict_) > 0: num = 10 x = [] y = [] for item in dict_: current = list(dict_[item]) bssid_count = 0 for i in xrange(0, len(current)): if i == 0 or current[i] != current[i - 1]: bssid_count += 1 if len(x) == 0 or item > x[-1]: x.append(item) y.append(bssid_count) elif item < x[0]: x.insert(0, item) y.insert(0, bssid_count) else: for n in xrange(len(x) - 1, 1): if item < x[n] and item > x[n - 1]: x.insert(n, item) y.insert(n, bssid_count) break flag = 0 col = random() for i in xrange(0, len(x)): if x[i] - x[flag] >= timedelta(days=6): num += 2 fig1 = figure(c, dpi=10) ax = fig1.add_subplot(111) m = i + 1 ax.bar(x[flag:m], y[flag:m], width=.35, color=cmap2(col)) title('From %s-%s -- %s' % (x[flag], x[i], device[-1])) xlabel('Time', fontsize=15) ylabel('Number of connections', fontsize=15) ax.xaxis.set_major_locator( WeekdayLocator(byweekday=(MO, TU, WE, TH, FR, SA))) ax.xaxis.set_major_formatter(DateFormatter('%A \n%d %b')) m = i + 1 flag = i + 1 pp.savefig(fig1) close() fig1.clear() pp.close()
formatters. See major_minor_demo1.py for more information on controlling major and minor ticks """ from __future__ import print_function import datetime from pylab import figure, show from matplotlib.dates import MONDAY, SATURDAY from matplotlib.finance import quotes_historical_yahoo from matplotlib.dates import MonthLocator, WeekdayLocator, DateFormatter date1 = datetime.date( 2002, 1, 5 ) date2 = datetime.date( 2003, 12, 1 ) # every monday mondays = WeekdayLocator(MONDAY) # every 3rd month months = MonthLocator(range(1,13), bymonthday=1, interval=3) monthsFmt = DateFormatter("%b '%y") quotes = quotes_historical_yahoo('INTC', date1, date2) if len(quotes) == 0: print ('Found no quotes') raise SystemExit dates = [q[0] for q in quotes] opens = [q[1] for q in quotes] fig = figure()
def plot_arrows( self, symbol: str, ax: Axes, start: datetime = None, end: datetime = None, ) -> None: pa: ProfitAttribution pas = sorted( [pa for pa in self.pas if pa.sym == symbol], key=lambda pa: pa.open_trade.time, ) ax.plot( [date2num(pa.buy_trade.time) for pa in pas], [pa.buy_trade.price for pa in pas], color="#0008", marker="2", lw=0, markersize=15, zorder=-1, ) ax.plot( [date2num(pa.sell_trade.time) for pa in pas], [pa.sell_trade.price for pa in pas], color="#0008", marker="1", lw=0, markersize=15, zorder=-1, ) if not pas: return max_qty = max([pa.qty for pa in pas]) for pa in pas: x0 = date2num(pa.open_trade.time) x1 = date2num(pa.close_trade.time) y0 = pa.open_trade.price y1 = pa.close_trade.price if pa.net_gain > 0: color = "green" else: color = "red" ax.plot( [x0, x1], [y0, y1], lw=(w := 0.5 + 2.5 * pa.qty / max_qty), zorder=1000 - w, color=color, ) ax.figure.autofmt_xdate() if start is None: lbx = min([pa.start_time for pa in pas]) - ONE_DAY else: lbx = start if end is None: ubx = max([pa.end_time for pa in pas]) + ONE_DAY else: ubx = end ax.set_xlim(lbx, ubx) lby = max(min([min(pa.buy_price, pa.sell_price) for pa in pas]) - 1, 0) uby = max([max(pa.buy_price, pa.sell_price) for pa in pas]) + 1 ax.set_ylim(lby, uby) ax.set_xlabel("Trade dt") ax.set_ylabel("Trade price") for day in date_range( lbx.replace(hour=0, minute=0, second=0), ubx, freq="1D" ): if day.weekday() in (5, 6): continue ax.axvspan( day.replace(hour=9, minute=30), day.replace(hour=16), 0, 1.0, facecolor="#0000ff20", zorder=-1e3, ) buys = np.array( [pa.buy_price for pa in pas for _ in range(abs(pa.qty))] ) sells = np.array( [pa.sell_price for pa in pas for _ in range(abs(pa.qty))] ) total_gain = (sells - np.minimum(sells, buys)).sum() total_loss = (sells - np.maximum(sells, buys)).sum() ax.set_title( f"{symbol} profits: {total_gain:.0f} - {-total_loss:.0f} = " f"{total_gain + total_loss:.0f}" ) ax.xaxis.set_major_locator(WeekdayLocator(byweekday=MO, tz=TZ_EASTERN)) ax.xaxis.set_minor_locator(DayLocator(tz=TZ_EASTERN)) ax.xaxis.set_minor_formatter(DateFormatter("%d")) ax.grid(color="#808080", lw=0.5) ax.grid(color="#808080", lw=0.25, axis="x", which="minor")