def square_root_diffusion_exact(initial_val=0.05, kappa=3.0, theta=0.02, sigma=0.1, time_year=2, num_samples=10000, num_time_interval_discretization=50): x = np.zeros((num_time_interval_discretization + 1, num_samples)) x[0] = initial_val dt = time_year / num_time_interval_discretization for t in range(1, num_time_interval_discretization + 1): df = 4 * theta * kappa / sigma**2 c = (sigma**2 * (1 - np.exp(-kappa * dt))) / (4 * kappa) nc = np.exp(-kappa * dt) / c * x[t - 1] x[t] = c * npr.noncentral_chisquare(df, nc, size=num_samples) plt.figure(figsize=(10, 6)) plt.hist(x[-1], bins=50) plt.title("Square root diffusion Exact") plt.xlabel('value') plt.ylabel('frequency') plt.show() plt.figure(figsize=(10, 6)) plt.plot(x[:, :10], lw=1.5) plt.xlabel('time') plt.ylabel('index level') plt.title('Sample Path SRD Exact') plt.show() return x
def geometric_brownian_motion_option_pricing( initial_val=100, num_samples=10000, riskless_rate=0.05, volatility_sigma=0.25, time_year=2.0, num_time_interval_discretization=50): dt = time_year / num_time_interval_discretization samples = np.zeros((num_time_interval_discretization + 1, num_samples)) samples[0] = initial_val for t in range(1, num_time_interval_discretization + 1): samples[t] = samples[t - 1] * np.exp( (riskless_rate - 0.5 * (volatility_sigma**2)) * dt + volatility_sigma * np.sqrt(dt) * npr.standard_normal(num_samples)) print(45 * "=") print(samples[1]) plt.figure(figsize=(10, 6)) plt.hist(samples[50], bins=50) plt.title("Geometric Brownian Motion") plt.xlabel('index level') plt.ylabel('frequency') plt.show() plt.figure(figsize=(10, 6)) plt.plot(samples[:, :10], lw=1.5) plt.xlabel('time') plt.ylabel('index level') plt.title('Sample Path') plt.show() return samples
def square_root_diffusion_euler(initial_val=0.05, kappa=3.0, theta=0.02, sigma=0.1, time_year=2, num_samples=10000, num_time_interval_discretization=50): dt = time_year / num_time_interval_discretization xh = np.zeros((num_time_interval_discretization + 1, num_samples)) x = np.zeros_like(xh) xh[0] = initial_val x[0] = initial_val for t in range(1, num_time_interval_discretization + 1): xh[t] = (xh[t - 1] + kappa * (theta - np.maximum(xh[t - 1], 0)) * dt + sigma * np.sqrt(np.maximum(xh[t - 1], 0)) * math.sqrt(dt) * npr.standard_normal(num_samples)) x = np.maximum(xh, 0) plt.figure(figsize=(10, 6)) plt.hist(x[-1], bins=50) plt.xlabel('value') plt.ylabel('frequency') plt.title('Square root diffusion Approx Euler') plt.show() plt.figure(figsize=(10, 6)) plt.plot(x[:, :10], lw=1.5) plt.xlabel('time') plt.ylabel('index level') plt.title('Sample Path SRD approx') plt.show() return x
def pure_data_plot(self,connect=False,suffix='',cmap=cm.jet,bg=cm.bone(0.3)): #fig=plt.figure() ax=plt.axes() plt.axhline(y=0,color='grey', zorder=-1) plt.axvline(x=0,color='grey', zorder=-2) if cmap is None: if connect: ax.plot(self.x,self.y, 'b-',lw=2,alpha=0.5) ax.scatter(self.x,self.y, marker='o', c='b', s=40) else: if connect: if cmap in [cm.jet,cm.brg]: ax.plot(self.x,self.y, 'c-',lw=2,alpha=0.5,zorder=-1) else: ax.plot(self.x,self.y, 'b-',lw=2,alpha=0.5) c=[cmap((f-self.f[0])/(self.f[-1]-self.f[0])) for f in self.f] #c=self.f ax.scatter(self.x, self.y, marker='o', c=c, edgecolors=c, zorder=True, s=40) #, cmap=cmap) #plt.axis('equal') ax.set_xlim(xmin=-0.2*amax(self.x), xmax=1.2*amax(self.x)) ax.set_aspect('equal') #, 'datalim') if cmap in [cm.jet,cm.brg]: ax.set_axis_bgcolor(bg) if self.ZorY == 'Z': plt.xlabel(r'resistance $R$ in Ohm'); plt.ylabel(r'reactance $X$ in Ohm') if self.ZorY == 'Y': plt.xlabel(r'conductance $G$ in Siemens'); plt.ylabel(r'susceptance $B$ in Siemens') if self.show: plt.show() else: plt.savefig(join(self.sdc.plotpath,'c{}_{}_circle_data'.format(self.sdc.case,self.ZorY)+self.sdc.suffix+self.sdc.outsuffix+suffix+'.png'), dpi=240) plt.close()
def plot_weightings(): """Plots all weighting functions defined in :module: splweighting.""" from scipy.signal import freqz from pylab import plt, np sample_rate = 48000 num_samples = 2 * 4096 fig, ax = plt.subplots() for name, weight_design in sorted(_weighting_coeff_design_funsd.items()): b, a = weight_design(sample_rate) w, H = freqz(b, a, worN=num_samples) freq = w * sample_rate / (2 * np.pi) ax.semilogx(freq, 20 * np.log10(np.abs(H) + 1e-20), label='{}-Weighting'.format(name)) plt.legend(loc='lower right') plt.xlabel('Frequency / Hz') plt.ylabel('Damping / dB') plt.grid(True) plt.axis([10, 20000, -80, 5]) return fig, ax
def plot2(self, figNum, time1, data1, time2, data2, title='', units='', options=''): plt.figure(figNum) # plt.hold(True); plt.grid(True) if title: self.title = title if not units: self.units = units # plt.cla() if self.preTitle: fig = plt.gcf() fig.canvas.set_window_title("Figure %d - %s" % (figNum, self.preTitle)) plt.title("%s" % (self.title)) plt.plot(time1, data1, options) plt.plot(time2, data2, options) plt.ylabel('(%s)' % (self.units)) plt.xlabel('Time (s)') plt.margins(0.04)
def plot_inv_conv(fvals, name, direc): plt.figure() plt.semilogy(fvals, 'ko-') plt.xlabel('Iterations') plt.ylabel('Cost Function') plt.savefig(os.path.join(direc, name + '.png'), dpi=300) plt.close()
def subplotSingle2x(self, figNum, plotNum, numRows, numCols, time, data, title='', units='', options=''): print("subplotSingle2x") plt.figure(figNum) if title: self.title = title if not units: self.units = units if self.preTitle: fig = plt.gcf() fig.canvas.set_window_title("%s" % (figNum, self.preTitle)) if not figNum in self.sharex.keys(): self.sharex[figNum] = plt.subplot(numRows, numCols, plotNum) plt.plot(time, data, options) plt.subplot(numRows, numCols, plotNum, sharex=self.sharex[figNum]) # plt.hold(True); plt.grid(True) plt.title("%s" % (self.title)) plt.plot(time, data, options) plt.ylabel('(%s)' % (self.units)) plt.margins(0.04)
def CHART_Running_Annual_Vol_with_Hourly_Samples(frequency,window,trading_hours_per_day): Trading_Hours_in_Trading_Year=252*trading_hours_per_day #Calculates Trading Hours in a year Sample=data['Price'][frequency-1::frequency] #Creates New Sampling list based on frequency input Returns=np.log(Sample) - np.log(Sample.shift(1)) #Calculates Returns on New Sample Running_Variance=Returns.rolling(window).var() #Calculates hourly running variance based on 'window size' input Running_Annual_Vol=np.sqrt(Running_Variance)*np.sqrt(Trading_Hours_in_Trading_Year/frequency) #Place Running Vols and Time Series in DataFrame DF=pd.DataFrame(Sample) DF['Running_Vol']=Running_Annual_Vol #Create Plot DF.Price.plot() plt.legend() plt.ylabel('Yield (%)') DF.Running_Vol.plot(secondary_y=True, style='g',rot=90) plt.xlabel('Date') plt.ylabel('Running Vol') plt.title('10 Year Bund Yield vs Annualized Running Vol (Window Size=200)') plt.legend(bbox_to_anchor=(0.8, 1)) plt.text(0.8, 5.4, "Frequency={}. Window Size={}. Trading Hours per Day={}".format(frequency, window,trading_hours_per_day)) return plt
def CHART_Running_Annual_Vol_with_Daily_Samples_on_Specific_Time_of_Day(frequency,sampling_time,window,trading_hours_per_day): Original_DAILY_Sample=data['Price'][sampling_time-1::trading_hours_per_day] #Grabs the Hourly Data and Converts it into Daily Data based on Sampling Time of Day AND Trading Hours per Day NEW_Sample=Original_DAILY_Sample[frequency-1::frequency] #Creates New Sampling list based on sampling frequency input Returns=np.log(NEW_Sample) - np.log(NEW_Sample.shift(1)) #Calculates Returns on New Sample Running_Variance=Returns.rolling(window).var() #Calculates daily running variance based on 'window size' input Running_Annual_Vol=np.sqrt(Running_Variance)*np.sqrt(252/frequency) #Place NEW Sampled data (prices) and Running Vols in DataFrame DF=pd.DataFrame(NEW_Sample) DF['Running_Vol']=Running_Annual_Vol #Create Plot DF.Price.plot() plt.legend() #data.Price.plot() plt.ylabel('Yield (%)') DF.Running_Vol.plot(secondary_y=True, style='g',rot=90) plt.xlabel('Date') plt.ylabel('Running Vol') plt.title('10 Year Bund Yield vs Annualized Running Vol ') plt.legend(bbox_to_anchor=(0.8, 1)) plt.text(0.8, 3.5, "Sampling Time={}. Window Size={}. Trading Hours per Day={}".format(sampling_time, window,trading_hours_per_day)) return plt
def plot_cumu_disp_decomposition(self, site, cmpt, loc=2, leg_fs=7, if_ylim=False): self.plot_cumu_obs_linres(site, cmpt) y = self.plot_cumu_disp_pred_added(site, cmpt, label='pred.') y += self.plot_R_co(site, cmpt, style='-^', label='Rco', color='orange') y += self.plot_E_cumu_slip(site, cmpt, color='green') y += self.plot_R_aslip(site, cmpt, color='black') plt.grid('on') if if_ylim: plt.ylim(calculate_lim(y)) plt.ylabel(r'meter') plt.legend(loc=loc, prop={'size': leg_fs}) plt.gcf().autofmt_xdate() plt.title('Cumulative Disp.: {site} - {cmpt}'.format( site=get_site_true_name(site_id=site), cmpt=cmpt))
def plot_post_disp_decomposition( self, site, cmpt, loc=2, leg_fs=7, marker_for_obs='x', ): y = self.plot_post_obs_linres(site, cmpt, label='obs.', marker=marker_for_obs) y += self.plot_post_disp_pred_added(site, cmpt, label='pred.') y += self.plot_R_co(site, cmpt, style='-^', label='Rco', color='orange') y += self.plot_E_aslip(site, cmpt, color='green') y += self.plot_R_aslip(site, cmpt, color='black') plt.grid('on') plt.legend(loc=loc, prop={'size': leg_fs}) plt.ylabel(r'meter') plt.gcf().autofmt_xdate() plt.title('Postseismic Disp. : {site} - {cmpt}'.format( site=get_site_true_name(site_id=site), cmpt=cmpt))
def print_statistics(a1, a2, a1_type, a2_type): ''' Prints selected statistics. Parameters ========== a1, a2: ndarray objects results objects from simulation ''' sta1 = scs.describe(a1) sta2 = scs.describe(a2) print('%14s %14s %14s' % ('statistic', 'data set 1', 'data set 2')) print(45 * "-") print('%14s %14.0f %14.0f' % ('size', sta1[0], sta2[0])) print('%14s %14.3f %14.3f' % ('min', sta1[1][0], sta2[1][0])) print('%14s %14.3f %14.3f' % ('max', sta1[1][1], sta2[1][1])) print('%14s %14.3f %14.3f' % ('mean', sta1[2], sta2[2])) print('%14s %14.3f %14.3f' % ('std', np.sqrt(sta1[3]), np.sqrt(sta2[3]))) print('%14s %14.3f %14.3f' % ('skew', sta1[4], sta2[4])) print('%14s %14.3f %14.3f' % ('kurtosis', sta1[5], sta2[5])) a1_sort = np.sort(a1) a2_sort = np.sort(a2) plt.scatter(x=a1_sort, y=a2_sort, marker='.', color='darkred') plt.plot(a1_sort, a1_sort, linestyle='dashed', color='darkblue', alpha=0.4) plt.xlabel(a1_type) plt.ylabel(a2_type)
def plot_roc_curve(fpr, tpr, label=None): """ Plots Rceiver Operating Characteristic (ROC) curve from false_positive_rate(fpr), true_positive_rate(tpr) Requires imports: from sklearn.metrics import roc_curve Returns: Nothing """ from pylab import mpl, plt import matplotlib.pyplot as plt import numpy as np plt.style.use('seaborn') mpl.rcParams['font.family'] = 'arial' np.random.seed(1000) np.set_printoptions(suppress=True, precision=4) plt.plot(fpr, tpr, linewidth=2, label=label) plt.plot([0, 1], [0, 1], 'k--') plt.axis([0, 1, 0, 1]) plt.xlabel('False Positive Rate') plt.ylabel('True Negatove Rate') plot_roc_curve(fpr, tpr)
def lagger(): global cols, scores lag_counts = range(1, lags + 1) cols = [] scores = [] for lag in range(1, lags + 1): col = 'lag_{}'.format(lag) data[col] = np.sign(data['returns'].shift(lag)) cols.append(col) data.dropna(inplace=True) print('Iteration number: {}'.format(lag)) %time model.fit(data[cols], np.sign(data['returns'])) model.predict(data[cols]) data['prediction'] = model.predict(data[cols]) data['prediction'].value_counts() score = accuracy_score(data['prediction'], np.sign(data['returns'])) scores.append(score) plt.figure() plt.plot(lag_counts, scores, lw=2) plt.xlabel('# of Lags') plt.ylabel('Test Score') return scores, cols
def serve_css(name, length, keys, values): from pylab import plt, mpl mpl.rcParams['font.sans-serif'] = ['SimHei'] mpl.rcParams['axes.unicode_minus'] = False from matplotlib.font_manager import FontProperties # font = FontProperties(fname="d:\Users\ll.tong\Desktop\msyh.ttf", size=12) font = FontProperties(fname="/usr/share/fonts/msyh.ttf", size=11) plt.xlabel(u'') plt.ylabel(u'出现次数',fontproperties=font) plt.title(u'词频统计',fontproperties=font) plt.grid() keys = keys.decode("utf-8").split(' ') values = values.split(' ') valuesInt = [] for value in values: valuesInt.append(int(value)) plt.xticks(range(int(length)), keys) plt.plot(range(int(length)), valuesInt) plt.xticks(rotation=defaultrotation, fontsize=9,fontproperties=font) plt.yticks(fontsize=10,fontproperties=font) name = name + str(datetime.now().date()).replace(':', '') + '.png' imgUrl = 'static/temp/' + name fig = matplotlib.pyplot.gcf() fig.set_size_inches(12.2, 2) plt.savefig(imgUrl, bbox_inches='tight', figsize=(20,4), dpi=100) plt.close() tempfile = static_file(name, root='./static/temp/') #os.remove(imgUrl) return tempfile
def MakePlot(x, y, styles, labels, axlabels): plt.figure(figsize=(10, 6)) for i in range(len(x)): plt.plot(x[i], y[i], styles[i], label=labels[i]) plt.xlabel(axlabels[0]) plt.ylabel(axlabels[1]) plt.legend(loc=0)
def draw(cls, t_max, agents_proportions, eco_idx, parameters): color_set = ["green", "blue", "red"] for agent_type in range(3): plt.plot(np.arange(t_max), agents_proportions[:, agent_type], color=color_set[agent_type], linewidth=2.0, label="Type-{} agents".format(agent_type)) plt.ylim([-0.1, 1.1]) plt.xlabel("$t$") plt.ylabel("Proportion of indirect exchanges") # plt.suptitle('Direct choices proportion per type of agents', fontsize=14, fontweight='bold') plt.legend(loc='upper right', fontsize=12) print(parameters) plt.title( "Workforce: {}, {}, {}; displacement area: {}; vision area: {}; alpha: {}; tau: {}\n" .format(parameters["x0"], parameters["x1"], parameters["x2"], parameters["movement_area"], parameters["vision_area"], parameters["alpha"], parameters["tau"]), fontsize=12) if not path.exists("../../figures"): mkdir("../../figures") plt.savefig("../../figures/figure_{}.pdf".format(eco_idx)) plt.show()
def test_screenstate_1(self): from gdesk import gui from pylab import plt from pathlib import Path gui.load_layout('console') samplePath = Path(r'./samples') gui.img.select(1) gui.img.open(samplePath / 'kodim05.png') gui.img.zoom_fit() plt.plot(gui.vs.mean(2).mean(1)) plt.title('Column means of image 1') plt.xlabel('Column Number') plt.ylabel('Mean') plt.grid() plt.show() gui.img.select(2) gui.img.open(samplePath / 'kodim23.png') gui.img.zoom_full() plt.figure() plt.plot(gui.vs.mean(2).mean(0)) plt.title('Row means of image 2') plt.xlabel('Row Number') plt.ylabel('Mean') plt.grid() plt.show()
def link_level_bars(levels, usages, quantiles, scheme, direction, color, nnames, lnames, admat=None): """ Bar plots of nodes' link usage of links at different levels. """ if not admat: admat = np.genfromtxt('./settings/eadmat.txt') if color == 'solar': cmap = Oranges_cmap elif color == 'wind': cmap = Blues_cmap elif color == 'backup': cmap = 'Greys' nodes, links = usages.shape usageLevels = np.zeros((nodes, levels)) usageLevelsNorm = np.zeros((nodes, levels)) for node in range(nodes): nl = neighbor_levels(node, levels, admat) for lvl in range(levels): ll = link_level(nl, lvl, nnames, lnames) ll = np.array(ll, dtype='int') usageSum = sum(usages[node, ll]) linkSum = sum(quantiles[ll]) usageLevels[node, lvl] = usageSum / linkSum if lvl == 0: usageLevelsNorm[node, lvl] = usageSum else: usageLevelsNorm[node, lvl] = usageSum / usageLevelsNorm[node, 0] usageLevelsNorm[:, 0] = 1 # plot all nodes usages = usageLevels.transpose() plt.figure(figsize=(11, 3)) ax = plt.subplot() plt.pcolormesh(usages[:, loadOrder], cmap=cmap) plt.colorbar().set_label(label=r'$U_n^{(l)}$', size=11) ax.set_yticks(np.linspace(.5, levels - .5, levels)) ax.set_yticklabels(range(1, levels + 1)) ax.yaxis.set_tick_params(width=0) ax.xaxis.set_tick_params(width=0) ax.set_xticks(np.linspace(1, nodes, nodes)) ax.set_xticklabels(loadNames, rotation=60, ha="right", va="top", fontsize=10) plt.ylabel('Link level') plt.savefig(figPath + '/levels/' + str(scheme) + '/' + 'total' + '_' + str(direction) + '_' + color + '.pdf', bbox_inches='tight') plt.close() # plot all nodes normalised to usage of first level usages = usageLevelsNorm.transpose() plt.figure(figsize=(11, 3)) ax = plt.subplot() plt.pcolormesh(usages[:, loadOrder], cmap=cmap) plt.colorbar().set_label(label=r'$U_n^{(l)}$', size=11) ax.set_yticks(np.linspace(.5, levels - .5, levels)) ax.set_yticklabels(range(1, levels + 1)) ax.yaxis.set_tick_params(width=0) ax.xaxis.set_tick_params(width=0) ax.set_xticks(np.linspace(1, nodes, nodes)) ax.set_xticklabels(loadNames, rotation=60, ha="right", va="top", fontsize=10) plt.ylabel('Link level') plt.savefig(figPath + '/levels/' + str(scheme) + '/' + 'total_norm_cont_' + str(direction) + '_' + color + '.pdf', bbox_inches='tight') plt.close()
def hexbin_plot(self, var1, var2, force=False): fig_name = "{}/hexbin_{}_{}.pdf".format(self.fig_folder, var1, var2) if path.exists(fig_name) and not force: return if var1 == "customer_extra_view_choices" and var2 == "delta_position": print("Doing hexbin plot '{}' against '{}'.".format(var2, var1)) x = np.asarray(self.stats.data[var1]) y = np.asarray(self.stats.data[var2]) fig = plt.figure() ax = fig.add_subplot(1, 1, 1) plt.xlim(self.range_var[var1]) plt.ylim(self.range_var[var2]) plt.xlabel(self.format_label(var1)) plt.ylabel(self.format_label(var2)) hb = ax.hexbin(x=x, y=y, gridsize=20, cmap='inferno') ax.set_facecolor('black') cb = fig.colorbar(hb, ax=ax) cb.set_label('counts') plt.savefig(fig_name) if self.display: plt.show() plt.close()
def plot_comfort(fingers_org=range(1, 6, 1), fingers_dst=range(1, 6, 1), jumps=range(-12, 13, 1)): import seaborn from mpl_toolkits.mplot3d import Axes3D from pylab import plt xs, ys, zs, cs = calculate_comforts(fingers_org, fingers_dst, jumps) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(xs, ys, zs, c=cs) ax.set_zlabel("Interval (half steps)", fontsize=15) ax.set_zlim(jumps[0], jumps[-1]) # ax.set_zticks(jumps) plt.xticks(fingers_org) plt.xlim(fingers_org[0], fingers_org[-1]) plt.xlabel("From finger", fontsize=15) plt.yticks(fingers_dst) plt.ylim(fingers_dst[0], fingers_dst[-1]) plt.ylabel("To finger", fontsize=15) plt.title("Difficulty of finger passages", fontsize=25) plt.savefig('./figures/image.png', figsize=(16, 12), dpi=300) plt.show()
def plot_stat(rows, cache): "Use matplotlib to plot DAS statistics" if not PLOT_ALLOWED: raise Exception('Matplotlib is not available on the system') if cache in ['cache', 'merge']: # cachein, cacheout, mergein, mergeout name_in = '%sin' % cache name_out = '%sout' % cache else: # webip, webq, cliip, cliq name_in = '%sip' % cache name_out = '%sq' % cache def format_date(date): "Format given date" val = str(date) return '%s-%s-%s' % (val[:4], val[4:6], val[6:8]) date_range = [r['date'] for r in rows] formated_dates = [format_date(str(r['date'])) for r in rows] req_in = [r[name_in] for r in rows] req_out = [r[name_out] for r in rows] plt.plot(date_range, req_in , 'ro-',\ date_range, req_out, 'gv-',) plt.grid(True) plt.axis([min(date_range), max(date_range), \ 0, max([max(req_in), max(req_out)])]) plt.xticks(date_range, tuple(formated_dates), rotation=17) # plt.xlabel('dates [%s, %s]' % (date_range[0], date_range[-1])) plt.ylabel('DAS %s behavior' % cache) plt.savefig('das_%s.pdf' % cache, format='pdf', transparent=True) plt.close()
def example_filterbank(): from pylab import plt import numpy as np x = _create_impulse(2000) gfb = GammatoneFilterbank(density=1) analyse = gfb.analyze(x) imax, slopes = gfb.estimate_max_indices_and_slopes() fig, axs = plt.subplots(len(gfb.centerfrequencies), 1) for (band, state), imx, ax in zip(analyse, imax, axs): ax.plot(np.real(band)) ax.plot(np.imag(band)) ax.plot(np.abs(band)) ax.plot(imx, 0, 'o') ax.set_yticklabels([]) [ax.set_xticklabels([]) for ax in axs[:-1]] axs[0].set_title('Impulse responses of gammatone bands') fig, ax = plt.subplots() def plotfun(x, y): ax.semilogx(x, 20 * np.log10(np.abs(y)**2)) gfb.freqz(nfft=2 * 4096, plotfun=plotfun) plt.grid(True) plt.title('Absolute spectra of gammatone bands.') plt.xlabel('Normalized Frequency (log)') plt.ylabel('Attenuation /dB(FS)') plt.axis('Tight') plt.ylim([-90, 1]) plt.show() return gfb
def plot_stat(rows, cache): "Use matplotlib to plot DAS statistics" if not PLOT_ALLOWED: raise Exception('Matplotlib is not available on the system') if cache in ['cache', 'merge']: # cachein, cacheout, mergein, mergeout name_in = '%sin' % cache name_out = '%sout' % cache else: # webip, webq, cliip, cliq name_in = '%sip' % cache name_out = '%sq' % cache def format_date(date): "Format given date" val = str(date) return '%s-%s-%s' % (val[:4], val[4:6], val[6:8]) date_range = [r['date'] for r in rows] formated_dates = [format_date(str(r['date'])) for r in rows] req_in = [r[name_in] for r in rows] req_out = [r[name_out] for r in rows] plt.plot(date_range, req_in , 'ro-', date_range, req_out, 'gv-', ) plt.grid(True) plt.axis([min(date_range), max(date_range), \ 0, max([max(req_in), max(req_out)])]) plt.xticks(date_range, tuple(formated_dates), rotation=17) # plt.xlabel('dates [%s, %s]' % (date_range[0], date_range[-1])) plt.ylabel('DAS %s behavior' % cache) plt.savefig('das_%s.pdf' % cache, format='pdf', transparent=True) plt.close()
def square_root_diffusion_euler(): x0 = 0.25 kappa = 3.0 theta = 0.15 sigma = 0.1 I = 10000 M = 50 dt = T / M xh = np.zeros((M + 1, I)) x = np.zeros_like(xh) xh[0] = x0 x[0] = x0 for t in range(1, M + 1): xh[t] = (xh[t - 1] + kappa * (theta - np.maximum(xh[t - 1], 0)) * dt + sigma * np.sqrt(np.maximum(xh[t - 1], 0)) * math.sqrt(dt) * npr.standard_normal(I)) x = np.maximum(xh, 0) plt.figure(figsize=(10, 6)) plt.hist(x[-1], bins=50) plt.xlabel('value(SRT(T)') plt.ylabel('frequency') plt.show() plt.figure(figsize=(10, 6)) plt.plot(x[:, :100], lw=1.5) plt.xlabel('time') plt.ylabel('index level') plt.show() return x
def plot_zipf(*freq): ''' basic plotting using matplotlib and pylab ''' ranks, frequencies = [], [] langs, colors = [], [] langs = ["English", "German", "Finnish"] colors = ['#FF0000', '#00FF00', '#0000FF'] if bonus_part: colors.extend(['#00FFFF', '#FF00FF', '#FFFF00']) langs.extend(["English (Stemmed)", "German (Stemmed)", "Finnish (Stemmed)"]) plt.subplot(111) # 1, 1, 1 num = 6 if bonus_part else 3 for i in xrange(num): ranks.append(range(1, len(freq[i]) + 1)) frequencies.append([e[1] for e in freq[i]]) # log x and y axi, both with base 10 plt.loglog(ranks[i], frequencies[i], marker='', basex=10, color=colors[i], label=langs[i]) plt.legend() plt.grid(True) plt.title("Zipf's law!") plt.xlabel('Rank') plt.ylabel('Frequency') plt.show()
def jump_diffusion(): S0 = 100.0 r = 0.05 sigma = 0.2 lamb = 0.05 mu = -0.6 delta = 0.25 rj = lamb * (math.exp(mu + 0.5 * delta**2) - 1) T = 1.0 M = 50 I = 10000 dt = T / M S = np.zeros((M + 1, I)) S[0] = S0 sn1 = npr.standard_normal((M + 1, I)) sn2 = npr.standard_normal((M + 1, I)) poi = npr.poisson(lamb * dt, (M + 1, I)) for t in range(1, M + 1, 1): S[t] = S[t - 1] * (np.exp( (r - rj - 0.5 * sigma**2) * dt + sigma * math.sqrt(dt) * sn1[t]) + (np.exp(mu + delta * sn2[t]) - 1) * poi[t]) S[t] = np.maximum(S[t], 0) plt.figure(figsize=(10, 6)) plt.hist(S[-1], bins=50) plt.xlabel('value') plt.ylabel('frequency') plt.show() plt.figure(figsize=(10, 6)) plt.plot(S[:, :100], lw=1.) plt.xlabel('time') plt.ylabel('index level') plt.show()
def curve_plot(self, variable, t_max): print("Doing curve plot for variable '{}'.".format(variable)) var = Variable(name=variable) if var.data is None: self.extract_single_dimension(var, t_max=t_max) x = np.arange(t_max) mean = var.data["mean"] std = var.data["std"] plt.plot(x, mean, c='black', lw=2) plt.plot(x, mean + std, c='black', lw=.1) plt.plot(x, mean - std, c='black', lw=.1) plt.fill_between(x, mean + std, mean - std, color='black', alpha=.1) plt.xlabel("t") plt.ylabel(self.format_label(variable)) plt.savefig("{}/curve_plot_{}.pdf".format(self.fig_folder, variable)) if self.display: plt.show() plt.close()
def draw(cls, t_max, agents_proportions, eco_idx, parameters): color_set = ["green", "blue", "red"] for agent_type in range(3): plt.plot(np.arange(t_max), agents_proportions[:, agent_type], color=color_set[agent_type], linewidth=2.0, label="Type-{} agents".format(agent_type)) plt.ylim([-0.1, 1.1]) plt.xlabel("$t$") plt.ylabel("Proportion of indirect exchanges") # plt.suptitle('Direct choices proportion per type of agents', fontsize=14, fontweight='bold') plt.legend(loc='upper right', fontsize=12) print(parameters) plt.title( "Workforce: {}, {}, {}; displacement area: {}; vision area: {}; alpha: {}; tau: {}\n" .format( parameters["x0"], parameters["x1"], parameters["x2"], parameters["movement_area"], parameters["vision_area"], parameters["alpha"], parameters["tau"] ), fontsize=12) if not path.exists("../../figures"): mkdir("../../figures") plt.savefig("../../figures/figure_{}.pdf".format(eco_idx)) plt.show()
def hexbin_plot(self, var1, var2): print("Doing hexbin plot '{}' against '{}'.".format(var2, var1)) x = np.asarray(self.stats.data[var1]) y = np.asarray(self.stats.data[var2]) fig = plt.figure() ax = fig.add_subplot(1, 1, 1) plt.xlim(self.range_var[var1]) plt.ylim(self.range_var[var2]) plt.xlabel(self.format_label(var1)) plt.ylabel(self.format_label(var2)) hb = ax.hexbin(x=x, y=y, gridsize=20, cmap='inferno') ax.set_facecolor('black') cb = fig.colorbar(hb, ax=ax) cb.set_label('counts') plt.savefig("{}/hexbin_{}_{}.pdf".format(self.fig_folder, var1, var2)) if self.display: plt.show() plt.close()
def example_filterbank(): from pylab import plt import numpy as np x = _create_impulse(2000) gfb = GammatoneFilterbank(density=1) analyse = gfb.analyze(x) imax, slopes = gfb.estimate_max_indices_and_slopes() fig, axs = plt.subplots(len(gfb.centerfrequencies), 1) for (band, state), imx, ax in zip(analyse, imax, axs): ax.plot(np.real(band)) ax.plot(np.imag(band)) ax.plot(np.abs(band)) ax.plot(imx, 0, 'o') ax.set_yticklabels([]) [ax.set_xticklabels([]) for ax in axs[:-1]] axs[0].set_title('Impulse responses of gammatone bands') fig, ax = plt.subplots() def plotfun(x, y): ax.semilogx(x, 20*np.log10(np.abs(y)**2)) gfb.freqz(nfft=2*4096, plotfun=plotfun) plt.grid(True) plt.title('Absolute spectra of gammatone bands.') plt.xlabel('Normalized Frequency (log)') plt.ylabel('Attenuation /dB(FS)') plt.axis('Tight') plt.ylim([-90, 1]) plt.show() return gfb
def plot_weightings(): """Plots all weighting functions defined in :module: splweighting.""" from scipy.signal import freqz from pylab import plt, np sample_rate = 48000 num_samples = 2*4096 fig, ax = plt.subplots() for name, weight_design in sorted( _weighting_coeff_design_funsd.items()): b, a = weight_design(sample_rate) w, H = freqz(b, a, worN=num_samples) freq = w*sample_rate / (2*np.pi) ax.semilogx(freq, 20*np.log10(np.abs(H)+1e-20), label='{}-Weighting'.format(name)) plt.legend(loc='lower right') plt.xlabel('Frequency / Hz') plt.ylabel('Damping / dB') plt.grid(True) plt.axis([10, 20000, -80, 5]) return fig, ax
def generate_start_time_figures(self): recording_time_grouped_by_patient = self.pain_data[["PatientID", "NRSTimeFromEndSurgery_mins"]].groupby("PatientID") recording_start_minutes = recording_time_grouped_by_patient.min() fig1 = "fig1.pdf" fig2 = "fig2.pdf" plt.figure(figsize=[8,4]) plt.title("Pain score recording start times", fontsize=14).set_y(1.05) plt.ylabel("Occurrences", fontsize=14) plt.xlabel("Recording Start Time (minutes)", fontsize=14) plt.hist(recording_start_minutes.values, bins=20, color="0.5") plt.savefig(os.path.join(self.tmp_directory, fig1), bbox_inches="tight") plt.figure(figsize=[8,4]) plt.title("Pain score recording start times, log scale", fontsize=14).set_y(1.05) plt.ylabel("Occurrences", fontsize=14) plt.xlabel("Recording Start Time (minutes)", fontsize=14) plt.hist(recording_start_minutes.values, bins=20, log=True, color="0.5") plt.savefig(os.path.join(self.tmp_directory, fig2), bbox_inches="tight") #save the figures in panel format f = open(os.path.join(self.tmp_directory, "tmp.tex"), 'w') f.write(r""" \documentclass[% ,float=false % this is the new default and can be left away. ,preview=true ,class=scrartcl ,fontsize=20pt ]{standalone} \usepackage[active,tightpage]{preview} \usepackage{varwidth} \usepackage{graphicx} \usepackage[justification=centering]{caption} \usepackage{subcaption} \usepackage[caption=false,font=footnotesize]{subfig} \renewcommand{\thesubfigure}{\Alph{subfigure}} \begin{document} \begin{preview} \begin{figure}[h] \begin{subfigure}{0.5\textwidth} \includegraphics[width=\textwidth]{""" + fig1 + r"""} \caption{Normal scale} \end{subfigure}\begin{subfigure}{0.5\textwidth} \includegraphics[width=\textwidth]{""" + fig2 + r"""} \caption{Log scale} \end{subfigure} \end{figure} \end{preview} \end{document} """) f.close() subprocess.call(["pdflatex", "-halt-on-error", "-output-directory", self.tmp_directory, os.path.join(self.tmp_directory, "tmp.tex")]) shutil.move(os.path.join(self.tmp_directory, "tmp.pdf"), os.path.join(self.output_directory, "pain_score_start_times.pdf"))
def plot_charts(self): print(self.final_portfolio_valuation) plt.figure(figsize=(10, 6)) plt.hist( self.final_portfolio_valuation, bins=100) plt.title("Final Exit Valuation complete Portfolio after {} year as Geometric Brownian Motion".format(self.max_year)) plt.xlabel('Exit Valuation') plt.ylabel('frequency'); plt.show()
def plot(self, new_plot=False, xlim=None, ylim=None, title=None, figsize=None, xlabel=None, ylabel=None, fontsize=None, show_legend=True, grid=True): """ Plot data using matplotlib library. Use show() method for matplotlib to see result or :: %pylab inline in IPython to see plot as cell output. :param bool new_plot: create or not new figure :param xlim: x-axis range :param ylim: y-axis range :type xlim: None or tuple(x_min, x_max) :type ylim: None or tuple(y_min, y_max) :param title: title :type title: None or str :param figsize: figure size :type figsize: None or tuple(weight, height) :param xlabel: x-axis name :type xlabel: None or str :param ylabel: y-axis name :type ylabel: None or str :param fontsize: font size :type fontsize: None or int :param bool show_legend: show or not labels for plots :param bool grid: show grid or not """ xlabel = self.xlabel if xlabel is None else xlabel ylabel = self.ylabel if ylabel is None else ylabel figsize = self.figsize if figsize is None else figsize fontsize = self.fontsize if fontsize is None else fontsize self.fontsize_ = fontsize self.show_legend_ = show_legend title = self.title if title is None else title xlim = self.xlim if xlim is None else xlim ylim = self.ylim if ylim is None else ylim new_plot = self.new_plot or new_plot if new_plot: plt.figure(figsize=figsize) plt.xlabel(xlabel, fontsize=fontsize) plt.ylabel(ylabel, fontsize=fontsize) plt.title(title, fontsize=fontsize) plt.tick_params(axis='both', labelsize=fontsize) plt.grid(grid) if xlim is not None: plt.xlim(xlim) if ylim is not None: plt.ylim(ylim) self._plot() if show_legend: plt.legend(loc='best', scatterpoints=1)
def create_plot(x, y, styles, labels, axlabels): plt.figure(figsize=(10, 6)) plt.scatter(x[0], y[0]) plt.scatter(x[1], y[1]) plt.xlabel(axlabels[0]) plt.ylabel(axlabels[1]) plt.legend(loc=0) plt.show()
def convert_all_to_png(vis_path, out_dir="maps_png", size=None): units = { 'gas_density': 'Gas Density [g/cm$^3$]', 'Tm': 'Temperature [K]', 'Tew': 'Temperature [K]', 'S': 'Entropy []', 'dm': 'DM Density [g/cm$^3$]', 'v': 'Velocity [km/s]' } log_list = ['gas_density'] for vis_file in os.listdir(vis_path): if ".dat" not in vis_file: continue print "converting %s" % vis_file map_type = re.search('sigma_(.*)_[xyz]', vis_file).group(1) (image, pixel_size, axis_values) = read_visualization_data(vis_path + "/" + vis_file, size) print "image width in Mpc/h: ", axis_values[-1] * 2.0 x, y = np.meshgrid(axis_values, axis_values) cmap_max = image.max() cmap_min = image.min() ''' plotting ''' plt.figure(figsize=(5, 4)) if map_type in log_list: plt.pcolor(x, y, image, norm=LogNorm(vmax=cmap_max, vmin=cmap_min)) else: plt.pcolor(x, y, image, vmax=cmap_max, vmin=cmap_min) cbar = plt.colorbar() if map_type in units.keys(): cbar.ax.set_ylabel(units[map_type]) plt.axis( [axis_values[0], axis_values[-1], axis_values[0], axis_values[-1]]) del image plt.xlabel(r"$Mpc/h$", fontsize=18) plt.ylabel(r"$Mpc/h$", fontsize=18) out_file = vis_file.replace("dat", "png") plt.savefig(out_dir + "/" + out_file, dpi=150) plt.close() plt.clf()
def plot_treward(agent): ''' Function to plot the total reward per training eposiode. ''' plt.figure(figsize=(10, 6)) x = range(1, len(agent.averages) + 1) y = np.polyval(np.polyfit(x, agent.averages, deg=3), x) plt.plot(x, agent.averages, label='moving average') plt.plot(x, y, 'r--', label='regression') plt.xlabel('episodes') plt.ylabel('total reward') plt.legend()
def plot_smoothed_alpha_comparison(self,rmsval,suffix=''): plt.plot(self.f,self.alpha,'ko',label='data set') plt.plot(self.f,self.salpha,'c-',lw=2,label='smoothed angle $\phi$') plt.xlabel('frequency in Hz') plt.ylabel('angle $\phi$ in coordinates of circle') plt.legend() ylims=plt.axes().get_ylim() plt.yticks((arange(9)-4)*0.5*pi, ['$-2\pi$','$-3\pi/2$','$-\pi$','$-\pi/2$','$0$','$\pi/2$','$\pi$','$3\pi/2$','$2\pi$']) plt.ylim(ylims) plt.title('RMS offset from smooth curve: {:.4f}'.format(rmsval)) if self.show: plt.show() else: plt.savefig(join(self.sdc.plotpath,'salpha','c{}_salpha_on_{}_circle'.format(self.sdc.case,self.ZorY)+self.sdc.suffix+self.sdc.outsuffix+suffix+'.png'), dpi=240) plt.close()
def convert_all_to_png(vis_path, out_dir = "maps_png", size = None) : units = { 'gas_density' : 'Gas Density [g/cm$^3$]', 'Tm' : 'Temperature [K]', 'Tew' : 'Temperature [K]', 'S' : 'Entropy []', 'dm' : 'DM Density [g/cm$^3$]', 'v' : 'Velocity [km/s]' } log_list = ['gas_density'] for vis_file in os.listdir(vis_path) : if ".dat" not in vis_file : continue print "converting %s" % vis_file map_type = re.search('sigma_(.*)_[xyz]', vis_file).group(1) (image, pixel_size, axis_values) = read_visualization_data(vis_path+"/"+vis_file, size) print "image width in Mpc/h: ", axis_values[-1]*2.0 x, y = np.meshgrid( axis_values, axis_values ) cmap_max = image.max() cmap_min = image.min() ''' plotting ''' plt.figure(figsize=(5,4)) if map_type in log_list: plt.pcolor(x,y,image, norm=LogNorm(vmax=cmap_max, vmin=cmap_min)) else : plt.pcolor(x,y,image, vmax=cmap_max, vmin=cmap_min) cbar = plt.colorbar() if map_type in units.keys() : cbar.ax.set_ylabel(units[map_type]) plt.axis([axis_values[0], axis_values[-1],axis_values[0], axis_values[-1]]) del image plt.xlabel(r"$Mpc/h$", fontsize=18) plt.ylabel(r"$Mpc/h$", fontsize=18) out_file = vis_file.replace("dat", "png") plt.savefig(out_dir+"/"+out_file, dpi=150 ) plt.close() plt.clf()
def plot_fault_framework(fault_framework): fm = fault_framework plt.plot(fm.Y_PC, fm.DEP, '-o') plt.axis('equal') plt.axhline(0, color='black') plt.gca().set_yticks(fm.DEP) plt.gca().set_xticks(fm.Y_PC) plt.grid('on') plt.xlabel('From trench to continent(km)') plt.ylabel('depth (km)') for xi, yi, dip in zip(fm.Y_PC, fm.DEP, fm.DIP_D): plt.text(xi, yi, 'dip = %.1f'%dip) plt.gca().invert_yaxis()
def plot_overview(self,suffix=''): x=self.x; y=self.y; r=self.radius; cx,cy=self.center.real,self.center.imag ax=plt.axes() plt.scatter(x,y, marker='o', c='b', s=40) plt.axhline(y=0,color='grey', zorder=-1) plt.axvline(x=0,color='grey', zorder=-2) t=linspace(0,2*pi,201) circx=r*cos(t) + cx circy=r*sin(t) + cy plt.plot(circx,circy,'g-') plt.plot([cx],[cy],'gx',ms=12) if self.ZorY == 'Z': philist,flist=[self.phi_a,self.phi_p,self.phi_n],[self.fa,self.fp,self.fn] elif self.ZorY == 'Y': philist,flist=[self.phi_m,self.phi_s,self.phi_r],[self.fm,self.fs,self.fr] for p,f in zip(philist,flist): if f is not None: xpos=cx+r*cos(p); ypos=cy+r*sin(p); xos=0.2*(xpos-cx); yos=0.2*(ypos-cy) plt.plot([0,xpos],[0,ypos],'co-') ax.annotate('{:.3f} Hz'.format(f), xy=(xpos,ypos), xycoords='data', xytext=(xpos+xos,ypos+yos), textcoords='data', #textcoords='offset points', arrowprops=dict(arrowstyle="->", shrinkA=0, shrinkB=10) ) #plt.xlim(0,0.16) #plt.ylim(-0.1,0.1) plt.axis('equal') if self.ZorY == 'Z': plt.xlabel(r'resistance $R$ in Ohm'); plt.ylabel(r'reactance $X$ in Ohm') if self.ZorY == 'Y': plt.xlabel(r'conductance $G$ in Siemens'); plt.ylabel(r'susceptance $B$ in Siemens') plt.title("fitting the admittance circle with Powell's method") tx1='best fit (fmin_powell):\n' tx1+='center at G+iB = {:.5f} + i*{:.8f}\n'.format(cx,cy) tx1+='radius = {:.5f}; '.format(r) tx1+='residue: {:.2e}'.format(self.resid) txt1=plt.text(-r,cy-1.1*r,tx1,fontsize=8,ha='left',va='top') txt1.set_bbox(dict(facecolor='gray', alpha=0.25)) idxlist=self.to_be_annotated('triple') ofs=self.annotation_offsets(idxlist,factor=0.1,xshift=0.15) for i,j in enumerate(idxlist): xpos,ypos = x[j],y[j]; xos,yos = ofs[i].real,ofs[i].imag ax.annotate('{:.1f} Hz'.format(self.f[j]), xy=(xpos,ypos), xycoords='data', xytext=(xpos+xos,ypos+yos), textcoords='data', #textcoords='offset points', arrowprops=dict(arrowstyle="->", shrinkA=0, shrinkB=10) ) if self.show: plt.show() else: plt.savefig(join(self.sdc.plotpath,'c{}_fitted_{}_circle'.format(self.sdc.case,self.ZorY)+suffix+'.png'), dpi=240) plt.close()
def update_img((expected, output)): plt.cla() plt.ylim((vmin, vmin+vmax)) plt.xlim((vmin, vmin+vmax)) ax = fig.add_subplot(111) plt.plot([vmin, vmin+vmax], [vmin, vmin+vmax]) ax.grid(True) plt.xlabel("expected output") plt.ylabel("network output") plt.legend() expected = expected*vmax + vmin output = output*vmax + vmin #scat.set_offsets((expected, output)) scat = ax.scatter(expected, output) return scat
def test_dep(self): xf = arange(0, 425) deps = self.fm.get_dep(xf) plt.plot(xf,deps) plt.gca().set_yticks(self.fm.DEP) plt.gca().set_xticks(self.fm.Y_PC) plt.grid('on') plt.title('Ground x versus depth') plt.xlabel('Ground X (km)') plt.ylabel('depth (km)') plt.axis('equal') plt.gca().invert_yaxis() plt.savefig(join(self.outs_dir, '~Y_PC_vs_deps.png')) plt.close()
def start_plot(self,w=1.3,connect=False): self.fig=plt.figure() self.ax=plt.axes() plt.axhline(y=0,color='grey', zorder=-1) plt.axvline(x=0,color='grey', zorder=-2) self.plot_data(connect=connect) #plt.axis('equal') self.ax.set_aspect('equal', 'datalim') if self.center is not None: cx,cy=self.center.real,self.center.imag; r=self.radius self.ax.axis([cx-w*r,cx+w*r,cy-w*r,cy+w*r]) else: xmx=amax(self.x); ymn,ymx=amin(self.y),amax(self.y) cx=0.5*xmx; cy=0.5*(ymn+ymx); r=0.5*(ymx-ymn) self.ax.axis([cx-w*r,cx+w*r,cy-w*r,cy+w*r]) if self.ZorY == 'Z': plt.xlabel(r'resistance $R$ in Ohm'); plt.ylabel(r'reactance $X$ in Ohm') if self.ZorY == 'Y': plt.xlabel(r'conductance $G$ in Siemens'); plt.ylabel(r'susceptance $B$ in Siemens')
def _plot_base(dep, val, deplim_small, xlim_small, xlabel): plt.subplot(1,2,1) plt.plot(val, dep) plt.gca().invert_yaxis() plt.grid('on') plt.ylabel('depth/km') plt.xlabel(xlabel) locs, labels = plt.xticks() plt.setp(labels, rotation=-45) plt.subplot(1,2,2) plt.plot(val, dep) plt.gca().invert_yaxis() plt.grid('on') plt.ylim(deplim_small) plt.xlim(xlim_small) plt.xlabel(xlabel) locs, labels = plt.xticks() plt.setp(labels, rotation=-45)
def plotter(mode,Bc,Tc,Q): col = ['#000080','#0000FF','#4169E1','#6495ED','#00BFFF','#B0E0E6'] plt.figure() ax = plt.subplot(111) for p in range(Bc.shape[1]): plt.plot(Tc[:,p],Bc[:,p],'-',color=str(col[p])) plt.xlabel('Tc [TW]') plt.ylabel('Bc normalised to total EU load') plt.title(str(mode)+' flow') # Shrink current axis by 25% to make room for legend box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.75, box.height]) plt.legend(\ ([str(Q[i]*100) for i in range(len(Q))]),\ loc='center left', bbox_to_anchor=(1, 0.5),title='Quantiles') plt.savefig('figures/bctc_'+str(mode)+'.eps')
def freqz(sosmat, nsamples=44100, sample_rate=44100, plot=True): """Plots Frequency response of sosmat.""" from pylab import np, plt, fft, fftfreq x = np.zeros(nsamples) x[nsamples/2] = 0.999 y, states = sosfilter_double_c(x, sosmat) Y = fft(y) f = fftfreq(len(x), 1.0/sample_rate) if plot: plt.grid(True) plt.axis([0, sample_rate / 2, -100, 5]) L = 20*np.log10(np.abs(Y[:len(x)/2]) + 1e-17) plt.semilogx(f[:len(x)/2], L, lw=0.5) plt.hold(True) plt.title('freqz sos filter') plt.xlabel('Frequency / Hz') plt.ylabel('Damping /dB(FS)') plt.xlim((10, sample_rate/2)) plt.hold(False) return x, y, f, Y
def plot_vel_decomposition(self, site, cmpt, loc=0, leg_fs=7, if_ylim=False ): y = self.plot_pred_vel_added(site, cmpt, label='total') y += self.plot_vel_R_co(site, cmpt, style='-^', label='Rco', color='orange') y += self.plot_vel_E_cumu_slip(site, cmpt, color='green') y += self.plot_vel_R_aslip(site, cmpt, color='black') plt.grid('on') if if_ylim: plt.ylim(calculate_lim(y)) plt.ylabel(r'mm/yr') plt.legend(loc=loc, prop={'size':leg_fs}) plt.gcf().autofmt_xdate() plt.title('Cumulative Disp.: {site} - {cmpt}'.format( site = get_site_true_name(site_id=site), cmpt = cmpt ))
def plot_post_disp_decomposition(self, site, cmpt, loc=2, leg_fs=7, added_label = None, marker_for_obs = 'x', ): y = self.plot_post_obs_linres(site,cmpt, label='obs.', marker=marker_for_obs) y += self.plot_post_disp_pred_from_result_file(site,cmpt, label='pred.') y += self.plot_R_co(site, cmpt, style = '-^', label='Rco', color='orange') y += self.plot_E_aslip(site, cmpt, color='green') plt.grid('on') self.plot_post_disp_pred_added(site, cmpt, label=added_label) plt.legend(loc=loc, prop={'size':leg_fs}) plt.ylabel(r'm') plt.gcf().autofmt_xdate() plt.title('Postseismic Disp. : {site} - {cmpt}'.format( site = get_site_true_name(site_id = site), cmpt = cmpt ))
def plot_cumu_disp_decomposition(self, site, cmpt, loc=2, leg_fs=7, if_ylim=False, added_label = None, ): self.plot_cumu_obs_linres(site, cmpt) y = self.plot_cumu_disp_pred_from_result_file(site, cmpt, label='pred.') y += self.plot_R_co(site, cmpt, style='-^', label='Rco', color='orange') y += self.plot_E_cumu_slip(site, cmpt, color='green') plt.grid('on') if if_ylim: plt.ylim(calculate_lim(y)) self.plot_cumu_disp_pred_added(site, cmpt, label=added_label) plt.ylabel(r'm') plt.legend(loc=loc, prop={'size':leg_fs}) plt.gcf().autofmt_xdate() plt.title('Cumulative Disp.: {site} - {cmpt}'.format( site = get_site_true_name(site_id=site), cmpt = cmpt ))
def plotLive(combine_Type, combine_Name, lat_Name, long_Name, massFlow_Name, filename): data = pd.read_csv(filename) if combine_Type != 0: comb_df = data[data[combine_Name] == combine_Type] lat_df = comb_df[lat_Name] lon_df = comb_df[long_Name] y = comb_df[massFlow_Name] else: lat_df = data[lat_Name] lon_df = data[long_Name] y = data[massFlow_Name] e,n = convertToUTM(lat_df, lon_df) def makeFig(): plt.plot(x,y) plt.ylabel('Easting') plt.xlabel('Northing') plt.ion() # enable interactivity plt.grid() fig = plt.figure() # make a figure x=list() y=list() for i in arange(len(n)): x.append(n[i]) y.append(e[i]) i+=1 drawnow(makeFig)
def plot_L_curve(files, nlin_pars = ['log10_He_','log10_visM_','rake'], nlin_pars_ylabels = [r'$log_{10}(He)$', r'$log_{10}(visM)$', 'rake'], ): nreses = collect_from_result_files(files, 'residual_norm_weighted') nroughs = collect_from_result_files(files, 'roughening_norm') num_subplots = 1 + len(nlin_pars) x1 = amin(nreses) x2 = amax(nreses) dx = x2 - x1 xlim = (x1-dx*0.02, x2+dx*0.2) xticks = range(int(x1), int(x2),5) plt.subplot(num_subplots,1,1) plt.loglog(nreses, nroughs,'o-') plt.xlim(xlim) plt.gca().set_xticks(xticks) plt.gca().get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter()) plt.ylabel('roughening') plt.xlabel('Residual Norm') plt.grid('on') nth = 2 for par, par_label in zip(nlin_pars, nlin_pars_ylabels): y = collect_from_result_files(files, par) plt.subplot(num_subplots,1,nth) plt.semilogx(nreses, y,'o-') plt.xlim(xlim) plt.gca().set_xticks(xticks) plt.gca().get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter()) plt.ylabel(par_label) plt.xlabel('Residual Norm') plt.grid('on') nth += 1
import h5py from pylab import plt def collect_results(outs_files, key): outs = [] for file in outs_files: with h5py.File(file, 'r') as fid: out = fid[key][...] outs.append(out) return outs files = sorted(glob.glob('../outs/ano_??.h5')) nrough1 = collect_results(files, 'regularization/roughening/norm') nres1 = collect_results(files, 'misfit/norm_weighted') files = sorted(glob.glob('../../run0/outs/ano_??.h5')) nrough0 = collect_results(files, 'regularization/roughening/norm') nres0 = collect_results(files, 'misfit/norm_weighted') plt.loglog(nres0, nrough0, '.', label='Result0') plt.loglog(nres1, nrough1, '.', label='Result1') plt.grid('on') plt.xlabel('norm of weighted residual') plt.ylabel('norm of solution roughness') plt.xlim([.7,5]) plt.legend() plt.savefig('compare_misfit.png') plt.show()
Fv = [] # For each frequency bin, estimate the stats t_init = time() for t in time_bins: covmats = covest.fit_transform(epochs_data[:, ::1, t:(t+window)]) p_test = PermutationDistance(1000, metric='riemann', mode='pairwise') p, F = p_test.test(covmats, labels, verbose=False) pv.append(p) Fv.append(F[0]) duration = time() - t_init # plot result fig, axes = plt.subplots(1, 1, figsize=[6, 3], sharey=True) sig = 0.05 times = np.array(time_bins)/float(Fs) + tmin axes.plot(times, Fv, lw=2, c='k') plt.xlabel('Time (sec)') plt.ylabel('Score') a = np.where(np.diff(np.array(pv) < sig))[0] a = a.reshape(int(len(a)/2), 2) st = (times[1] - times[0])/2.0 for p in a: axes.axvspan(times[p[0]]-st, times[p[1]]+st, facecolor='g', alpha=0.5) axes.legend(['Score', 'p<%.2f' % sig]) axes.set_title('Pairwise distance - %.1f sec.' % duration) sns.despine() plt.tight_layout() plt.show()
import numpy as np from pylab import plt import h5py from epochs import epochs def read_inland_rms_from_files(files): rms = [] for file in files: with h5py.File(file) as fid: rms.append(fid['misfit/rms_inland'][...]) return np.asarray(rms) nrough = 10 files = sorted(glob.glob('../outs/epoch_????_rough_%02d.h5'%nrough)) rms = read_inland_rms_from_files(files) plt.plot(epochs, rms*100., 'o-', label='RMS (afterslip only model)') with open('rms_deconv.pkl','rb') as fid: t,y = pickle.load(fid) plt.plot(t, np.asarray(y)*100., 'o-', label='RMS (afterslip + viscoelastic relax.)') plt.xlabel('days after the mainshock') plt.ylabel('RMS misfit (cm)') plt.grid('on') plt.title('Slip only RMS misfit') plt.legend(loc=0) plt.savefig('rms_static_vs_deconv.png') plt.show()
Hes.append(m[-2]) rakes.append(m[-1]) nrough = fid['regularization/roughening/norm'][...] nroughs.append(nrough) xlim = (7, 22) xlim = None xticks = range(7,22) plt.subplot(411) plt.semilogx(nreses, visMs,'o') plt.xlim(xlim) plt.gca().set_xticks(xticks) plt.grid('on') plt.ylabel('log10(visM/(Pa.s))') plt.subplot(412) plt.semilogx(nreses, Hes,'o') plt.xlim(xlim) plt.gca().set_xticks(xticks) plt.grid('on') plt.ylabel('He/km') plt.subplot(413) plt.semilogx(nreses, rakes,'o') plt.xlim(xlim) plt.gca().set_xticks(xticks) plt.ylabel('rake') plt.grid('on')
def get_linear_model_histogramDouble(code, ptype='f', dtype='d', start=None, end=None, vtype='close', filter='n', df=None): # 399001','cyb':'zs399006','zxb':'zs399005 # code = '999999' # code = '601608' # code = '000002' # asset = ts.get_hist_data(code)['close'].sort_index(ascending=True) # df = tdd.get_tdx_Exp_day_to_df(code, 'f').sort_index(ascending=True) # vtype='close' # if vtype == 'close' or vtype=='' # ptype= if start is not None and filter == 'y': if code not in ['999999', '399006', '399001']: index_d, dl = tdd.get_duration_Index_date(dt=start) log.debug("index_d:%s dl:%s" % (str(index_d), dl)) else: index_d = cct.day8_to_day10(start) log.debug("index_d:%s" % (index_d)) start = tdd.get_duration_price_date(code, ptype='low', dt=index_d) log.debug("start:%s" % (start)) if df is None: # df = tdd.get_tdx_append_now_df(code, ptype, start, end).sort_index(ascending=True) df = tdd.get_tdx_append_now_df_api(code, ptype, start, end).sort_index(ascending=True) if not dtype == 'd': df = tdd.get_tdx_stock_period_to_type(df, dtype).sort_index(ascending=True) asset = df[vtype] log.info("df:%s" % asset[:1]) asset = asset.dropna() dates = asset.index if not code.startswith('999') or not code.startswith('399'): if code[:1] in ['5', '6', '9']: code2 = '999999' elif code[:1] in ['3']: code2 = '399006' else: code2 = '399001' df1 = tdd.get_tdx_append_now_df_api(code2, ptype, start, end).sort_index(ascending=True) # df1 = tdd.get_tdx_append_now_df(code2, ptype, start, end).sort_index(ascending=True) if not dtype == 'd': df1 = tdd.get_tdx_stock_period_to_type(df1, dtype).sort_index(ascending=True) asset1 = df1.loc[asset.index, vtype] startv = asset1[:1] # asset_v=asset[:1] # print startv,asset_v asset1 = asset1.apply(lambda x: round(x / asset1[:1], 2)) # print asset1[:4] # 画出价格随时间变化的图像 # _, ax = plt.subplots() # fig = plt.figure() fig = plt.figure(figsize=(16, 10)) # fig = plt.figure(figsize=(16, 10), dpi=72) # fig.autofmt_xdate() #(no fact) # plt.subplots_adjust(bottom=0.1, right=0.8, top=0.9) plt.subplots_adjust(left=0.05, bottom=0.08, right=0.95, top=0.95, wspace=0.15, hspace=0.25) # set (gca,'Position',[0,0,512,512]) # fig.set_size_inches(18.5, 10.5) # fig=plt.fig(figsize=(14,8)) ax1 = fig.add_subplot(321) # asset=asset.apply(lambda x:round( x/asset[:1],2)) ax1.plot(asset) # ax1.plot(asset1,'-r', linewidth=2) ticks = ax1.get_xticks() # start, end = ax1.get_xlim() # print start, end, len(asset) # print ticks, ticks[:-1] # (ticks[:-1] if len(asset) > end else np.append(ticks[:-1], len(asset) - 1)) ax1.set_xticklabels([dates[i] for i in (np.append(ticks[:-1], len(asset) - 1))], rotation=15) # Label x-axis with dates # 拟合 X = np.arange(len(asset)) x = sm.add_constant(X) model = regression.linear_model.OLS(asset, x).fit() a = model.params[0] b = model.params[1] # log.info("a:%s b:%s" % (a, b)) log.info("X:%s a:%s b:%s" % (len(asset), a, b)) Y_hat = X * b + a # 真实值-拟合值,差值最大最小作为价值波动区间 # 向下平移 i = (asset.values.T - Y_hat).argmin() c_low = X[i] * b + a - asset.values[i] Y_hatlow = X * b + a - c_low # 向上平移 i = (asset.values.T - Y_hat).argmax() c_high = X[i] * b + a - asset.values[i] Y_hathigh = X * b + a - c_high plt.plot(X, Y_hat, 'k', alpha=0.9); plt.plot(X, Y_hatlow, 'r', alpha=0.9); plt.plot(X, Y_hathigh, 'r', alpha=0.9); # plt.xlabel('Date', fontsize=12) plt.ylabel('Price', fontsize=12) plt.title(code + " | " + str(dates[-1])[:11], fontsize=14) plt.legend([asset.iat[-1]], fontsize=12, loc=4) plt.grid(True) # plt.legend([code]); # plt.legend([code, 'Value center line', 'Value interval line']); # fig=plt.fig() # fig.figsize = [14,8] scale = 1.1 zp = zoompan.ZoomPan() figZoom = zp.zoom_factory(ax1, base_scale=scale) figPan = zp.pan_factory(ax1) ax2 = fig.add_subplot(323) # ax2.plot(asset) # ticks = ax2.get_xticks() ax2.set_xticklabels([dates[i] for i in (np.append(ticks[:-1], len(asset) - 1))], rotation=15) # plt.plot(X, Y_hat, 'k', alpha=0.9) n = 5 d = (-c_high + c_low) / n c = c_high while c <= c_low: Y = X * b + a - c plt.plot(X, Y, 'r', alpha=0.9); c = c + d # asset=asset.apply(lambda x:round(x/asset[:1],2)) ax2.plot(asset) # ax2.plot(asset1,'-r', linewidth=2) # plt.xlabel('Date', fontsize=12) plt.ylabel('Price', fontsize=12) plt.grid(True) # plt.title(code, fontsize=14) # plt.legend([code]) # 将Y-Y_hat股价偏离中枢线的距离单画出一张图显示,对其边界线之间的区域进行均分,大于0的区间为高估,小于0的区间为低估,0为价值中枢线。 ax3 = fig.add_subplot(322) # distance = (asset.values.T - Y_hat) distance = (asset.values.T - Y_hat)[0] if code.startswith('999') or code.startswith('399'): ax3.plot(asset) plt.plot(distance) ticks = ax3.get_xticks() ax3.set_xticklabels([dates[i] for i in (np.append(ticks[:-1], len(asset) - 1))], rotation=15) n = 5 d = (-c_high + c_low) / n c = c_high while c <= c_low: Y = X * b + a - c plt.plot(X, Y - Y_hat, 'r', alpha=0.9); c = c + d ax3.plot(asset) # plt.xlabel('Date', fontsize=12) plt.ylabel('Price-center price', fontsize=14) plt.grid(True) else: as3 = asset.apply(lambda x: round(x / asset[:1], 2)) ax3.plot(as3) ax3.plot(asset1, '-r', linewidth=2) plt.grid(True) zp3 = zoompan.ZoomPan() figZoom = zp3.zoom_factory(ax3, base_scale=scale) figPan = zp3.pan_factory(ax3) # plt.title(code, fontsize=14) # plt.legend([code]) # 统计出每个区域内各股价的频数,得到直方图,为了更精细的显示各个区域的频数,这里将整个边界区间分成100份。 ax4 = fig.add_subplot(325) log.info("assert:len:%s %s" % (len(asset.values.T - Y_hat), (asset.values.T - Y_hat)[0])) # distance = map(lambda x:int(x),(asset.values.T - Y_hat)/Y_hat*100) # now_distanse=int((asset.iat[-1]-Y_hat[-1])/Y_hat[-1]*100) # log.debug("dis:%s now:%s"%(distance[:2],now_distanse)) # log.debug("now_distanse:%s"%now_distanse) distance = (asset.values.T - Y_hat) now_distanse = asset.iat[-1] - Y_hat[-1] # distance = (asset.values.T-Y_hat)[0] pd.Series(distance).plot(kind='hist', stacked=True, bins=100) # plt.plot((asset.iat[-1].T-Y_hat),'b',alpha=0.9) plt.axvline(now_distanse, hold=None, label="1", color='red') # plt.axhline(now_distanse,hold=None,label="1",color='red') # plt.axvline(asset.iat[0],hold=None,label="1",color='red',linestyle="--") plt.xlabel('Undervalue ------------------------------------------> Overvalue', fontsize=12) plt.ylabel('Frequency', fontsize=14) # plt.title('Undervalue & Overvalue Statistical Chart', fontsize=14) plt.legend([code, asset.iat[-1], str(dates[-1])[5:11]], fontsize=12) plt.grid(True) # plt.show() # import os # print(os.path.abspath(os.path.curdir)) ax5 = fig.add_subplot(326) # fig.figsize=(5, 10) log.info("assert:len:%s %s" % (len(asset.values.T - Y_hat), (asset.values.T - Y_hat)[0])) # distance = map(lambda x:int(x),(asset.values.T - Y_hat)/Y_hat*100) distance = (asset.values.T - Y_hat) / Y_hat * 100 now_distanse = ((asset.iat[-1] - Y_hat[-1]) / Y_hat[-1] * 100) log.debug("dis:%s now:%s" % (distance[:2], now_distanse)) log.debug("now_distanse:%s" % now_distanse) # n, bins = np.histogram(distance, 50) # print n, bins[:2] pd.Series(distance).plot(kind='hist', stacked=True, bins=100) # plt.plot((asset.iat[-1].T-Y_hat),'b',alpha=0.9) plt.axvline(now_distanse, hold=None, label="1", color='red') # plt.axhline(now_distanse,hold=None,label="1",color='red') # plt.axvline(asset.iat[0],hold=None,label="1",color='red',linestyle="--") plt.xlabel('Undervalue ------------------------------------------> Overvalue', fontsize=14) plt.ylabel('Frequency', fontsize=12) # plt.title('Undervalue & Overvalue Statistical Chart', fontsize=14) plt.legend([code, asset.iat[-1]], fontsize=12) plt.grid(True) ax6 = fig.add_subplot(324) h = df.loc[:, ['open', 'close', 'high', 'low']] highp = h['high'].values lowp = h['low'].values openp = h['open'].values closep = h['close'].values lr = LinearRegression() x = np.atleast_2d(np.linspace(0, len(closep), len(closep))).T lr.fit(x, closep) LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False) xt = np.atleast_2d(np.linspace(0, len(closep) + 200, len(closep) + 200)).T yt = lr.predict(xt) bV = [] bP = [] for i in range(1, len(highp) - 1): if highp[i] <= highp[i - 1] and highp[i] < highp[i + 1] and lowp[i] <= lowp[i - 1] and lowp[i] < lowp[i + 1]: bV.append(lowp[i]) bP.append(i) d, p = LIS(bV) idx = [] for i in range(len(p)): idx.append(bP[p[i]]) lr = LinearRegression() X = np.atleast_2d(np.array(idx)).T Y = np.array(d) lr.fit(X, Y) estV = lr.predict(xt) ax6.plot(closep, linewidth=2) ax6.plot(idx, d, 'ko') ax6.plot(xt, estV, '-r', linewidth=3) ax6.plot(xt, yt, '-g', linewidth=3) plt.grid(True) # plt.tight_layout() zp2 = zoompan.ZoomPan() figZoom = zp2.zoom_factory(ax6, base_scale=scale) figPan = zp2.pan_factory(ax6) # plt.ion() plt.show(block=False)