def start_analyse(df, countries, y_label, y_column): df = df[(df[y_column] > 0)] # suppress data with no cases (before begin of epidemy) X = {} converted_x = {} y = {} y_log = {} regressor = {} model = {} r_sq = {} x_pred = {} y_pred = {} coeff = {} daily_coeff = {} doubling_coeff = {} for country in countries: # Select data X[country] = df[df["Country/Region"] == country][["update"]].copy() converted_x[country] = X[country].copy() converted_x[country]["update"] = converted_x[country][ "update"].astype(int) # for backwards conversion to datetime: pd.to_datetime(X["update"].astype(int)) y[country] = df[df["Country/Region"] == country][y_column] y_log[country] = np.log( df[df["Country/Region"] == country][y_column]) # Create model regressor[country] = LinearRegression() model[country] = regressor[country].fit(converted_x[country], y_log[country]) r_sq[country] = model[country].score(converted_x[country], y_log[country]) coeff[country] = regressor[country].coef_[0] daily_coeff[country] = round( 10**(3600 * 24 * (10**9) * regressor[country].coef_[0]), 1) doubling_coeff[country] = 2 / daily_coeff[country] # Generate prediction: regressor.predict(converted_x) x_pred[country] = pd.DataFrame([ X[country].min(), X[country].min() + pd.Timedelta(1, unit='d'), X[country].min() + pd.Timedelta(2, unit='d'), X[country].min() + pd.Timedelta(3, unit='d'), X[country].min() + pd.Timedelta(4, unit='d'), X[country].min() + pd.Timedelta(5, unit='d'), X[country].min() + pd.Timedelta(6, unit='d'), X[country].min() + pd.Timedelta(7, unit='d'), X[country].max() + pd.Timedelta(14, unit='d') ]) y_pred[country] = np.exp(regressor[country].predict( x_pred[country].astype(int))) # Plot result fig, ax = plt.subplots(ncols=1, nrows=1) ax.set_yscale("log") # ax.set_ylim(1,10000) ax.grid(True, which="minor", axis="y", color='g', linestyle='--', linewidth=1) ax.grid(True, which="major", axis="y", color='g', linestyle='-', linewidth=2) ax.xaxis.set_minor_locator( dates.DayLocator(bymonthday=range(1, 32), interval=1)) ax.xaxis.set_minor_formatter(dates.DateFormatter('%d')) ax.xaxis.set_major_locator(dates.WeekdayLocator(byweekday=0)) ax.xaxis.set_major_formatter(dates.DateFormatter('\n%m-%d')) ax.grid(True, which="major", axis="x", color='g', linestyle='-', linewidth=2) fig.set_size_inches(20, 5) for country in countries: plot_df = pd.DataFrame(X[country]) plot_df["y"] = y[country] plot_df.plot(x="update", y="y", ax=ax, style="o:", label="act " + country) plot_df_pred = pd.DataFrame(x_pred[country]) plot_df_pred["y"] = y_pred[country] plot_df_pred.plot(x="update", y="y", ax=ax, style="--", label="pred " + country) plt.title('prediction') plt.xlabel('datetime') plt.ylabel(y_label) plt.show() print('Coefficient of determination:', r_sq) print("Evolution factor per day: {}".format(daily_coeff)) print("Doubling in day(s): {}".format(doubling_coeff)) x_pred[country]["pred"] = pd.DataFrame(y_pred[country]) x_pred[country]["prev"] = x_pred[country].apply( lambda row: x_pred[country][x_pred[country]["update"] < row[ "update"]]["update"].max(), axis=1) x_pred[country]["diff"] = x_pred[country].apply( lambda row: row["pred"] / x_pred[country][x_pred[country][ "update"] == row["prev"]]["pred"].max(), axis=1) return x_pred
def checkVisPA(ra, dec, targetName=None, ephFileName=pkg_resources.resource_filename('ExoCTK', 'data/contam_visibility/JWST_ephem_short.txt'), save=False, fig=''): if ra.find(':')>-1: #format is hh:mm:ss.s or dd:mm:ss.s ra = convert_ddmmss_to_float(ra) * 15. * D2R dec = convert_ddmmss_to_float(dec) * D2R else: #format is decimal ra = float(ra) * D2R dec = float(dec) * D2R #load ephemeris eclFlag = False eph = EPH.Ephemeris(ephFileName, eclFlag) #convert dates from MJD to Gregorian calendar dates mjd = np.array(eph.datelist) d = mdates.julian2num(mjd+2400000.5) gd = mdates.num2date(d) #loop through dates and determine VIS and PAs (nominal, min, max) vis = np.empty(mjd.size,dtype=bool) paNom, paMin, paMax = np.empty(mjd.size), np.empty(mjd.size), np.empty(mjd.size) for i in range(mjd.size): #is it visible? vis[i] = eph.in_FOR(mjd[i],ra,dec) #nominal PA at this date pa = eph.normal_pa(mjd[i],ra,dec) #search for minimum PA allowed by roll pa0 = pa while eph.is_valid(mjd[i],ra,dec,pa0-0.002): pa0 -= 0.002 #search for maximum PA allowed by roll pa1 = pa while eph.is_valid(mjd[i],ra,dec,pa1+0.002): pa1 += 0.002 paNom[i] = (pa*R2D)%360 paMin[i] = (pa0*R2D)%360 paMax[i] = (pa1*R2D)%360 #does PA go through 360 deg? wrap = np.any(np.abs(np.diff(paNom[np.where(vis)[0]])) > 350) #Determine good and bad PA ranges #Good PAs i, = np.where(vis) pa = np.concatenate((paNom[i],paMin[i],paMax[i])) if wrap: pa = np.append(pa,(0.,360.)) pa.sort() i1, = np.where(np.diff(pa)>10) i0 = np.insert(i1+1,0,0) i1 = np.append(i1,-1) paGood = np.dstack((pa[i0],pa[i1])).round(1).reshape(-1,2).tolist() #bad PAs (complement of the good PAs) paBad = [] if paGood[0][0]>0: paBad.append([0.,paGood[0][0]]) for i in range(1,len(paGood)): paBad.append([paGood[i-1][1],paGood[i][0]]) if paGood[-1][1]<360.: paBad.append([paGood[-1][1],360.]) #print results to file """ if save: fName='visibilityPA-'+targetName+'.txt' fic=open(fName,'w') fic.write('#Date MJD VIS? PAnom PArange\n') for i in range(vis.size): tmp1='{:7.3f}'.format(paNom[i]) if vis[i] else 7*'-' tmp2='{:7.3f}--{:7.3f}'.format(paMin[i],paMax[i]) if vis[i] else 16*'-' #fic.write(gd[i].strftime("%y-%m-%d")+' {:f} {:5s} {:7.3f} {:7.3f}--{:7.3f} \n'.format(mjd[i],str(vis[i]),paNom[i],paMin[i],paMax[i])) fic.write(gd[i].strftime("%y-%m-%d")+' {:f} {:5s} {} {} \n'.format(mjd[i],str(vis[i]),tmp1,tmp2)) fic.write("\n") fic.write("Accessible PA ranges: ") fic.write(','.join([str(x) for x in paGood])) fic.write("\n") fic.write("Non-accessible PA ranges: ") fic.write(','.join([str(x) for x in paBad])) fic.write("\n") fic.close() """ # Make a figure if not fig or fig==True: fig = plt.gcf() # Do all figure calculations iBad, = np.where(vis==False) paMasked = np.copy(paNom) paMasked[iBad] = np.nan gdMasked = np.copy(gd) i = np.argmax(paNom) if paNom[i+1]<10: i+=1 paMasked = np.insert(paMasked,i,np.nan) gdMasked = np.insert(gdMasked,i,gdMasked[i]) i = np.argmax(paMin) goUp = paMin[i-2]<paMin[i-1] #PA going up at wrap point? # Top part i0_top = 0 if goUp else i i1_top = i if goUp else paMin.size-1 paMaxTmp = np.copy(paMax) paMaxTmp[np.where(paMin>paMax)[0]] = 360 # Bottom part i = np.argmin(paMax) i0_bot = i if goUp else 0 i1_bot = paMin.size-1 if goUp else i paMinTmp = np.copy(paMin) paMinTmp[np.where(paMin>paMax)[0]] = 0 # Add fits to matplotlib if isinstance(fig, matplotlib.figure.Figure): # Make axes ax = plt.axes() plt.title(targetName) #plot nominal PA plt.plot(gdMasked,paMasked,color='k') #plot ranges allowed through roll if wrap: i = np.argmax(paMin) goUp = paMin[i-2]<paMin[i-1] #PA going up at wrap point? #top part plt.fill_between(gd[i0_top:i1_top+1],paMin[i0_top:i1_top+1],paMaxTmp[i0_top:i1_top+1],where=vis[i0_top:i1_top+1],lw=0,facecolor='k',alpha=0.5) #bottom part plt.fill_between(gd[i0_bot:i1_bot+1],paMinTmp[i0_bot:i1_bot+1],paMax[i0_bot:i1_bot+1],where=vis[i0_bot:i1_bot+1],lw=0,facecolor='k',alpha=0.5) else: plt.fill_between(gd,paMin,paMax,where=vis,lw=0,facecolor='k',alpha=0.5) plt.ylabel('Position Angle (degrees)') plt.xlim(min(gd),max(gd)) ax.xaxis.set_major_locator(mdates.MonthLocator()) ax.xaxis.set_major_formatter(mdates.DateFormatter("%b '%y")) ax.xaxis.set_minor_locator(mdates.DayLocator(list(range(1,32,5)))) plt.ylim(0,360) ax.yaxis.set_major_locator(MultipleLocator(25)) ax.yaxis.set_minor_locator(MultipleLocator(5)) plt.grid() for label in ax.get_xticklabels(): label.set_rotation(45) # Or to bokeh! else: # Convert datetime to a number for Bokeh gdMaskednum = [datetime.date(2019, 6, 1)+datetime.timedelta(days=n) for n,d in enumerate(gdMasked)] color = 'green' # Draw the curve and error fig.line(gdMaskednum, paMasked, legend='cutoff', line_color=color) # Top err_y = np.concatenate([paMin[i0_top:i1_top+1],paMaxTmp[i0_top:i1_top+1][::-1]]) # err_x = np.concatenate([[d.timestamp() for d in gd[i0_top:i1_top+1]],[d.timestamp() for d in gd[i0_top:i1_top+1]][::-1]]) err_x = np.concatenate([gdMaskednum[i0_top:i1_top+1],gdMaskednum[i0_top:i1_top+1][::-1]]) fig.patch(err_x, err_y, color=color, fill_alpha=0.2, line_alpha=0) # Bottom err_y = np.concatenate([paMinTmp[i0_bot:i1_bot+1],paMax[i0_bot:i1_bot+1][::-1]]) # err_x = np.concatenate([[d.timestamp() for d in gd[i0_bot:i1_bot+1]],[d.timestamp() for d in gd[i0_bot:i1_bot+1]][::-1]]) err_x = np.concatenate([gdMaskednum[i0_bot:i1_bot+1],gdMaskednum[i0_bot:i1_bot+1][::-1]]) fig.patch(err_x, err_y, color=color, fill_alpha=0.2, line_alpha=0) # Plot formatting fig.xaxis.axis_label = 'Date' fig.yaxis.axis_label = 'Position Angle (degrees)' return paGood, paBad, gd, fig
plt.figure('COV', facecolor='lightgray') plt.title('COV', fontsize=18) plt.xlabel('Date', fontsize=14) plt.ylabel('Price', fontsize=14) plt.grid(linestyle=':') plt.tick_params(labelsize=10) # 设置刻度定位器 ax = plt.gca() # 每周一一个主刻度 maloc = md.WeekdayLocator(byweekday=md.MO) ax.xaxis.set_major_locator(maloc) # 设置主刻度日期的格式 ax.xaxis.set_major_formatter(md.DateFormatter('%Y-%m-%d')) # DayLocator:每天一个次刻度 ax.xaxis.set_minor_locator(md.DayLocator()) # 把dates的数据类型改为matplotlib的日期类型 dates = dates.astype(md.datetime.datetime) # 绘制收盘价 plt.plot(dates, bhp_closing_prices, label='BHP Closing Prices', linewidth=2, color='dodgerblue', linestyle='-') # 绘制收盘价 plt.plot(dates, vale_closing_prices,
def source_solar_angle(catalogue, ref_antenna): """Source solar angle. The solar separation angle (in degrees) from the target observation region as seen by the ref_ant Parameters ---------- catalogue: list or file Data on the target objects to be observed ref_antenna: katpoint.Antenna A MeerKAT reference antenna Returns -------- solar separation angle for a target wrst ref_ant at a given time """ date = ref_antenna.observer.date horizon = numpy.degrees(ref_antenna.observer.horizon) date = date.datetime().replace(hour=0, minute=0, second=0, microsecond=0) numdays = 365 date_list = [date - timedelta(days=x) for x in range(0, numdays)] sun = katpoint.Target("Sun, special") target_tags = get_filter_tags(catalogue, targets=True) katpt_targets = catalogue.filter(target_tags) for cnt, katpt_target in enumerate(katpt_targets): plt.figure(figsize=(17, 7), facecolor="white") ax = plt.subplot(111) plt.subplots_adjust(right=0.8) fontP = FontProperties() fontP.set_size("small") solar_angle = [] for the_date in date_list: ref_antenna.observer.date = the_date sun.body.compute(ref_antenna.observer) katpt_target.body.compute(ref_antenna.observer) solar_angle.append( numpy.degrees(ephem.separation(sun.body, katpt_target.body))) myplot, = plt.plot_date(date_list, solar_angle, fmt=".", linewidth=0, label="{}".format(katpt_target.name)) ax.axhspan(0.0, horizon, facecolor="k", alpha=0.2) box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.95, box.height]) plt.grid() plt.legend(loc="center left", bbox_to_anchor=(1, 0.5), prop={"size": 10}, numpoints=1) plt.ylabel("Solar Separation Angle (degrees)") ax.set_xticklabels(date_list[0::20], rotation=30, fontsize=10) ax.xaxis.set_major_formatter(mdates.DateFormatter("%b %d")) ax.xaxis.set_major_locator( mdates.DayLocator(bymonthday=range(30), interval=10)) ax.set_xlabel("Date")
subList.append(int(tmpList[1]) / 1000000.0) #print sum(subList) with open('../data/peSubByDay.stat') as f: lines = f.read().splitlines() for line in lines: tmpList = line.split() peList.append(int(tmpList[1]) / 1000000.0) print sum(peList) #for date in dateList: # print date days = mdates.DayLocator(interval=7) fig, ax = plt.subplots() ax.plot(dateList, subList, 'b--', label='Submissions', linewidth=2.0) #ax.plot(dateList, peList, 'r-.', label = 'PE Submissions', linewidth=2.0) ax.plot(dateList, peList, 'g-', label='PE Submissions', linewidth=2.0) legend = ax.legend(loc='upper left', fontsize='large') plt.ylabel('VirusTotal reports (in million)', fontsize=18) ax.xaxis.set_major_locator(days) ax.xaxis.set_major_formatter(mdates.DateFormatter('%m-%d-%y')) datemin = dateList[0] - timedelta(days=3) datemax = dateList[-1] + timedelta(days=3) ax.set_xlim(datemin, datemax)
def burndown_chart_for_sprint(self, sprint): def _day(date): return datetime(date.year, date.month, date.day) import matplotlib.dates as mdates fmt = mdates.DateFormatter('%Y-%m-%d') days = mdates.DayLocator() sprint = self.resolve_sprint(sprint) entries = self.get_entries_in_sprint(sprint) if len(entries) == 0: return False length = self.get_expected_hours_for_sprint(sprint) start, end = self.get_dates_from_sprint(sprint) time_array = np.arange( _day(start).timestamp(), _day(end).timestamp() + 86400., 86400.) hours_left = np.ones(len(time_array)) * length hours = np.zeros(len(time_array)) for e in entries: d = self.to_datetime(e[2]) idx = np.searchsorted(time_array, d.timestamp()) hours_left[idx:] -= e[4] hours[idx] += e[4] ideal_burn = self.get_ideal_burn(sprint) actual_burn = self.get_actual_burn(sprint) projected_completion = start.timestamp( ) + 86400 * length / actual_burn if actual_burn != 0. else np.nan required_burn = self.get_required_burn(sprint) dates = [datetime.fromtimestamp(t) for t in time_array] last = datetime.fromtimestamp(_day(end).timestamp() + 86400.) x_max = datetime.fromtimestamp( min(max(projected_completion, _day(end).timestamp() + 86400.), _day(end).timestamp() + 5 * 86400.)) days_left = [] for i, d in enumerate(dates): if d.timestamp() > datetime.today().timestamp(): days_left.append(d) hours_left[i:] = 0. fig, (ax, ax2) = plt.subplots(nrows=2, ncols=1, sharex=True, figsize=(12, 12)) ax.bar(dates, hours_left, align='edge', alpha=0.5) ax.plot([dates[0], last], [length, 0.], ls='--', lw=3, c='black', label='ideal burn {:.1f} hr/day'.format(ideal_burn)) # ax.scatter([datetime.fromtimestamp(projected_completion)],[0.],s=100,c='blue',label='projected completion') ax.plot( [dates[0], datetime.fromtimestamp(projected_completion)], [length, 0.], ls='--', lw=3, c='blue', label='actual burn {:.1f} hr/day'.format(actual_burn)) ax.vlines(datetime.today(), 0., length, color='green', lw=3, label='today') ax.legend() ax.set_title("Burndown for {}\nRunning {} to {}".format( self.get_sprint_name_from_sprint(sprint), dates[0].date(), dates[-1].date())) ax.xaxis.set_major_formatter(fmt) ax.xaxis.set_major_locator(days) ax.grid(True) ax.set_xlim(dates[0], x_max) ax.set_ylabel('hours') ax2.bar(dates, hours, align='edge', alpha=0.5) if len(days_left) > 0: ax2.bar(days_left, required_burn * np.ones(len(days_left)), alpha=0.5, align='edge', label='goal {:.1f} hr/day'.format(required_burn)) ax2.vlines(datetime.today(), 0., length, color='green', lw=3) ax2.set_title("Hours per day\nDaily hourly gain {:.1f}".format( self.get_daily_gain_in_sprint(sprint))) ax2.xaxis.set_major_formatter(fmt) ax2.xaxis.set_major_locator(days) ax2.grid(True) ax2.set_xlim(dates[0], x_max) ax2.set_ylabel('hours') ax2.set_xlabel('date') if len(days_left) > 0: ax2.legend() fig.autofmt_xdate() plt.tight_layout() plt.show() return True
N[N < -32] = np.NaN W[W < -32] = np.NaN # time average Ebin = E.resample('60s').mean() Nbin = N.resample('60s').mean() Wbin = W.resample('60s').mean() Tbin = T.resample('1s').mean() fs = 1 / 2.0 # sample rate raw, Hz fs_bin = 1 / 60.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])] #### ------ filter timeseries ----- #### from scipy.signal import butter, lfilter, freqz, filtfilt def butter_lowpass(cutoff, fs, order=5):
facecolors='white', label="Expected peak", color="green", s=70) plt.scatter(sol, logistic_model(sol, a, b, c), zorder=10, label="Expected $\epsilon$-end", color="blue") plt.axhline(y=c, label='Final expected infected', color="magenta", linestyle="--", linewidth=0.9) ax = plt.axes() plt.grid(which='minor', alpha=0.2) ax.minorticks_on() plt.grid() ax.set_title('Logistic Model Covid-19 (Italy)') plt.legend(loc='lower right', prop={'size': 13}) plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d-%b')) plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=5)) plt.gcf().autofmt_xdate() plt.xticks(fontsize=12) plt.xlabel("Date") plt.ylabel("Total number of infected people") #~ plt.show() plt.savefig('../plots/italy.png', bbox_inches='tight')
def plot_rt(result, ax, state_name): ax.set_title(f"{state_name}") # Colors ABOVE = [1,0,0] MIDDLE = [1,1,0] BELOW = [0,1,0] cmap = ListedColormap(np.r_[ np.linspace(BELOW,MIDDLE,25), np.linspace(MIDDLE,ABOVE,25) ]) color_mapped = lambda y: np.clip(y, .5, 1.5)-.5 index = result['ML'].index.get_level_values('date') index = pd.to_datetime(index) values = result['ML'].values # Plot dots and line ax.plot(index, values, c='k', zorder=1, alpha=.25) ax.scatter(index, values, s=40, lw=.5, c=cmap(color_mapped(values)), edgecolors='k', zorder=2) # Aesthetically, extrapolate credible interval by 1 day either side lowfn = interp1d(date2num(index), result['Low_90'].values, bounds_error=False, fill_value='extrapolate') highfn = interp1d(date2num(index), result['High_90'].values, bounds_error=False, fill_value='extrapolate') extended = pd.date_range(start=pd.Timestamp('2020-03-30'), end=index[-1]+pd.Timedelta(days=1)) ax.fill_between(extended, lowfn(date2num(extended)), highfn(date2num(extended)), color='k', alpha=.1, lw=0, zorder=3) ax.axhline(1.0, c='k', lw=1, label='$R_t=1.0$', alpha=.25); # Formatting ax.xaxis.set_major_locator(mdates.MonthLocator()) ax.xaxis.set_major_formatter(mdates.DateFormatter('%b')) ax.xaxis.set_minor_locator(mdates.DayLocator()) ax.yaxis.set_major_locator(ticker.MultipleLocator(1)) ax.yaxis.set_major_formatter(ticker.StrMethodFormatter("{x:.1f}")) ax.yaxis.tick_right() ax.spines['left'].set_visible(False) ax.spines['bottom'].set_visible(False) ax.spines['right'].set_visible(False) ax.margins(0) ax.grid(which='major', axis='y', c='k', alpha=.1, zorder=-2) ax.margins(0) ax.set_ylim(0.0, 5.0) ax.set_xlim(pd.Timestamp('2020-03-30'), pd.to_datetime(result.index.get_level_values('date')[-1])+pd.Timedelta(days=1)) fig.set_facecolor('w')
def plot_rates(Md, Mdea, key, figsize=(6.5, 3), startdate=None): """plot the time series of new cases and dead for a specific country Parameters: ----------- Md : cases dictionary created by get_jhu_data function Mdea : dead dictionary created by get_jhu_data function key: country (or continent) key for data to plot figsize : default (6.5,3), optional startdate : datetime object, used to set left limit of the plot Returns: -------- list containing [figure handle, ax1 handle, ax2 handle] """ adata = dataset(Md[key], key) adatad = dataset(Mdea[key], key) if startdate is None: startdate = adata.do.dt[0] # modify startdate on first significant number of cases #for minnum in [10, 100]: # if np.max(adata.y)>minnum: # a=[i for i,x in enumerate(adata.y>minnum) if x] # startdate = adata.do.dt[a[0]] # days intervall for xticks daysrange = (adata.do.dt[-1] - startdate).days tickintervall = 14 if daysrange > 5 * 28: tickintervall = 28 if daysrange > 12 * 28: tickintervall = 2 * 28 # index of startdate: get y scaling startdateindx = np.nonzero( np.array([(x - startdate).days for x in adata.do.dt]) > 0)[0][0] f = plt.figure(tight_layout=True, figsize=figsize, dpi=120) # first subplot: cases ax1 = plt.subplot(211) plt.title(key) plt.bar(adata.do.dt, adata.dy, align='center', color='r', alpha=0.7, zorder=3) plt.step(adata.do.dt, adata.dy_weekly_mean, where='mid', c='b', lw=1.5) plt.grid(True, which='major', zorder=1) plt.grid(True, which='minor', lw=0.5, c='0.9', zorder=1) # second subplot: dead ax2 = plt.subplot(212) plt.bar(adatad.do.dt, adatad.dy, align='center', color='r', alpha=0.7, zorder=3) plt.step(adatad.do.dt, adatad.dy_weekly_mean, where='mid', c='b', lw=1.5) plt.grid(which='major') plt.grid(which='minor', lw=0.5, c='0.9', zorder=1) for ax in [ax1, ax2]: ax.set_xlim(left=startdate) ax.xaxis.set_major_formatter(mdates.DateFormatter('%d.%m')) ax.xaxis.set_major_locator(mdates.DayLocator(interval=tickintervall)) ax.yaxis.set_minor_locator(ticker.AutoMinorLocator()) # set y scaling if np.min(adata.dy) < -10: ymin = -100 else: ymin = -10 ax1.set_ylim([ymin, np.quantile(adata.dy[startdateindx::], 0.98) * 2]) if np.min(adatad.dy) < -10: ymin = -100 else: ymin = -10 ax2.set_ylim([ymin, np.quantile(adatad.dy[startdateindx::], 0.98) * 2]) ax1.set_ylabel('new cases') ax2.set_ylabel('new dead') #for label in ax1.get_xticklabels(): # label.set_ha("right") # label.set_rotation(45) return f, ax1, ax2
def plot_model(df, country, min_infections, start_exponential_phase, end_exponential_phase, start_linear_phase, end_linear_phase, end_after_n_days): # filter just one country df = df[df["Country/Region"] == country] df = df.drop(columns=["Country/Region", "Province/State", "Lat", "Long"]) df = df.iloc[0] # convert to pd.Series # start with first infections df = df[df.values > min_infections] # apply maximal number of days to consider df = df[:end_after_n_days] # parse to datetime df.index = pd.to_datetime(df.index, format='%m/%d/%y') # fit to exponential function duration_exponential_phase = end_exponential_phase - start_exponential_phase days_exponential_phase = np.arange( duration_exponential_phase) + start_exponential_phase poptimal_exponential, pcovariance_exponential = curve_fit( exponential, days_exponential_phase, df.values[start_exponential_phase:end_exponential_phase], p0=[1, 0.35, 0]) # compute exponential prediction prediction_in_days = 10 time_in_days_extra = np.arange(start=start_exponential_phase, stop=duration_exponential_phase + prediction_in_days) prediction = exponential(time_in_days_extra, *poptimal_exponential).astype(int) df_prediction_exponential = pd.Series(prediction) # convert index to dates df_prediction_exponential.index = pd.date_range( start=df.index[start_exponential_phase], periods=duration_exponential_phase + prediction_in_days, closed="left") # fit to linear function duration_linear_phase = end_linear_phase - start_linear_phase days_linear_phase = np.arange(duration_linear_phase) + start_linear_phase poptimal_linear, pcovariance_linear = curve_fit( linear_func, days_linear_phase, df.values[start_linear_phase:end_linear_phase], p0=[1, 1]) # compute linear prediction prediction_in_days = 20 time_in_days_extra = np.arange(start=start_linear_phase, stop=end_linear_phase + prediction_in_days) prediction_linear = linear_func(time_in_days_extra, *poptimal_linear).astype(int) df_prediction_linear = pd.Series(prediction_linear) # convert index to dates df_prediction_linear.index = pd.date_range( start=df.index[start_linear_phase], periods=duration_linear_phase + prediction_in_days, closed="left") fig, ax = plt.subplots(figsize=(15, 10)) # plot real data ax.plot(df.index, df.values, '*', color="blue", markersize=5, label=f"Infections in {country}") # plot exponential phase ax.plot(df.index[start_exponential_phase:end_exponential_phase], exponential(days_exponential_phase, *poptimal_exponential), 'r-', linewidth=2, label="Exponential Phase") # plot exponential prediction ax.plot(df_prediction_exponential.index[duration_exponential_phase:], df_prediction_exponential.values[duration_exponential_phase:], 'r--', label="Exponential Phase Prediction") # plot linear phase ax.plot(df.index[start_linear_phase:end_linear_phase], linear_func(days_linear_phase, *poptimal_linear), 'm-', linewidth=2, label="Linear Phase") # plot linear prediction ax.plot(df_prediction_linear.index[duration_linear_phase:], df_prediction_linear.values[duration_linear_phase:], 'm--', label="Linear Phase Prediction") ax.set_xlabel("Date") ax.set_ylabel("Number of Infections") ax.legend() ax.grid() ax.xaxis.set_major_locator(mdates.DayLocator(interval=2)) fig.suptitle(f"{date.today()} - Number of Infected persons in {country}") fig.autofmt_xdate() fig.savefig(f"plots/model_{country}.png", bbox_inches='tight')
def make_graph_image(total_by, days_prev, speed_over): ''' Extract Data from sql db and generate matplotlib graph showing totals for specified hour, day, month ''' if not (is_int(days_prev) and is_int(speed_over)): logging.error("days_prev and speed_over must be integer >= 0") return days_prev = abs(days_prev) # Make sure they are positive speed_over = abs(speed_over) speed_units = get_speed_units_str() total_by = total_by.upper() db_path = os.path.join(DB_DIR, DB_NAME) count_sql_query = get_query_str(total_by, days_prev, speed_over) right_now = dt.datetime.now() now = ("%02d-%02d-%02d-%02d:%02d" % (right_now.year, right_now.month, right_now.day, right_now.hour, right_now.minute)) if GRAPH_ADD_DATE_TO_FILENAME: file_now = now + '_' # prefix file name with datetime else: file_now = '' # No Datetime on filename image_filepath = os.path.join( GRAPH_PATH, file_now + 'graph_count_' + 'prev' + str(days_prev) + 'days_by' + total_by.lower() + '_ge' + str(speed_over) + speed_units + '.jpg') graph_title = ( 'Previous %s days COUNT by %s for SPEEDS >= %s %s\n%s' % (str(days_prev), total_by, str(speed_over), speed_units, now)) if DEBUG: logging.info("Running: %s", graph_title) logging.info("Connect to Database %s", db_path) connection = sqlite3.connect(db_path) connection.row_factory = sqlite3.Row cursor = connection.cursor() if DEBUG: logging.info('Executing Query \n %s', count_sql_query) cursor.execute(count_sql_query) xd = [] # list for database query date data y = [] # list for database query count data while True: row = cursor.fetchone() if row is None: break if DEBUG: print(row) xdat, ydat = row # Create x,y data lists for matplotlib plt if total_by == 'HOUR': xd.append(dt.datetime.strptime(xdat, '%Y-%m-%d %H')) elif total_by == 'DAY': xd.append(dt.datetime.strptime(xdat, '%Y-%m-%d')) elif total_by == 'MONTH': xd.append(dt.datetime.strptime(xdat, '%Y-%m')) y.append(ydat) cursor.close() connection.close() plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H')) plt.gca().xaxis.set_major_locator(mdates.DayLocator()) plt.gcf().autofmt_xdate() plt.figure(figsize=(10.0, 7.0), dpi=100) plt.title(graph_title) plt.ylabel('COUNT by ' + total_by) plt.plot(xd, y) plt.xticks(rotation=60) plt.tight_layout() plt.savefig(image_filepath) logging.info('Saved - %s', image_filepath) return image_filepath
dtype='M8[D], f8, f8, f8, f8', delimiter=',', converters={1:dmy2ymd}) print(dates,type(dates[0])) #绘制收盘价的折线图 mp.figure('AAPL', facecolor='lightgray') mp.title('AAPL', fontsize=14) mp.xlabel('Date', fontsize=12) mp.ylabel('Price', fontsize=12) mp.tick_params(labelsize=10) mp.grid(linestyle=':') # 设置刻度定位器 ax = mp.gca() #设置主刻度定位器-每周一一个主刻度 major_loc=md.WeekdayLocator(byweekday=md.MO) ax.xaxis.set_major_locator(major_loc) ax.xaxis.set_major_formatter( md.DateFormatter('%Y-%m-%d')) # 设置次刻度定位器为日定位器 minor_loc=md.DayLocator() ax.xaxis.set_minor_locator(minor_loc) mp.plot(list(dates), closing_prices, c='dodgerblue', linestyle='--', linewidth=3, label='AAPL') mp.legend() mp.gcf().autofmt_xdate() mp.show()
else: points.append(0.0) points = np.array(points) times = mdates.epoch2num(times) ax = plt.axes() formatter = mdates.DateFormatter("%H:%M") ax.xaxis.set_major_formatter(formatter) locator = mdates.HourLocator(interval=3) ax.xaxis.set_major_locator(locator) minor_formatter = mdates.DateFormatter("%m-%d") ax.xaxis.set_minor_formatter(minor_formatter) minor_locator = mdates.DayLocator() ax.xaxis.set_minor_locator(minor_locator) ax.xaxis.set_tick_params(which='minor', pad=15) colormap = np.where(points>0.5, 'r', 'b') plt.ylim(0, 1) plt.axhline(0.5) plt.bar(times, points, color=colormap, width=0.005) for tick in plt.xticks()[0]: plt.axvline(tick, dashes=(1,2), linewidth=0.5) ax.set_ylabel('nT/s') ax.set_title("Magneettinen aktiivisuus kuluneen vuorokauden aikana")
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.]) # # determine the discrete colors for all observables # colormap = plt.cm.tab20 # I suggest to use nipy_spectral, Set1, Paired # colors = [colormap(i) for i in np.linspace(0, 1, 3)] # # print('colors = {!s}'.format(colors)) # 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 annotatetxt = markerAnnotation(crd, stdDev2Plot[i]) 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(10, 60, 10), interval=1)) pass 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
def _gen_project_trend_graph(project, start, end, machine_category, force_overwrite=False): """Generates a bar graph for a project Keyword arguments: project -- Project start -- start date end -- end date machine_category -- MachineCategory object """ filename = graphs.get_project_trend_graph_filename(project, start, end, machine_category) csv_filename = os.path.join(GRAPH_ROOT, filename + '.csv') png_filename = os.path.join(GRAPH_ROOT, filename + '.png') _check_directory_exists(csv_filename) _check_directory_exists(png_filename) if not settings.GRAPH_DEBUG or force_overwrite: if os.path.exists(csv_filename): if os.path.exists(png_filename): return query = CPUJob.objects.filter(project=project, machine__category=machine_category, date__range=(start, end)) query = query.values('account', 'account__username', 'date') query = query.annotate(Sum('cpu_usage')).order_by('account', 'date') t_start = start t_end = end start_str = start.strftime('%Y-%m-%d') end_str = end.strftime('%Y-%m-%d') fig, ax = plt.subplots(figsize=(6, 4)) ax.set_xlim(start, end + datetime.timedelta(days=1)) ax.set_title('%s %s - %s' % (project.pid, start_str, end_str)) ax.set_ylabel("CPU Time (hours)") ax.set_xlabel("Date") locator = mdates.AutoDateLocator() ax.xaxis.set_major_locator(locator) ax.xaxis.set_major_formatter(mdates.AutoDateFormatter(locator)) ax.xaxis.set_minor_locator(mdates.DayLocator()) data = {} x_data = {} y_data = {} with open(csv_filename, 'wb') as csv_file: csv_writer = csv.writer(csv_file) for row in query.iterator(): csv_writer.writerow([ row['account__username'], row['date'], row['cpu_usage__sum'] / 3600.00 ]) account = row['account'] date = row['date'] if account not in data: data[account] = {} x_data[account] = [] y_data[account] = [] data[account][date] = row['cpu_usage__sum'] for account, dates in six.iteritems(data): start = t_start end = t_end while start <= end: total = 0 if start in dates: total = dates[start] x_data[account].append(start) y_data[account].append(total / 3600.00) start = start + datetime.timedelta(days=1) del data totals = [] start = t_start end = t_end while start <= end: totals.append(0) start = start + datetime.timedelta(days=1) count = 0 for account in x_data.keys(): ax.bar(x_data[account], y_data[account], bottom=totals, color=graphs.get_colour(count), edgecolor=graphs.get_colour(count), align='edge') count = count + 1 i = 0 start = t_start end = t_end while start <= end: totals[i] += y_data[account][i] i = i + 1 start = start + datetime.timedelta(days=1) del x_data del y_data del totals fig.autofmt_xdate() plt.tight_layout() plt.savefig(png_filename) plt.close()
def plot_overnight(ax, df): """ docstring """ hours = mdates.DayLocator(interval=1) df["date"] = df.index # print(df) h_fmt = mdates.DateFormatter('%d - %m') ax.xaxis.set_major_locator(hours) ax.xaxis.set_major_formatter(h_fmt) # df.plot(kind='line', linestyle="None", marker="o", # y=["close", "open"], ax=ax, grid=True, linewidth=0.5, color=["red", "green"]) ax = PlotI.plot_candles(df, ax, 0.5, 0.05) # ax.fill_between(df.index, df.close, df.open, where=df.close > df.open, # facecolor='green', interpolate=True, alpha=0.2) # ax.fill_between(df.index, df.close, df.open, where=df.open >= df.close, # facecolor='red', interpolate=True, alpha=0.2) if "weekday" not in df: df['date'] = pd.to_datetime(df.index, utc=True) # df.index = pd.to_datetime(df.index, utc=True) df["weekday"] = df.date.dt.dayofweek for val in range(0, len(df) - 1): print try: if df.iloc[val].weekday != (df.iloc[val + 1].weekday - 1): last_week_day = df.iloc[val].weekday weekend_percents = Utils.calc_perc(df.iloc[val].close, df.iloc[val + 1].open) ax.text(date2num(df.iloc[val].date), ax.get_ylim()[1], str(round(weekend_percents, 2)) + '%', fontsize=8, color="green" if weekend_percents >= 0 else "red") if (df.iloc[val - 1].weekday - df.iloc[val].weekday) > 1: first_week_day = df.iloc[val].weekday days_to_end = 1 while (df.iloc[val + days_to_end].weekday - df.iloc[val].weekday) > 0: days_to_end += 1 week_percents = Utils.calc_perc( df.iloc[val].open, df.iloc[val + days_to_end - 1].close) color = "green" if week_percents >= 0 else "red" # change in week ax.text(date2num(df.iloc[val].date), ax.get_ylim()[1], str(round(week_percents, 2)) + '%', fontsize=8, color=color) ax.axvspan(date2num(df.iloc[val].date), date2num(df.iloc[val + days_to_end - 1].date), alpha=0.05, color=color) # print(df.iloc[val].date.day_name() + " - " + df.iloc[val+days_to_end-1].date.day_name()) except IndexError: print("plot_overnight Index error") try: perc = Utils.calc_perc(df.iloc[val].close, df.iloc[val + 1].open) except IndexError: print("indexError") ax.text(df.iloc[val].date, ax.get_ylim()[0], str(round(perc, 2)) + '%', fontsize=8, color="green" if perc >= 0 else "red") ax.text(df.iloc[val].date, df.iloc[val].close, str(round(df.iloc[val].close, 1)), fontsize=8, color="brown") # # sum_perc = Utils.calc_perc( # # df.iloc[0].open, df.iloc[-1].close) # # ax.text(df.iloc[-1].date, ax.get_ylim()[1], str( # # round(sum_perc, 1)) + '%' + ' sum', fontsize=8, color="green" if sum_perc >= 0 else "red") # for tick in ax.get_xticklabels(): # tick.set_rotation(0) return ax
def _gen_institute_trend_graph(institute, start, end, machine_category, force_overwrite=False): """ Institute trend graph for machine category. """ filename = graphs.get_institute_trend_graph_filename( institute, start, end, machine_category) csv_filename = os.path.join(GRAPH_ROOT, filename + '.csv') png_filename = os.path.join(GRAPH_ROOT, filename + '.png') _check_directory_exists(csv_filename) _check_directory_exists(png_filename) if not settings.GRAPH_DEBUG or force_overwrite: if os.path.exists(csv_filename): if os.path.exists(png_filename): return query = CPUJob.objects.filter(project__institute=institute, machine__category=machine_category, date__range=(start, end)) query = query.values('date').annotate(Sum('cpu_usage')) query = query.order_by('date') t_start = start t_end = end start_str = start.strftime('%Y-%m-%d') end_str = end.strftime('%Y-%m-%d') fig, ax = plt.subplots(figsize=(6, 4)) ax.set_xlim(start, end) ax.set_title('%s - %s' % (start_str, end_str)) ax.set_ylabel("CPU Time (hours)") ax.set_xlabel("Date") locator = mdates.AutoDateLocator() ax.xaxis.set_major_locator(locator) ax.xaxis.set_major_formatter(mdates.AutoDateFormatter(locator)) ax.xaxis.set_minor_locator(mdates.DayLocator()) data = {} x_data = [] y_data = [] with open(csv_filename, 'wb') as csv_file: csv_writer = csv.writer(csv_file) for row in query.iterator(): csv_writer.writerow([row['date'], row['cpu_usage__sum'] / 3600.00]) date = row['date'] data[date] = row['cpu_usage__sum'] start = t_start end = t_end while start <= end: total = 0 if start in data: total = data[start] x_data.append(start) y_data.append(total / 3600.00) start = start + datetime.timedelta(days=1) del data ax.plot(x_data, y_data) del x_data del y_data fig.autofmt_xdate() plt.tight_layout() plt.savefig(png_filename) plt.close()
def plot_single_historical_subplot(subplot, df, near, far): #https://xkcd.com/color/rgb/ xkcd20 = [ "windows blue", "amber", "ruby", "reddy brown", "carnation", "spruce", "cool green", "swamp", "dusk blue", "indian red", "purple/blue", "petrol", "royal", "tan brown", "lightish red" ] sns.set_palette(sns.xkcd_palette(xkcd20)) # show the color palette #sns.palplot(sns.color_palette()) subplot.set_prop_cycle( cycler('color', sns.color_palette()) + (3 * cycler('marker', ['^', 'H', 'p', 'v', '*']))) ########### # plot shifted data with the mean ########### shiftedCols = [col for col in df.columns] subplot.xaxis.set_major_locator(dates.MonthLocator(interval=1)) #subplot.xaxis.set_major_formatter(ticker.FuncFormatter(lambda x,pos: get_month_label(x))) subplot.xaxis.set_major_formatter(dates.DateFormatter('%b-01')) subplot.xaxis.set_minor_locator( dates.DayLocator(bymonthday=[15], interval=1)) subplot.xaxis.set_minor_formatter(dates.DateFormatter('%d')) currYearColName = get_most_recent_year_column_name(df) for i, c in enumerate(shiftedCols): l = None if (c == currYearColName): subplot.plot(df[c], label=c, linewidth=2.5, color='black', markevery=100) subplot.axvline(df[c].last_valid_index(), color='blue', linewidth=1.75) subplot.axhline(df[c][df[c].last_valid_index()], color='blue', linewidth=1.75) else: subplot.plot(df[c], label=c, linewidth=1, markevery=14 + (2 * i)) # calculate the mean for all years (minus the current contract) and plot it cols = [col for col in shiftedCols if col not in [currYearColName]] df['mean'] = df[cols].mean(axis=1) df['99per'] = df[cols].apply(lambda x: np.nanpercentile(x, 99), axis=1) df['75per'] = df[cols].apply(lambda x: np.nanpercentile(x, 75), axis=1) df['25per'] = df[cols].apply(lambda x: np.nanpercentile(x, 25), axis=1) df['1per'] = df[cols].apply(lambda x: np.nanpercentile(x, 1), axis=1) subplot.fill_between( df.index, df['75per'].rolling(window=1, min_periods=1).mean(), df['25per'].rolling(window=1, min_periods=1).mean(), #color='#FFF717', color='#DEE2E3', alpha=0.65) subplot.plot(df['mean'], color='red', linewidth=2.5, label='AVG', markevery=100, linestyle='dashed') shiftedCols = [ col for col in df.columns if col.startswith('orig_') == False ] legend = subplot.legend(loc='lower left', ncol=2, frameon=True) frame = legend.get_frame() frame.set_facecolor('white') # Shrink the x-axis and place the legend on the outside # http://stackoverflow.com/a/4701285 #box = subplot.get_position() #subplot.set_position([box.x0, box.y0, box.width * 0.95, box.height * 0.90]) #legend = subplot.legend(frameon=True, loc='center left', bbox_to_anchor=(1,0.5), # fancybox=True, shadow=True, ncol=2) #frame = legend.get_frame() #frame.set_facecolor('white') subplot.set_title('{0} - {1}'.format(near, far)) subplot.grid(b=True, which='major', color='k', linewidth=0.5, linestyle='dashed') subplot.grid(b=True, which='minor', color='k', linewidth=0.5, linestyle='dotted') # rotate major tick labels to avoid overlapping # http://stackoverflow.com/a/28063506 plt.setp(subplot.get_xticklabels(), rotation=30, horizontalalignment='right')
ax2.spines['top'].set_visible(False) ax1.spines['right'].set_visible(False) ax2.spines['right'].set_visible(False) ax1.spines['bottom'].set_visible(False) ax2.spines['bottom'].set_visible(False) ax1.spines['left'].set_visible(False) ax2.spines['left'].set_visible(False) ax1.margins(0) ax2.margins(0) ax1.set_ylim(ax1.get_ylim()[0], ax1.get_ylim()[1] * 1.02) ax2.set_ylim(ax2.get_ylim()[0], ax2.get_ylim()[1] * 1.02) ax1.xaxis.set_major_formatter(mdates.DateFormatter('%d.%m.%Y')) ax2.xaxis.set_major_formatter(mdates.DateFormatter('%d.%m.%Y')) ax1.xaxis.set_major_locator(mdates.DayLocator(interval=int(view_length_2 / 7))) ax1.xaxis.set_minor_locator(mdates.DayLocator(interval=1)) ax2.xaxis.set_major_locator(mdates.DayLocator(interval=int(view_length_2 / 7))) ax2.xaxis.set_minor_locator(mdates.DayLocator(interval=1)) #plt.gcf().autofmt_xdate() plt.setp(plt.xticks()[1], rotation=30, ha='right') fig.tight_layout() ax1.grid(True,'major',ls='--',lw=.8,c='black',alpha=.3) ax1.grid(True,'minor',ls=':',lw=.5,c='k',alpha=.3) ax2.grid(True,'major',ls='--',lw=.8,c='black',alpha=.3) ax2.grid(True,'minor',ls=':',lw=.5,c='k',alpha=.3) ax1.fill_between(data_dates[-view_length_1:], ax1.get_ylim()[0], real_stock_price[-view_length_1:], color=color_palette["blue"], alpha=.3) ax2.fill_between(data_dates[-view_length_2:], ax2.get_ylim()[0], real_stock_price[-view_length_2:], color=color_palette["blue"], alpha=.3)
def test_RRuleLocator_dayrange(): loc = mdates.DayLocator() x1 = datetime.datetime(year=1, month=1, day=1, tzinfo=mdates.UTC) y1 = datetime.datetime(year=1, month=1, day=16, tzinfo=mdates.UTC) loc.tick_values(x1, y1)
# date processing from datetime import datetime import re # Animation from matplotlib.animation import FuncAnimation from matplotlib import animation # Date formatting import matplotlib.dates as mdates # from time import sleep # import scipy.interpolate.make_interp_spline as spl # Formaters for dates days = mdates.DayLocator() # every day months = mdates.MonthLocator() # every month s_fmt = mdates.DateFormatter('%b-%Y') should_logscale = False # Printf debugging def STAGE(stage, desc): print("[", stage, "]\t", desc) def SUBSTAGE(substage, desc): print("\t[", substage, "]\t", desc, flush=True)
def source_rise_set(catalogue, ref_antenna): """Set source rise time. display rise and set times in LST (default UTC) Parameters ---------- catalogue: list or file Data on the target objects to be observed ref_antenna: katpoint.Antenna objec A MeerKAT reference antenna Returns ------- The UTC time on the day when the source will be above the prescribed horizon """ date = ref_antenna.observer.date date = date.datetime().replace(hour=0, minute=0, second=0, microsecond=0) numdays = 365 date_list = [date - timedelta(days=x) for x in range(0, numdays)] target_tags = get_filter_tags(catalogue, targets=True) katpt_targets = catalogue.filter(target_tags) for cnt, katpt_target in enumerate(katpt_targets): plt.figure(figsize=(17, 7), facecolor="white") ax = plt.subplot(111) plt.subplots_adjust(right=0.8) fontP = FontProperties() fontP.set_size("small") rise_times = [] set_times = [] for the_date in date_list: ref_antenna.observer.date = the_date risetime = ref_antenna.observer.next_rising(katpt_target.body) settime = ref_antenna.observer.next_setting( katpt_target.body, risetime) risetime = risetime.datetime().time() rise_times.append(risetime.hour + risetime.minute / 60.0) settime = settime.datetime().time() set_times.append(settime.hour + settime.minute / 60.0) myplot, = plt.plot_date(date_list, rise_times, fmt=".", linewidth=0, label="{} rising".format(katpt_target.name)) myplot, = plt.plot_date(date_list, set_times, fmt=".", linewidth=0, label="{} setting".format(katpt_target.name)) ax.axhspan(7.25, 17.5, facecolor="k", alpha=0.2) box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.95, box.height]) plt.grid() plt.legend(loc='center left', bbox_to_anchor=(1, 0.5), prop={'size': 10}, numpoints=1) plt.ylabel("Time UTC (hour)") plt.yticks(numpy.arange(0.0, 24.0, 1.0), fontsize=10) ax.set_xticklabels(date_list[0::20], rotation=30, fontsize=10) ax.xaxis.set_major_formatter(mdates.DateFormatter("%b %d")) ax.xaxis.set_major_locator( mdates.DayLocator(bymonthday=range(30), interval=10)) ax.set_xlabel("Date")
def _generate_burn_down_chart(self, name, min_y, max_y, step_y, burn_down_show, burn_down_begin_date, burn_down_begin_value, burn_down_end_date, burn_down_end_value, data, graph_filename): date_format = "%d-%b-%Y" plt.clf() # set range min_x = datetime.strptime(self.chart_begin_date, date_format) max_x = datetime.strptime(self.chart_end_date, date_format) #plt.xlim(min_x, max_x) # sort values by datetime dates = [] values = [] for key, value in sorted(data.items()): dates.append(key) values.append(value) min_val = min(values) - 10 max_val = max(values) + 10 print(':::::::::::::::::::::', min_val) print(dates) print(dates[0]) print(dates[-1]) print(values) print(':::::::::::::::::::::', max_val) plt.xlim(dates[0], dates[-1]) history_interval_in_days = (dates[-1] - dates[0]).days # plot history data plt.plot_date(dates, values, "-") # plot burn down if burn_down_show: burn_down_begin_x = datetime.strptime(burn_down_begin_date, date_format) burn_down_end_x = datetime.strptime(burn_down_end_date, date_format) plt.plot([burn_down_begin_x, burn_down_end_x], [burn_down_begin_value, burn_down_end_value], '--r', label="Burndown") # plot labels plt.xlabel('Date') plt.title(name) # plot ticks years = mdates.YearLocator() # every year months = mdates.MonthLocator() # every month day = mdates.DayLocator() years_fmt = mdates.DateFormatter('%Y') dt_fmt = mdates.DateFormatter('%d-%m-%y') if (history_interval_in_days > 365): plt.gca().xaxis.set_major_locator(years) plt.gca().xaxis.set_minor_locator(months) elif (history_interval_in_days > 90): plt.gca().xaxis.set_major_locator(months) plt.gca().xaxis.set_minor_locator( mdates.WeekdayLocator(interval=2)) elif (history_interval_in_days > 30): plt.gca().xaxis.set_major_locator( mdates.WeekdayLocator(interval=2)) plt.gca().xaxis.set_minor_locator( mdates.WeekdayLocator(interval=1)) elif (history_interval_in_days < 13): plt.gca().xaxis.set_major_locator(mdates.DayLocator()) #plt.gca().xaxis.set_minor_locator(mdates.WeekdayLocator(interval=1)) else: plt.gca().xaxis.set_major_locator( mdates.WeekdayLocator(interval=1)) plt.gca().xaxis.set_minor_locator(mdates.DayLocator(interval=2)) #plt.gca().xaxis.set_major_locator(mdates.WeekdayLocator(interval=1)) plt.gca().xaxis.set_major_formatter( mdates.DateFormatter('%d-%m-%y')) #dt_fmt)#years_fmt) #plt.gca().xaxis.set_minor_locator(day)#months) plt.gca().xaxis.set_minor_formatter(DateFormatter('%d')) #stp=8#int( (max_val-min_val)/8 ) #plt.yticks(np.arange(min_val, max_val, step=stp)) # plt.yticks(np.arange(min_y, max_y, step=step_y)) # save image plt.savefig(graph_filename, bbox_inches='tight', dpi=70)
forecast = np.reshape(forecast, (-1)) #plot the data plt.figure(figsize=(45, 25)) ax = plt.gca() annotate_points(prediction_dates, prediction, ax, intervals) annotate_points(forecast_dates, forecast, ax, interval_between_predicted) ax.xaxis.set_major_formatter(mdates.DateFormatter('%d.%m %H:%M')) ax.set_yticks( np.arange( np.min(close_data_plot) - 1, np.max(close_data_plot) + 1, intervals)) ax.xaxis.set_major_locator(mdates.DayLocator(interval=1)) plt.plot(currency_dates[-500:], close_data_plot[-500:]) plt.plot(prediction_dates[-500:], prediction[-500:]) plt.plot(forecast_dates, forecast, marker='o') additional_info = "Currency: " + currency + "\n" + '\n epoch: ' + str( num_epochs) + '\n window size: ' + str(n_steps) plt.xlabel("Time") plt.ylabel("Price") plt.figtext(.9, 0.9, additional_info) plt.legend(['original', 'trained', 'prediction']) ax.grid(True) save_plot(save_plot_dir)
check_XY_dt = np.concatenate((check_X_dt, check_Y_dt), axis=1) check_XY = np.concatenate((check_X0, check_Y), axis=1) with tf.Session() as sess: saver.restore(sess, dir0 + "final.ckpt") check_outputs = sess.run(outputs, feed_dict={X: check_X, Y: check_Y}) check_mse = ((check_Y - check_outputs)**2).mean(axis=1) fig, axes = plt.subplots(n_check, 1, figsize=(9, 15)) for i in range(n_check): ax = axes[i] ax.plot(check_XY_dt[i], check_XY[i], 'k.-', label='obs') ax.plot(check_X_dt[i], check_X1[i], 'b.-', label='tid') ax.plot(check_Y_dt[i], check_outputs[i], 'r.-', label='prediction') days = mdates.DayLocator() hours = mdates.HourLocator() dt_fmt = mdates.DateFormatter('%b %d') ax.xaxis.set_major_locator(days) ax.xaxis.set_major_formatter(dt_fmt) ax.xaxis.set_minor_locator(hours) ax.set_title('mse = {:0.4f}'.format(check_mse[i])) ax.legend(loc=2) ax.set_xlabel( '{:s} {:s} X:{:d}h Y:{:d}h Overall test mse = {:0.4f}m'.format( station, feature, x_len, y_len, mse_test), weight='bold') fig.tight_layout() fig.savefig(dir0 + 'check.png', format='png', dpi=300) plt.close(fig)
def scatterPlot(**kwargs): fig, ax = plt.subplots(figsize=(8, 5)) number_of_locations = len(kwargs['locations']) number_of_samples = len(kwargs['a_df']) if (number_of_locations > 1): plural_or_not = 'locations' else: plural_or_not = 'location' kwargs['the_title'][ "label"] = "Total number of samples: {} from {} {}.".format( number_of_samples, number_of_locations, plural_or_not) kwargs['the_sup_title']["label"] = '{}, {} - {}'.format( kwargs['the_sup_title']["label"], kwargs['min_date'], kwargs['max_date']) color_map = plt.cm.get_cmap(kwargs['color_map'], 100) color = iter(color_map(np.linspace(.2, .75, number_of_locations))) a_df = kwargs['a_df'] for location in kwargs['locations']: new_color = next(color) new_df = a_df[a_df['location'] == location] x = new_df['py_date'] y = new_df['total'] plt.scatter(x, y, color=new_color, label=location, s=kwargs['point_size'], edgecolor=kwargs['edge_c']) plt.ylabel(kwargs['y_axis']['label'], fontfamily=kwargs['y_axis']['fontfamily'], labelpad=kwargs['y_axis']['lablepad'], color=kwargs['y_axis']['color'], size=kwargs['y_axis']['size']) plt.xlabel(kwargs['x_axis']['label'], fontfamily=kwargs['x_axis']['fontfamily'], labelpad=kwargs['x_axis']['lablepad'], color=kwargs['x_axis']['color'], size=kwargs['x_axis']['size'], ha='left', x=0) plt.subplots_adjust(**kwargs['subplot_params']) plt.title( kwargs['the_title']['label'], fontdict=kwargs['title_style'], pad=kwargs['the_title_position']['pad'], loc=kwargs['the_title_position']['loc'], ) plt.suptitle( kwargs['the_sup_title']['label'], fontdict=kwargs['sup_title_style'], # color=kwargs['sup_title_style']['color'], x=kwargs['sup_title_position']['x'], y=kwargs['sup_title_position']['y'], va=kwargs['sup_title_position']['va'], ha=kwargs['sup_title_position']['ha']) plt.grid(b=True, which='major', axis='both') years = mdates.YearLocator() months = mdates.MonthLocator() days = mdates.DayLocator() weeks = mdates.WeekdayLocator(byweekday=1, interval=1, tz=None) years_fmt = mdates.DateFormatter(kwargs['x_tick_date']['years']) months_fmt = mdates.DateFormatter(kwargs['x_tick_date']['months']) days_fmt = mdates.DateFormatter(kwargs['x_tick_date']['days']) if (kwargs['ticks'] == 'years'): ax.xaxis.set_major_locator(years) ax.xaxis.set_major_formatter(years_fmt) ax.xaxis.set_minor_locator(months) elif (kwargs['ticks'] == 'months'): ax.xaxis.set_major_locator(years) ax.xaxis.set_major_formatter(years_fmt) ax.xaxis.set_minor_locator(months) ax.xaxis.set_minor_formatter(months_fmt) elif (kwargs['ticks'] == 'days'): ax.xaxis.set_major_locator(weeks) ax.xaxis.set_major_formatter(days_fmt) ax.xaxis.set_minor_locator(days) save_the_figure(**kwargs['save_this']) plt.show() plt.close()
def plot_launches(filename, type): f = open(filename, 'r') dates = [] times = [] launches = [] distances = [] for line in f: data = line.rstrip("\n").split(" ") dates.append(data[0]) launches.append(float(data[1])) times.append(float(data[2])) distance = float(data[3]) - MARS_RADIUS if distance < 0: distance = 0 distances.append(distance) f.close() min_index = np.argmin(distances) print("Minimum distance to Mars achieved:") print("\tLaunch Day:", (INITIAL_DATE + datetime.timedelta(seconds=launches[min_index]) ).strftime('%Y-%m-%d %H:%M:%S'), "(" + str(launches[min_index]) + "[s])") print("\tArrival day:", (INITIAL_DATE + datetime.timedelta(seconds=times[min_index]) ).strftime('%Y-%m-%d %H:%M:%S') + "[s]") print("\tTime of Flight:", str(times[min_index]) + "[s]") print("\tDistance to Mars:", str(distances[min_index]) + "[km]") if type == TYPE_MINUTE or type == TYPE_SECOND or type == TYPE_HOUR: date_index = 0 x_values = [] for i in range(len(launches)): sub_date = INITIAL_DATE + datetime.timedelta(seconds=launches[i]) x_values.append(sub_date) y_values = distances elif type == TYPE_DAY: x_values = [ datetime.datetime.strptime(d, "%Y-%m-%d").date() for d in dates ] y_values = distances elif type == TYPE_WEEK: x_values = [ datetime.datetime.strptime(d, "%Y-%m-%d").date() for d in dates ] y_values = distances ax = plt.gca() formatter = mdates.DateFormatter("%d-%m-%Y") ax.xaxis.set_major_formatter(formatter) locator = mdates.DayLocator() ax.xaxis.set_major_locator(locator) plt.scatter(x_values, y_values, color="red") # Removes the scientific notation on top # https://stackoverflow.com/questions/28371674/prevent-scientific-notation-in-matplotlib-pyplot ax.ticklabel_format(style='plain', axis='y') # Format the date into months & days # Change the tick interval if type == TYPE_SECOND: interval = 15 plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S')) plt.gca().xaxis.set_major_locator( mdates.SecondLocator(interval=interval)) plt.gca().set_ylabel("Distancia a Superficie de Marte [km]") plt.gca().set_xlabel("Fecha de Despegue [" + dates[0] + "]") elif type == TYPE_MINUTE: interval = 10 plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S')) plt.gca().xaxis.set_major_locator( mdates.MinuteLocator(interval=interval)) plt.gca().set_ylabel("Distancia a Superficie de Marte [km]") plt.gca().set_xlabel("Fecha de Despegue [" + dates[0] + "]") elif type == TYPE_HOUR: interval = 4 plt.gca().xaxis.set_major_formatter( mdates.DateFormatter('%m-%d %H:%M')) plt.gca().xaxis.set_major_locator( mdates.HourLocator(interval=interval)) plt.gca().set_ylabel("Distancia a Superficie de Marte [km]") plt.gca().set_xlabel("Fecha de Despegue [Año " + dates[0].split("-")[0] + "]") elif type == TYPE_DAY: interval = 2 plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d-%m')) plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=interval)) plt.gca().set_ylabel("Distancia a Superficie de Marte [km]") plt.gca().set_xlabel("Fecha de Despegue [Año " + dates[0].split("-")[0] + "]") elif type == TYPE_WEEK: # https://stackoverflow.com/questions/46555819/months-as-axis-ticks interval = 2 plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m-%Y')) plt.gca().xaxis.set_major_locator( mdates.MonthLocator(interval=interval)) plt.gca().set_ylabel("Distancia a Superficie de Marte [km]") plt.gca().set_xlabel("Fecha de Despegue") # Puts x-axis labels on an angle plt.gca().xaxis.set_tick_params(rotation=30) plt.show()
days = [0,0,0,0,0,0,0,0,0,0,0] iterCount = 0 dates = ["Oct 11", "Oct 12", "Oct 13", "Oct 14", "Oct 15", "Oct 16", "Oct 17", "Oct 18", "Oct 19", "Oct 20", "Oct 21"] while (iterCount < len(days)): days[iterCount] = col.find({ "created_at" : {'$regex': dates[iterCount]}}).count() iterCount += 1 plt.plot(date, days, label=title) print "Created plot: " + title return days; firstDay = dt.date(2016,10,11) date = [firstDay + dt.timedelta(days=x) for x in range(0,11)] plt.gca().xaxis.set_major_formatter(dates.DateFormatter('%m/%d/%Y')) plt.gca().xaxis.set_major_locator(dates.DayLocator()) sumDays = [0,0,0,0,0,0,0,0,0,0,0] con = MongoClient('localhost', 27017) db = con.streamData altcoin = db.altcoin bitcoin = db.bitcoin coindesk = db.coindesk cryptocurrency = db.cryptocurrency gold = db.gold appl = db.appl goog = db.goog yhoo = db.yhoo CurrentSum = createPlot("Altcoin", altcoin) sumDays = [x+y for x,y in zip(sumDays,CurrentSum)]
def visualize(self, results, positive_anomalies, test_alerts, symptom_date, diagnosis_date, k): """ visualize all the data with anomalies and alerts """ try: with plt.style.context('fivethirtyeight'): fig, ax = plt.subplots(1, figsize=(80, 15)) ax.bar(test_alerts['index'], test_alerts['heartrate'], linestyle='-', color='midnightblue', lw=6, width=0.01) training = data_train[0] training = training.reset_index() ax.bar(training['index'], training['heartrate'], linestyle='-', color='midnightblue', lw=6, width=0.01) print(training) colors = { 0: '', 'RED': 'red', 'YELLOW': 'yellow', 'GREEN': 'lightgreen' } for i in range(len(test_alerts)): v = colors.get(test_alerts['alert_type'][i]) ax.vlines(test_alerts['index'][i], test_alerts['heartrate'].min(), test_alerts['heartrate'].max(), linestyle='dotted', lw=4, color=v) #ax.scatter(positive_anomalies['index'],positive_anomalies['heartrate'], color='red', label='Anomaly', s=500) ax.tick_params(axis='both', which='major', color='blue', labelsize=60) ax.tick_params(axis='both', which='minor', color='blue', labelsize=60) ax.set_title(myphd_id, fontweight="bold", size=50) # Title ax.set_ylabel('Std. RHR\n', fontsize=50) # Y label ax.axvline(pd.to_datetime(symptom_date), color='grey', zorder=1, linestyle='--', marker="v", markersize=22, lw=6) # Symptom date ax.axvline(pd.to_datetime(diagnosis_date), color='purple', zorder=1, linestyle='--', marker="v", markersize=22, lw=6) # Diagnosis date ax.tick_params(axis='both', which='major', labelsize=60) ax.tick_params(axis='both', which='minor', labelsize=60) ax.xaxis.set_major_locator(mdates.DayLocator(interval=7)) ax.grid(zorder=0) ax.grid(True) plt.xticks(fontsize=30, rotation=90) plt.yticks(fontsize=50) ax.patch.set_facecolor('white') fig.patch.set_facecolor('white') figure = fig.savefig("_" + str(round(lst[k], 1)) + "_" + myphd_id_figure1, bbox_inches='tight') return figure except: with plt.style.context('fivethirtyeight'): fig, ax = plt.subplots(1, figsize=(80, 15)) ax.bar(test_alerts['index'], test_alerts['heartrate'], linestyle='-', color='midnightblue', lw=6, width=0.01) training = data_train[0] training = training.reset_index() ax.bar(training['index'], training['heartrate'], linestyle='-', color='midnightblue', lw=6, width=0.01) print(training) colors = { 0: '', 'RED': 'red', 'YELLOW': 'yellow', 'GREEN': 'lightgreen' } for i in range(len(test_alerts)): v = colors.get(test_alerts['alert_type'][i]) ax.vlines(test_alerts['index'][i], test_alerts['heartrate'].min(), test_alerts['heartrate'].max(), linestyle='dotted', lw=4, color=v) #ax.scatter(positive_anomalies['index'],positive_anomalies['heartrate'], color='red', label='Anomaly', s=500) ax.tick_params(axis='both', which='major', color='blue', labelsize=60) ax.tick_params(axis='both', which='minor', color='blue', labelsize=60) ax.set_title(myphd_id, fontweight="bold", size=50) # Title ax.set_ylabel('Std. RHR\n', fontsize=50) # Y label ax.tick_params(axis='both', which='major', labelsize=60) ax.tick_params(axis='both', which='minor', labelsize=60) ax.xaxis.set_major_locator(mdates.DayLocator(interval=7)) ax.grid(zorder=0) ax.grid(True) plt.xticks(fontsize=30, rotation=90) plt.yticks(fontsize=50) ax.patch.set_facecolor('white') fig.patch.set_facecolor('white') figure = fig.savefig("_" + str(round(lst[k], 1)) + "_" + myphd_id_figure1, bbox_inches='tight') return figure