def plot_correlation_demo2(): np.random.seed(0) # to reproduce the data later on pylab.clf() pylab.figure(num=None, figsize=(8, 8)) x = np.arange(-5, 5, 0.2) pylab.subplot(221) y = 0.5 * x**2 + norm.rvs(1, scale=.01, size=len(x)) _plot_correlation_func(x, y) pylab.subplot(222) y = 0.5 * x**2 + norm.rvs(1, scale=.1, size=len(x)) _plot_correlation_func(x, y) pylab.subplot(223) y = 0.5 * x**2 + norm.rvs(1, scale=1, size=len(x)) _plot_correlation_func(x, y) pylab.subplot(224) y = 0.5 * x**2 + norm.rvs(1, scale=10, size=len(x)) _plot_correlation_func(x, y) pylab.autoscale(tight=True) pylab.grid(True) filename = "corr_demo_2.png"
def plot_p_leading_order(frame): mat = scipy.io.loadmat('sound-speed_2D-wave.mat') T = 5 nt = T / 0.5 pp = mat['U'][nt, :, :] xx = mat['xx'] yy = mat['yy'] fig = pl.figure(figsize=(8, 3.5)) #pl.title("t= "+str(sol.state.t),fontsize=20) pl.xticks(size=20) pl.yticks(size=20) pl.xlabel('x', fontsize=20) pl.ylabel('y', fontsize=20) #pl.pcolormesh(xx,yy,p_subxy,cmap=cm.OrRd) pl.pcolormesh(xx, yy, pp, cmap='RdBu_r') pl.autoscale(tight=True) cb = pl.colorbar(ticks=[0.5, 1, 1.5, 2]) #pl.clim(ticks=[0.5,1,1.5,2]) imaxes = pl.gca() pl.axes(cb.ax) pl.yticks(fontsize=20) pl.axes(imaxes) #pl.xticks(fontsize=20); pl.axes(imaxes) #pl.axis('equal') pl.axis('tight') fig.tight_layout() pl.savefig('./_plots_to_paper/sound-speed_LO_t' + str(frame) + '_pcolor.png') pl.close()
def plot_mi_demo(): np.random.seed(0) # to reproduce the data later on pylab.clf() pylab.figure(num=None, figsize=(8, 8)) x = np.arange(0, 10, 0.2) pylab.subplot(221) y = 0.5 * x + norm.rvs(1, scale=.01, size=len(x)) _plot_mi_func(x, y) # pylab.subplot(222) # y = 0.5 * x + norm.rvs(1, scale=.1, size=len(x)) # _plot_mi_func(x, y) # pylab.subplot(223) # y = 0.5 * x + norm.rvs(1, scale=1, size=len(x)) # _plot_mi_func(x, y) # pylab.subplot(224) # y = norm.rvs(1, scale=10, size=len(x)) # _plot_mi_func(x, y) pylab.autoscale(tight=True) pylab.grid(True) filename = "mi_demo_1.png" pylab.savefig(os.path.join(CHART_DIR, filename), bbox_inches="tight")
def plot_mi_demo2(): np.random.seed(0) pylab.clf() pylab.figure(num=None, figsize=(8, 8)) x = np.arange(-5, 5, 0.2) pylab.subplot(221) y = 0.5 * x**2 + norm.rvs(1, scale=.01, size=len(x)) _plot_mi_func(x, y) pylab.subplot(222) y = 0.5 * x**2 + norm.rvs(1, scale=.1, size=len(x)) _plot_mi_func(x, y) pylab.subplot(223) y = 0.5 * x**2 + norm.rvs(1, scale=1, size=len(x)) _plot_mi_func(x, y) pylab.subplot(224) y = 0.5 * x**2 + norm.rvs(1, scale=10, size=len(x)) _plot_mi_func(x, y) pylab.autoscale(tight=True) pylab.grid(True) filename = "mi_demo_2.png"
def plot_p(frame): sol=Solution(frame,file_format='petsc',read_aux=False,path='./_output/_p/',file_prefix='claw_p') x=sol.state.grid.x.centers; y=sol.state.grid.y.centers mx=len(x); my=len(y) mp=sol.state.num_eqn yy,xx = np.meshgrid(y,x) p=sol.state.q[0,:,:] fig = pl.figure(figsize=(8, 3.5)) #pl.title("t= "+str(sol.state.t),fontsize=20) pl.xticks(size=20); pl.yticks(size=20) pl.xlabel('x',fontsize=20); pl.ylabel('y',fontsize=20) #pl.pcolormesh(xx,yy,p_subxy,cmap=cm.OrRd) pl.pcolormesh(xx,yy,p,cmap='RdBu_r') pl.autoscale(tight=True) cb = pl.colorbar(ticks=[0.5,1,1.5,2]); #pl.clim(ticks=[0.5,1,1.5,2]) imaxes = pl.gca(); pl.axes(cb.ax) pl.yticks(fontsize=20); pl.axes(imaxes) #pl.xticks(fontsize=20); pl.axes(imaxes) #pl.axis('equal') pl.axis('tight') fig.tight_layout() pl.savefig('./_plots_to_paper/sound-speed_FV_t'+str(frame)+'_pcolor.png') pl.close()
def plot_p_leading_order(frame): mat = scipy.io.loadmat('sound-speed_2D-wave.mat') T=5; nt=T/0.5 pp=mat['U'][nt,:,:] xx=mat['xx'] yy=mat['yy'] fig=pl.figure(figsize=(8, 3.5)) #pl.title("t= "+str(sol.state.t),fontsize=20) pl.xticks(size=20); pl.yticks(size=20) pl.xlabel('x',fontsize=20); pl.ylabel('y',fontsize=20) #pl.pcolormesh(xx,yy,p_subxy,cmap=cm.OrRd) pl.pcolormesh(xx,yy,pp,cmap='RdBu_r') pl.autoscale(tight=True) cb = pl.colorbar(ticks=[0.5,1,1.5,2]); #pl.clim(ticks=[0.5,1,1.5,2]) imaxes = pl.gca(); pl.axes(cb.ax) pl.yticks(fontsize=20); pl.axes(imaxes) #pl.xticks(fontsize=20); pl.axes(imaxes) #pl.axis('equal') pl.axis('tight') fig.tight_layout() pl.savefig('./_plots_to_paper/sound-speed_LO_t'+str(frame)+'_pcolor.png') pl.close()
def plot_mi_demo1(): np.random.seed(0) # to reproduce the data later on pylab.clf() pylab.figure(num=None, figsize=(8, 8)) x = np.arange(0, 10, 0.2) pylab.subplot(221) y = 0.5 * x + norm.rvs(1, scale=.01, size=len(x)) _plot_mi_func(x, y) pylab.subplot(222) y = 0.5 * x + norm.rvs(1, scale=.1, size=len(x)) _plot_mi_func(x, y) pylab.subplot(223) y = 0.5 * x + norm.rvs(1, scale=1, size=len(x)) _plot_mi_func(x, y) pylab.subplot(224) y = norm.rvs(1, scale=10, size=len(x)) _plot_mi_func(x, y) pylab.autoscale(tight=True) pylab.grid(True) filename = "mi_demo_1.png"
def plot_simple_demo_2(): pylab.clf() fig = pylab.figure(num=None, figsize=(10, 4)) pylab.subplot(121) title = "Original feature space" pylab.title(title) pylab.xlabel("$X_1$") pylab.ylabel("$X_2$") np.random.seed(3) x1 = np.arange(0, 10, .2) x2 = x1 + np.random.normal(loc=0, scale=1, size=len(x1)) good = x1 > x2 bad = ~good x1g = x1[good] x2g = x2[good] pylab.scatter(x1g, x2g, edgecolor="blue", facecolor="blue") x1b = x1[bad] x2b = x2[bad] pylab.scatter(x1b, x2b, edgecolor="red", facecolor="white") pylab.grid(True) pylab.subplot(122) X = np.c_[(x1, x2)] pca = decomposition.PCA(n_components=1) Xtrans = pca.fit_transform(X) Xg = Xtrans[good] Xb = Xtrans[bad] pylab.scatter(Xg[:, 0], np.zeros(len(Xg)), edgecolor="blue", facecolor="blue") pylab.scatter(Xb[:, 0], np.zeros(len(Xb)), edgecolor="red", facecolor="white") title = "Transformed feature space" pylab.title(title) pylab.xlabel("$X'$") fig.axes[1].get_yaxis().set_visible(False) print("======系统pca 数据2==========") print("特征重要度:", pca.explained_variance_ratio_) pylab.grid(True) pylab.autoscale(tight=True) filename = "pca_demo_2.png" pylab.savefig(os.path.join(CHART_DIR, filename), bbox_inches="tight")
def plot_correlation_demo(): np.random.seed(0) # to reproduce the data later on pylab.clf() pylab.figure(num=None, figsize=(8, 8)) x = np.arange(0, 10, 0.2) pylab.subplot(221) y = 0.5 * x + norm.rvs(1, loc=0, scale=.01, size=len(x)) _plot_correlation_func(x, y) pylab.subplot(222) y = 0.5 * x + norm.rvs(1, loc=0, scale=.1, size=len(x)) _plot_correlation_func(x, y) pylab.subplot(223) y = 0.5 * x + norm.rvs(1, loc=0, scale=1, size=len(x)) _plot_correlation_func(x, y) pylab.subplot(224) y = norm.rvs(1, loc=0, scale=10, size=len(x)) _plot_correlation_func(x, y) pylab.autoscale(tight=True) pylab.grid(True) filename = "corr_demo_1.png" pylab.savefig(os.path.join(CHART_DIR, filename), bbox_inches="tight") pylab.clf() pylab.figure(num=None, figsize=(8, 8)) x = np.arange(-5, 5, 0.2) pylab.subplot(221) y = 0.5 * x ** 2 + norm.rvs(1, loc=0, scale=.01, size=len(x)) _plot_correlation_func(x, y) pylab.subplot(222) y = 0.5 * x ** 2 + norm.rvs(1, loc=0, scale=.1, size=len(x)) _plot_correlation_func(x, y) pylab.subplot(223) y = 0.5 * x ** 2 + norm.rvs(1, loc=0, scale=1, size=len(x)) _plot_correlation_func(x, y) pylab.subplot(224) y = 0.5 * x ** 2 + norm.rvs(1, loc=0, scale=10, size=len(x)) _plot_correlation_func(x, y) pylab.autoscale(tight=True) pylab.grid(True) filename = "corr_demo_2.png" pylab.savefig(os.path.join(CHART_DIR, filename), bbox_inches="tight")
def plot_simple_demo_lda(): #from sklearn import lda # 与demo2不同的是,lda会利用(即有监督的标签信息,而pca并不会利用此信息)类别信息进行降维 from sklearn.discriminant_analysis import LinearDiscriminantAnalysis pylab.clf() fig = pylab.figure(num=None, figsize=(10, 4)) pylab.subplot(121) title = "Original feature space" pylab.title(title) pylab.xlabel("$X_1$") pylab.ylabel("$X_2$") good = x1 > x2 bad = ~good x1g = x1[good] x2g = x2[good] pylab.scatter(x1g, x2g, edgecolor="blue", facecolor="blue") x1b = x1[bad] x2b = x2[bad] pylab.scatter(x1b, x2b, edgecolor="red", facecolor="white") pylab.grid(True) pylab.subplot(122) X = np.c_[(x1, x2)] #lda_inst = lda.LDA(n_components=1) lda_inst = LinearDiscriminantAnalysis(n_components=1) Xtrans = lda_inst.fit_transform(X, good) Xg = Xtrans[good] Xb = Xtrans[bad] pylab.scatter(Xg[:, 0], np.zeros(len(Xg)), edgecolor="blue", facecolor="blue") pylab.scatter(Xb[:, 0], np.zeros(len(Xb)), edgecolor="red", facecolor="white") title = "Transformed feature space" pylab.title(title) pylab.xlabel("$X'$") fig.axes[1].get_yaxis().set_visible(False) pylab.grid(True) pylab.autoscale(tight=True) filename = "lda_demo.png" pylab.savefig(os.path.join(CHART_DIR, filename), bbox_inches="tight")
def plot_correlation_demo(): np.random.seed(0) pylab.clf() pylab.figure(num=None, figsize=(8, 8)) x = np.arange(0, 10, 0.2) pylab.subplot(221) y = 0.5 * x + norm.rvs(1, scale=.01, size=len(x)) _plot_correlation_func(x, y) pylab.subplot(222) y = 0.5 * x + norm.rvs(1, scale=.1, size=len(x)) _plot_correlation_func(x, y) pylab.subplot(223) y = 0.5 * x + norm.rvs(1, scale=1, size=len(x)) _plot_correlation_func(x, y) pylab.subplot(224) y = norm.rvs(1, scale=10, size=len(x)) _plot_correlation_func(x, y) pylab.autoscale(tight=True) pylab.grid(True) filename = "corr_demo_1.png" pylab.savefig(os.path.join(CHART_DIR, filename), bbox_inches="tight") pylab.clf() pylab.figure(num=None, figsize=(8, 8)) x = np.arange(-5, 5, 0.2) pylab.subplot(221) y = 0.5 * x**2 + norm.rvs(1, scale=.01, size=len(x)) _plot_correlation_func(x, y) pylab.subplot(222) y = 0.5 * x**2 + norm.rvs(1, scale=.1, size=len(x)) _plot_correlation_func(x, y) pylab.subplot(223) y = 0.5 * x**2 + norm.rvs(1, scale=1, size=len(x)) _plot_correlation_func(x, y) pylab.subplot(224) y = 0.5 * x**2 + norm.rvs(1, scale=10, size=len(x)) _plot_correlation_func(x, y) pylab.autoscale(tight=True) pylab.grid(True) filename = "corr_demo_2.png" pylab.savefig(os.path.join(CHART_DIR, filename), bbox_inches="tight")
def plot_simple_demo_2(): pylab.clf() fig = pylab.figure(num=None, figsize=(10, 4)) pylab.subplot(121) title = "Original feature space" pylab.title(title) pylab.xlabel("$X_1$") pylab.ylabel("$X_2$") x1 = np.arange(0, 10, .2) x2 = x1 + np.random.normal(loc=0, scale=1, size=len(x1)) good = x1 > x2 bad = ~good x1g = x1[good] x2g = x2[good] pylab.scatter(x1g, x2g, edgecolor="blue", facecolor="blue") x1b = x1[bad] x2b = x2[bad] pylab.scatter(x1b, x2b, edgecolor="red", facecolor="white") pylab.grid(True) pylab.subplot(122) X = np.c_[(x1, x2)] pca = decomposition.PCA(n_components=1) Xtrans = pca.fit_transform(X) Xg = Xtrans[good] Xb = Xtrans[bad] pylab.scatter( Xg[:, 0], np.zeros(len(Xg)), edgecolor="blue", facecolor="blue") pylab.scatter( Xb[:, 0], np.zeros(len(Xb)), edgecolor="red", facecolor="white") title = "Transformed feature space" pylab.title(title) pylab.xlabel("$X'$") fig.axes[1].get_yaxis().set_visible(False) print(pca.explained_variance_ratio_) pylab.grid(True) pylab.autoscale(tight=True) filename = "pca_demo_2.png" pylab.savefig(os.path.join(CHART_DIR, filename), bbox_inches="tight")
def Plot(): u = Calculator() u = u[::-1]*4.47058 x = condition[:, 1]*1000000. plt.plot(x, u) plt.xlabel('micrometers depth') plt.ylabel('argon concentration ppm') plt.title('1D system state') plt.grid(True) #plt.savefig("F1D_result.png") plt.autoscale(enable=True, axis='y') plt.show() return 'sucessfully plotted'
def plot_simple_demo_lda(): pylab.clf() fig = pylab.figure(num=None, figsize=(10, 4)) pylab.subplot(121) title = "Original feature space" pylab.title(title) pylab.xlabel("$X_1$") pylab.ylabel("$X_2$") good = x1 > x2 bad = ~good x1g = x1[good] x2g = x2[good] pylab.scatter(x1g, x2g, edgecolor="blue", facecolor="blue") x1b = x1[bad] x2b = x2[bad] pylab.scatter(x1b, x2b, edgecolor="red", facecolor="white") pylab.grid(True) pylab.subplot(122) X = np.c_[(x1, x2)] lda_inst = lda(n_components=1) Xtrans = lda_inst.fit_transform(X, good) Xg = Xtrans[good] Xb = Xtrans[bad] pylab.scatter(Xg[:, 0], np.zeros(len(Xg)), edgecolor="blue", facecolor="blue") pylab.scatter(Xb[:, 0], np.zeros(len(Xb)), edgecolor="red", facecolor="white") title = "Transformed feature space" pylab.title(title) pylab.xlabel("$X'$") fig.axes[1].get_yaxis().set_visible(False) pylab.grid(True) pylab.autoscale(tight=True) filename = "lda_demo.png" pylab.savefig(os.path.join(CHART_DIR, filename), bbox_inches="tight")
def plot_clusters(x, y, title, mx=None, ymax=None, xmin=None, km=None): pylab.figure(num=None, figsize=(8, 6)) if km: pylab.scatter(x, y, s=50, c=km.predict(list(zip(x, y)))) else: pylab.scatter(x, y, s=50) pylab.title(title) pylab.xlabel("Occurance of Word1") pylab.ylabel("Occurance of Word2") pylab.autoscale(tight=True) pylab.ylim(ymin=0, ymax=1) pylab.xlim(xmin=0, xmax=1) pylab.grid(True, linestyle='-', color='0.75') return pylab
def Plot(): u, u_sec = Calculator() u = u[::-1]*4.47058 u_sec = u_sec[::-1]*2.17143 x = condition[:, 1]*1000000. plt.plot(x, u) plt.plot(x, u_sec) plt.xlabel('micrometers depth') plt.ylabel('argon concentration ppm') plt.title('Zhang and measured D ranges') plt.grid(True) plt.savefig("F1D_result.png") plt.autoscale(enable=True, axis='y') plt.show()
def plot_simple_demo_lda(): pylab.clf() fig = pylab.figure(num=None, figsize=(10, 4)) pylab.subplot(121) title = "Original feature space" pylab.title(title) pylab.xlabel("$X_1$") pylab.ylabel("$X_2$") good = x1 > x2 bad = ~good x1g = x1[good] x2g = x2[good] pylab.scatter(x1g, x2g, edgecolor="blue", facecolor="blue") x1b = x1[bad] x2b = x2[bad] pylab.scatter(x1b, x2b, edgecolor="red", facecolor="white") pylab.grid(True) pylab.subplot(122) X = np.c_[(x1, x2)] lda_inst = lda.LDA(n_components=1) Xtrans = lda_inst.fit_transform(X, good) Xg = Xtrans[good] Xb = Xtrans[bad] pylab.scatter( Xg[:, 0], np.zeros(len(Xg)), edgecolor="blue", facecolor="blue") pylab.scatter( Xb[:, 0], np.zeros(len(Xb)), edgecolor="red", facecolor="white") title = "Transformed feature space" pylab.title(title) pylab.xlabel("$X'$") fig.axes[1].get_yaxis().set_visible(False) pylab.grid(True) pylab.autoscale(tight=True) filename = "lda_demo.png" pylab.savefig(os.path.join(CHART_DIR, filename), bbox_inches="tight")
def plot_clustering(x, y, title, mx=None, ymax=None, xmin=None, km=None): pylab.figure(num=None, figsize=(8, 6)) if km: pylab.scatter(x, y, s=50, c=km.predict(list(zip(x, y)))) else: pylab.scatter(x, y, s=50) pylab.title(title) pylab.xlabel("Occurrence word 1") pylab.ylabel("Occurrence word 2") pylab.autoscale(tight=True) pylab.ylim(ymin=0, ymax=1) pylab.xlim(xmin=0, xmax=1) pylab.grid(True, linestyle='-', color='0.75') return pylab
def plot_results(benchmark_series, target_series, target_balances, n_assets, columns, name2plot = '', path2save = './', base_name_series = 'series'): # N = len(np.array(benchmark_series).cumsum()) N = len(np.array([item for sublist in benchmark_series for item in sublist]).cumsum()) if not os.path.exists(path2save): os.makedirs(path2save) for i in range(0, len(target_balances)): current_range = np.arange(0, N) current_ts = np.zeros(N) current_ts2 = np.zeros(N) ts_benchmark = np.array([item for sublist in benchmark_series[:i+1] for item in sublist]).cumsum() ts_target = np.array([item for sublist in target_series[:i+1] for item in sublist]).cumsum() t = len(ts_benchmark) current_ts[:t] = ts_benchmark current_ts2[:t] = ts_target current_ts[current_ts == 0] = ts_benchmark[-1] current_ts2[current_ts2 == 0] = ts_target[-1] plt.figure(figsize = (12, 10)) plt.subplot(2, 1, 1) plt.bar(np.arange(n_assets), target_balances[i], color = 'grey') plt.xticks(np.arange(n_assets), columns, rotation='vertical') plt.subplot(2, 1, 2) plt.colormaps = current_cmap plt.plot(current_range[:t], current_ts[:t], color = 'black', label = 'Benchmark') plt.plot(current_range[:t], current_ts2[:t], color = 'red', label = name2plot) plt.plot(current_range[t:], current_ts[t:], ls = '--', lw = .1, color = 'black') plt.autoscale(False) plt.ylim([-1, 1]) plt.legend()
def plot_entropy(): pylab.clf() pylab.figure(num=None, figsize=(5, 4)) title = "Entropy $H(X)$" pylab.title(title) pylab.xlabel("$P(X=$coin will show heads up$)$") pylab.ylabel("$H(X)$") pylab.xlim(xmin=0, xmax=1.1) x = np.arange(0.001, 1, 0.001) y = -x * np.log2(x) - (1-x) * np.log2(1-x) pylab.plot(x, y) pylab.autoscale(tight=True) pylab.grid(True) filename = "entropy_demo.png" pylab.savefig(os.path.join(CHART_DIR, filename), bbox_inches="tight")
def plot_clustering(x, y, title, mx=None, ymax=None, xmin=None, km=None): pylab.figure(num=None, figsize=(8, 6)) if km: pylab.scatter(x, y, s=50, c=km.predict(list(zip(x, y)))) else: pylab.scatter(x, y, s=50) pylab.title(title) pylab.xlabel("Occurrence word 1") pylab.ylabel("Occurrence word 2") # pylab.xticks([w*7*24 for w in range(10)], ['week %i'%w for w in range(10)]) pylab.autoscale(tight=True) pylab.ylim(ymin=0, ymax=1) pylab.xlim(xmin=0, xmax=1) pylab.grid(True, linestyle='-', color='0.75') return pylab
def show_image(img, messages=None, fig=None, ax=None, cmap='Gray'): if not ax or not fig: w, h = plt.figaspect(img.shape[0] / img.shape[1]) fig = plt.figure(figsize=(w, h)) ax = fig.add_axes([0.1, 0.1, 0.8, 0.8]) bbox_opts = dict(ec='k', fc='k') ax.clear() img_axes = ax.imshow(img, cmap=cmaps[cmap]) ax.axis('off') ax.text(0.03, 0.97, messages['filename'], fontsize=8, color='w', ha='left', va='top', transform=ax.transAxes, bbox=bbox_opts) ax.text(0.03, 0.03, messages['text_bottom'], fontsize=8, color='w', ha='left', va='bottom', transform=ax.transAxes, bbox=bbox_opts) fig.canvas.draw() plt.autoscale(tight=True) fig.show() return fig, ax, img_axes
def periodogram(pipe): """Create a plot of the periodogram with nice labels. Highlights highest SNR point and also overplots harmonics """ pgram = pipe.pgram # Nicely formatted periodogram ticks xt = [ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400, 500, 600, 700, 800, 900, ] grid_P = pipe.header.ix['grid_P'].value grid_s2n = pipe.header.ix['grid_s2n'].value plt.semilogx() peak = pgram.sort_values('s2n').iloc[-1] # Plot periogram, peak, and harmonics plt.plot(pgram.P, pgram.s2n) plt.plot(grid_P, grid_s2n, 'ro') harm = np.array([2,4]).astype(float) harm = np.hstack([1,1/harm,harm]) harm *= grid_P for h in harm: plt.axvline(h,lw=6,alpha=0.2,color='r',zorder=0) # Label plot ax = plt.gca() plt.autoscale(axis='x',tight=True) xl = plt.xlim() # Save away the tight-fitting limits plt.xticks(xt, xt) # Add the nicely formatted ticks ax.xaxis.set_ticks_position('top') ax.xaxis.set_label_position('top') plt.xlim(*xl) # Reset the plot limits. plt.xlabel('Period [days]') plt.ylabel('SNR') plt.draw()
def plot_entropy(): pylab.clf() pylab.figure(num=None, figsize=(5, 4)) title = "Entropy $H(X)$" pylab.title(title) pylab.xlabel("$P(X=$coin will show heads up$)$") pylab.ylabel("$H(X)$") pylab.xlim(xmin=0, xmax=1.1) x = np.arange(0.001, 1, 0.001) y = -x * np.log2(x) - (1 - x) * np.log2(1 - x) pylab.plot(x, y) # pylab.xticks([w*7*24 for w in [0,1,2,3,4]], ['week %i'%(w+1) for w in # [0,1,2,3,4]]) pylab.autoscale(tight=True) pylab.grid(True) filename = "entropy_demo.png" pylab.savefig(os.path.join(CHART_DIR, filename), bbox_inches="tight")
def __init__(self, x, y, title, mx=None, y_max=None, x_min=None, kmeans=None): pylab.figure(num=None, figsize=(8, 6)) if kmeans: pylab.scatter(x, y, s=50, c=kmeans.predict(list(zip(x, y)))) else: pylab.scatter(x, y, s=50) pylab.title(title) pylab.xlabel("Occurrence of first word") pylab.ylabel("Occurrence of second word") pylab.autoscale(tight=True) pylab.ylim(bottom=0, top=1) pylab.xlim(left=0, right=1) self._pylab = pylab
def plot_p(frame): sol = Solution(frame, file_format='petsc', read_aux=False, path='./_output/_p/', file_prefix='claw_p') x = sol.state.grid.x.centers y = sol.state.grid.y.centers mx = len(x) my = len(y) mp = sol.state.num_eqn yy, xx = np.meshgrid(y, x) p = sol.state.q[0, :, :] fig = pl.figure(figsize=(8, 3.5)) #pl.title("t= "+str(sol.state.t),fontsize=20) pl.xticks(size=20) pl.yticks(size=20) pl.xlabel('x', fontsize=20) pl.ylabel('y', fontsize=20) #pl.pcolormesh(xx,yy,p_subxy,cmap=cm.OrRd) pl.pcolormesh(xx, yy, p, cmap='RdBu_r') pl.autoscale(tight=True) cb = pl.colorbar(ticks=[0.5, 1, 1.5, 2]) #pl.clim(ticks=[0.5,1,1.5,2]) imaxes = pl.gca() pl.axes(cb.ax) pl.yticks(fontsize=20) pl.axes(imaxes) #pl.xticks(fontsize=20); pl.axes(imaxes) #pl.axis('equal') pl.axis('tight') fig.tight_layout() pl.savefig('./_plots_to_paper/sound-speed_FV_t' + str(frame) + '_pcolor.png') pl.close()
def main(): #mat_dir = 'Annotations_Part/' #origimage_dir = 'JPEGImages/' mat_dir = 'Annotations_Part/' origimage_dir = 'JPEGImages/' label_dir = 'yolabel/' personimage_dir = 'yoimage/' os.mkdir(label_dir) os.mkdir(personimage_dir) list_mats = os.listdir(mat_dir) for mat in list_mats: imname = mat[:-4] + '.jpg' image_size, part_mask = make_label(origimage_dir + imname, mat_dir + mat) if image_size == 0: continue copy_cmd = 'cp ' + origimage_dir + imname + ' ' + personimage_dir os.system(copy_cmd) mpl.rcParams['savefig.pad_inches'] = 0 figsize = (image_size[1]*1.0/100, image_size[0]*1.0/100) fig = plt.figure(figsize=figsize) ax = plt.axes([0,0,1,1], frameon=False) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) plt.autoscale(tight=True) if np.max(part_mask) == 0: ax.imshow(part_mask, cmap='gray') else: ax.imshow(part_mask, cmap=color_map(N=np.max(part_mask) + 1)) #plt.savefig(label_dir + imname) cv2.imwrite(label_dir + imname, part_mask) plt.close()
def ImageViewer(im, cmap=None, interpolation='nearest', **kwargs): import matplotlib.pyplot as plt import matplotlib.cm as cm if type(im) is str: im = open_image(im, rgb2gray=False) if cmap is None: cmap = cm.gray fig = plt.figure() ax = fig.add_subplot(111) ax.imshow(im, interpolation=interpolation, cmap=cmap, **kwargs) h = im.shape[0] w = im.shape[1] if im.ndim == 2: def format_coord(x, y): col = int(x + 0.5) row = int(y + 0.5) if col >= 0 and col < w and row >= 0 and row < h: z = im[row, col] return '%1.3f (y=%1.1f, x=%1.1f)' % (z, y, x) else: return 'x=%1.1f, y=%1.1f' % (x, y) else: def format_coord(x, y): col = int(x + 0.5) row = int(y + 0.5) if col >= 0 and col < w and row >= 0 and row < h: r = im[row, col, 0] g = im[row, col, 1] b = im[row, col, 2] return 'y=%1.1f, x=%1.1f, R=%d, G=%d, B=%d' % (y, x, r, g, b) else: return 'x=%1.1f, y=%1.1f' % (x, y) ax.format_coord = format_coord plt.autoscale(tight=True) return plt
def plot_entropy(): pylab.clf() pylab.figure(num=None, figsize=(5, 4)) title = "Entropy $H(X)$" pylab.title(title) pylab.xlabel("$P(X=$coin will show heads up$)$") pylab.ylabel("$H(X)$") pylab.xlim(xmin=0, xmax=1.1) x = np.arange(0.001, 1, 0.001) # Claude Shannon's information entropy # H(X) = - Sum_i( p(Xi) * log(p(Xi))) y = -x * np.log2(x) - (1 - x) * np.log2(1 - x) pylab.plot(x, y) pylab.autoscale(tight=True) pylab.grid(True) filename = "b_entropy_coin.png" pylab.savefig(os.path.join(CHART_DIR, filename), bbox_inches="tight")
def t_tms_from_Xc(M, savefig=None, plot_fig=None, ttms_true=None, Xc=None): ''' Calculates the t(tms) for a given Xc and mass Xc: float M: float ''' # ------------------------------------------------------------------------------ # Parameters from the models mass = np.array([14.6, 12.5, 10.8, 9.6, 8.6, 7.7, 6.4, 5.5, 4.8, 4.2, 3.8, 3.4]) nm = len(mass) str_mass = ['M14p60', 'M12p50', 'M10p80', 'M9p600', 'M8p600', 'M7p700', 'M6p400', 'M5p500', 'M4p800', 'M4p200', 'M3p800', 'M3p400'] st = ['B0.5', 'B1', 'B1.5', 'B2', 'B2.5', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B9'] zsun = 'Z01400' str_vel = ['V60000', 'V70000', 'V80000', 'V90000', 'V95000'] Hfracf = 0. # end of main sequence # **** folder_data = 'tables/models/models_bes/' if plot_fig is True: plt.xlabel(r'$t/t_{MS}$') plt.ylabel(r'$X_c$') plt.ylim([0.0, 0.8]) plt.xlim([0.0, 1.0]) # ------------------------------------------------------------------------------ # Loop (reading the models) typ = (1, 3, 16, 21) # Age, Lum versus Teff versus Hfrac arr_age = [] arr_Hfr = [] arr_t_tc = [] cor = phc.gradColor(np.arange(len(st)), cmapn='inferno') iv = 2 # O que eh isto? arr_Xc = [] for i in range(nm): file_data = folder_data + str_mass[i] + zsun + str_vel[iv] + '.dat' age, lum, Teff, Hfrac = np.loadtxt(file_data, usecols=typ, unpack=True, skiprows=2) arr_age.append(age) arr_Hfr.append(Hfrac) iMS = np.where(abs(Hfrac - Hfracf) == min(abs(Hfrac - Hfracf))) X_c = Hfrac[0:iMS[0][0]] arr_Xc.append(X_c) t_tc = age[0:iMS[0][0]] / max(age[0:iMS[0][0]]) arr_t_tc.append(t_tc) if plot_fig is True: plt.plot(t_tc, X_c, color=cor[i], label=('%s' % st[i])) # ------------------------------------------------------------------------------ # Interpolation k = find_nearest(mass, M)[1] if plot_fig is True: plt.plot(ttms_true, Xc, 'o') plt.autoscale() plt.minorticks_on() plt.legend(fontsize=10, ncol=2, fancybox=False, frameon=False) # ------------------------------------------------------------------------------ if savefig is True: pdfname = 'Xc_vs_Tsp.png' plt.savefig(pdfname) return k, arr_t_tc, arr_Xc
def setup( subplt=None, figsize=None, ax=None, xr=None, xmin=None, xmax=None, yr=None, ymin=None, ymax=None, xlog=False, ylog=False, xoffset=None, yoffset=None, xlabel=None, ylabel=None, xtickv=None, xticknames=None, xtickrotate=None, ytickv=None, yticknames=None, ytickrotate=None, halfxlog=False, suptitle=None, suptitle_prop=None, subtitle=None, subtitle_prop=None, subtitleloc=1, title=None, xticks=None, yticks=None, autoticks=False, embiggenx=None, embiggeny=None, secondx=False, secondx_prop=None, secondy=False, secondy_prop=None, grid=True, tickmarks=True, font=True, adjust=True, hspace=0.1, wspace=0.1, aspect=None, rasterized=False, ): '''Setup some nice defaults so that we are all fancy like xr,yr -- xrange and yrange. by setting this it turns off the autoscale feature in that axis xlog,ylog -- T/F I do love me my log log plots xlabel,ylabel,title -- Set some nice text for the different axis xticks, yticks -- set to False if you want to hide the tickmarks and labels grid -- Turn on the grid in a nice way tickmarks -- make me some nick minor and major ticks you can pass in a gridspec ''' # if notebook: # # http://matplotlib.org/users/customizing.html # # http://damon-is-a-geek.com/publication-ready-the-first-time-beautiful-reproducible-plots-with-matplotlib.html # matplotlib.rcParams['savefig.dpi'] = 144 # matplotlib.rcParams.update({'font.size': 12}) # # matplotlib.rcParams['font.family'] = 'serif' # # matplotlib.rcParams['font.serif'] = ['Computer Modern Roman'] # # matplotlib.rcParams['text.usetex'] = True if figsize is not None: fig = pylab.figure(figsize=figsize) ## Handle subplot being an int `223`, tuple `(2,2,3)` or gridspec try: if subplt is None: if ax is None: ax = pylab.gca() elif isinstance(subplt, (int, gridspec.GridSpec, gridspec.SubplotSpec)): ax = pylab.subplot(subplt) else: ax = pylab.subplot(*subplt) except Exception as e: raise ValueError( 'Failed to setup subplt:[{}] -- probably indexing error'.format( subplt)) # Ranges -- Setting either xr,yr stops the auto ranging if xr is not None: ax.set_xlim(xr) pylab.autoscale(False, 'x', True) if yr is not None: ax.set_ylim(yr) pylab.autoscale(False, 'y', True) # Log stuff -- do this afterwards to ensure the minor tick marks are updated # can set the specific ticks using subsx, subsy -- # ax.set_xscale('log', subsx=[2, 3, 4, 5, 6, 7, 8, 9]) # Look here: http://www.ianhuston.net/2011/02/minor-tick-labels-in-matplotlib # clip ensures that lines that go off the edge of the plot are shown but clipped if xlog: ax.set_xscale('log', nonposx='clip') if ylog: ax.set_yscale('log', nonposy='clip') # Labels if xlabel is not None: pylab.xlabel(xlabel) if ylabel is not None: pylab.ylabel(ylabel) if title is not None: pylab.title(title) if suptitle is not None: if suptitle_prop is None: suptitle_prop = {} pylab.suptitle(suptitle, **suptitle_prop) if subtitle is not None: prop = dict(transform=ax.transAxes) if subtitleloc == 1: prop.update({ 'location': (0.95, 0.95), 'horizontalalignment': 'right', 'verticalalignment': 'top' }) elif subtitleloc == 3: prop.update({ 'location': (0.05, 0.05), 'horizontalalignment': 'left', 'verticalalignment': 'bottom' }) else: raise NotImplementedError( 'Get to work adding the following subtitle location: %d' % (subtitleloc)) if subtitle_prop is not None: prop.update(subtitle_prop) loc = prop.pop('location') outline = prop.pop('outline', True) outlinewidth = prop.pop('linewidth', 3.5) txt = pylab.text(loc[0], loc[1], subtitle, **prop) if outline: # pylab.setp(txt,path_effects=[PathEffects.Stroke(linewidth=outlinewidth, foreground="w")]) # Broken txt.set_path_effects([ PathEffects.Stroke(linewidth=outlinewidth, foreground="w"), PathEffects.Normal() ]) # raise ValueError() if xtickv is not None: ax.xaxis.set_major_locator(matplotlib.ticker.FixedLocator(xtickv)) if xticknames is not None: ax.xaxis.set_major_formatter( matplotlib.ticker.FixedFormatter(xticknames)) if xtickrotate is not None: if xtickrotate == 'vertical': pylab.xticks(xtickv, xticknames, rotation='vertical') else: tmp = dict(rotation=30, ha='right') if isinstance(xtickrotate, dict): tmp.update(xtickrotate) else: tmp['rotation'] = xtickrotate pylab.setp(pylab.xticks()[1], **tmp) if ytickv is not None: ax.yaxis.set_major_locator(matplotlib.ticker.FixedLocator(ytickv)) if yticknames is not None: ax.yaxis.set_major_formatter( matplotlib.ticker.FixedFormatter(yticknames)) if ytickrotate is not None: pylab.setp(pylab.yticks()[1], rotation=ytickrotate, ha='right') # Axis hiding if autoticks: if not (((isinstance(subplt, tuple) and len(subplt) == 3)) or ((isinstance(subplt, gridspec.SubplotSpec)))): splog('Cannot setup auto ticks without a proper subplot') else: if isinstance(subplt, gridspec.SubplotSpec): rows, cols, i, _ = subplt.get_geometry() i += 1 # i is 0 indexed. else: rows, cols, i = subplt if ((i % cols) != 1) and (not yticks): yticks = False # if ( i < (cols*(rows-1) + 1) ) and (not xticks): # xticks = False xticks = not ((i < (cols * (rows - 1) + 1)) and (not xticks)) # Tickmark hiding -- used by autoticks as well. if xticks is False: # ax.set_xticklabels([]) ax.set_xlabel('') pylab.setp(ax.get_xticklabels(), visible=False) if yticks is False: # ax.set_yticklabels([]) ax.set_ylabel('') pylab.setp(ax.get_yticklabels(), visible=False) # some nice defaults if grid is True: pylab.grid(b=True, which='major', linestyle='solid', color='0.3', alpha=0.5) ax.set_axisbelow(True) if grid is False: pylab.grid('off') if tickmarks: ax.tick_params('both', which='major', length=5, width=2) ax.tick_params('both', which='minor', length=3, width=1) ax.minorticks_on() else: ax.minorticks_off() pylab.tick_params(axis='both', which='both', bottom='off', top='off', left='off', right='off') if adjust: pylab.subplots_adjust(hspace=hspace, wspace=wspace) if font: # this in theory should work, but fails becuase of the stupidify known as `_` # pylab.rc('font', **{'family':'sans-serif', 'sans-serif':['Helvetica']}) # prop = {'family' : 'normal', 'weight' : 'bold', 'size' : 22} # pylab.rc('font', **{'family':'serif', 'serif':['Computer Modern Roman']}) # pylab.rc('text', usetex=True) pass if aspect is not None: # 'auto', 'equal' ax.set_aspect(aspect) if embiggenx: setup(xr=math.embiggen(ax.axis()[:2], embiggenx)) if embiggeny: setup(yr=math.embiggen(ax.axis()[2:], embiggeny)) if (xticks) and (halfxlog): xax = ax.xaxis xax.set_minor_formatter(matplotlib.ticker.FormatStrFormatter('%g')) tmp = (10.0**(np.arange(-1, 5, 1))) * 5.0 for x, label in zip(xax.get_minorticklocs(), xax.get_minorticklabels()): if x in tmp: label.set_fontsize(8) else: label.set_fontsize(0) pylab.setp(label, visible=False) if secondx: # second x axis tmp = dict( xlabel=None, fcn=lambda arr: arr, fmt='{}', xr=ax.axis()[:2], xlog=('log' in ax.get_xscale()), # yr=ax.axis()[2:] xtickv=ax.get_xticks(), ) # defaults if secondx_prop: tmp.update(secondx_prop) # adjust some nice things f = tmp.pop('fcn') fcn = lambda arr: [f(x) for x in arr] fmt = tmp.pop('fmt') if isinstance(fmt, str): fmt = (fmt, fmt) if 'xtickv' in tmp: tmp['xtickv'] = np.array(tmp['xtickv']) if 'xticknames' not in tmp: arr = np.array(fcn(tmp['xtickv'])) tmp['xticknames'] = np.array([fmt[0].format(x) for x in arr]) if tmp['xlog'] and tmp['nicelog']: ii = np.where((arr >= 1e1) & (arr < 1e4))[0] tmp['xticknames'][ii] = [fmt[1].format(x) for x in arr[ii]] ii = np.where(arr >= 1e4) tmp['xticknames'][ii] = [ fmt[2].format(np.log10(x)) for x in arr[ii] ] ax2 = ax.twiny() # I have never understood this ax2.set_xlabel(tmp['xlabel']) ax2.set_xlim(tmp['xr']) if tmp['xlog']: ax2.set_xscale('log', nonposx='clip') if 'xtickv' in tmp: ax2.xaxis.set_major_locator( matplotlib.ticker.FixedLocator(tmp['xtickv'])) if 'xticknames' in tmp: ax2.xaxis.set_major_formatter( matplotlib.ticker.FixedFormatter(tmp['xticknames'])) ax.secondx = ax2 pylab.sca(ax) if secondy: # second x axis tmp = dict( ylabel=None, fcn=lambda arr: arr, fmt='{}', yr=ax.axis()[2:], ylog=('log' in ax.get_yscale()), ytickv=ax.get_yticks(), ) if secondy_prop: tmp.update(secondy_prop) # adjust some nice things f = tmp.pop('fcn') fcn = lambda arr: [f(x) for x in arr] fmt = tmp.pop('fmt') if isinstance(fmt, str): fmt = (fmt, fmt) if 'ytickv' in tmp: tmp['ytickv'] = np.array(tmp['ytickv']) if 'yticknames' not in tmp: arr = np.array(fcn(tmp['ytickv'])) tmp['yticknames'] = np.array([fmt[0].format(x) for x in arr]) if tmp['ylog'] and tmp['nicelog']: ii = np.where((arr >= 1e1) & (arr < 1e4))[0] tmp['yticknames'][ii] = [fmt[1].format(x) for x in arr[ii]] ii = np.where(arr >= 1e4) tmp['yticknames'][ii] = [ fmt[2].format(np.log10(x)) for x in arr[ii] ] print(tmp) ax2 = ax.twinx() # I have never understood this ax2.set_ylabel(tmp['ylabel']) ax2.set_ylim(tmp['yr']) if tmp['ylog']: ax2.set_yscale('log', nonposx='clip') if 'ytickv' in tmp: ax2.yaxis.set_major_locator( matplotlib.ticker.FixedLocator(tmp['ytickv'])) if 'yticknames' in tmp: ax2.yaxis.set_major_formatter( matplotlib.ticker.FixedFormatter(tmp['yticknames'])) ax.secondy = ax2 pylab.sca(ax) if xoffset is False: tmp = matplotlib.ticker.ScalarFormatter(useOffset=False) tmp.set_scientific(False) ax.xaxis.set_major_formatter(tmp) if yoffset is False: tmp = matplotlib.ticker.ScalarFormatter(useOffset=False) tmp.set_scientific(False) ax.yaxis.set_major_formatter(tmp) if rasterized: ax.set_rasterized(True) # temp return ax
def plot_mi_demo(): # determinism! np.random.seed(0) # plot1 pylab.clf() pylab.figure(num=None, figsize=(8, 8)) x = np.arange(0, 10, 0.2) pylab.subplot(221) y = 0.5 * x + norm.rvs(1, scale=.01, size=len(x)) plot_mi_func(x, y) pylab.subplot(222) y = 0.5 * x + norm.rvs(1, scale=.1, size=len(x)) plot_mi_func(x, y) pylab.subplot(223) y = 0.5 * x + norm.rvs(1, scale=1, size=len(x)) plot_mi_func(x, y) pylab.subplot(224) y = norm.rvs(1, scale=10, size=len(x)) plot_mi_func(x, y) pylab.autoscale(tight=True) pylab.grid(True) filename = "c_mutual_information_1_ok.png" pylab.savefig(os.path.join(CHART_DIR, filename), bbox_inches="tight") ############## # plot 2 pylab.clf() pylab.figure(num=None, figsize=(8, 8)) x = np.arange(-5, 5, 0.2) pylab.subplot(221) y = 0.5 * x ** 2 + norm.rvs(1, scale=.01, size=len(x)) plot_mi_func(x, y) pylab.subplot(222) y = 0.5 * x ** 2 + norm.rvs(1, scale=.1, size=len(x)) plot_mi_func(x, y) pylab.subplot(223) y = 0.5 * x ** 2 + norm.rvs(1, scale=1, size=len(x)) plot_mi_func(x, y) pylab.subplot(224) y = 0.5 * x ** 2 + norm.rvs(1, scale=10, size=len(x)) plot_mi_func(x, y) pylab.autoscale(tight=True) pylab.grid(True) filename = "c_mutual_information_2_ok.png" pylab.savefig(os.path.join(CHART_DIR, filename), bbox_inches="tight")
forecast = model_fit.forecast(forecast_len) #forecast = np.exp(forecast) # for log correction # eval metrics rmse = np.sqrt(((forecast - df_test)**2).mean()) p_rmse = (rmse / df_test.mean()) * 100 # plot it again plt.figure(figsize=(20, 10)) plt.plot(series, 'b') plt.plot(forecast, 'r') plt.title('RMSE: %.2f Percent Avg Error: %.2f' % (rmse, p_rmse)) plt.xlabel('Weeks') plt.ylabel('Weekly Rates') plt.autoscale(enable=True, axis='x', tight=True) plt.axvline(x=series.index[len(df_train)], color='black') # write out for testing df_test.to_csv('../../data/processed/test_actuals.csv') forecast.to_csv('../../data/processed/test_forecasted.csv') ############## # prediction # ############## # predict out for 52 model = SARIMAX(series, order=(1, 1, 2), seasonal_order=(1, 0, 1, 52), enforce_stationarity=False,
def plot_MDS(x, y=None, data_set_ids=None): if data_set_ids is None: data_set_ids = np.ones(x.shape[0]) pl.close() fig = pl.figure() #axes = fig.add_subplot(111) axes = pl.subplot(111) to_use = false(x.shape[0]) max_to_use = 200 for i in np.unique(data_set_ids): I = (data_set_ids == i).nonzero()[0] if len(I) <= max_to_use: to_use[I] = True else: choice = np.random.choice(I, max_to_use, replace=False) to_use[choice] = True x = x[to_use, :] data_set_ids = data_set_ids[to_use] if y is not None: y = y[to_use] x = try_toarray(x) I_nonzero = x.sum(1).nonzero()[0] x = x[I_nonzero, :] data_set_ids = data_set_ids[I_nonzero] y = y[I_nonzero] ''' x = np.zeros(x.shape) x[data_set_ids==0,0] = 1 x[data_set_ids==1,1] = 1 x[data_set_ids==2,2] = 1 ''' W = pairwise.pairwise_distances(x, x, 'cosine') #W = pairwise.pairwise_distances(x,x,'euclidean') W = make_rbf(x, sigma=10, metric='cosine') W = 1 - W #mds = sklearn.manifold.MDS(dissimilarity='precomputed',max_iter=1000,verbose=1,metric=False) mds = sklearn.manifold.MDS(dissimilarity='precomputed', max_iter=1000, verbose=1, n_init=4) x_mds = mds.fit_transform(W) #mds = sklearn.manifold.MDS() #x_mds = mds.fit_transform(x) colors = ['r', 'g', 'b'] labels = ['s', 'o'] for ind, i in enumerate(np.unique(data_set_ids)): if ind == 0 and False: continue if ind == 2 and False: continue I = data_set_ids == i alpha = 1 / float(I.sum()) alpha = alpha * 50 alpha = .3 if i == 0: alpha = 1 y_curr = y[I] x_curr = x_mds[I, :] for ind2, j in enumerate(np.unique(y_curr)): I2 = y_curr == j axes.scatter(x_curr[I2, 0], x_mds[I2, 1], alpha=alpha, c=colors[ind], s=100, marker=labels[ind2]) #axes.scatter(x_mds[I,0],x_mds[I,1], alpha=alpha,c=colors[ind],s=60) move_fig(fig) pl.autoscale() pl.show(block=True) pass
def plot_MDS(x, y=None, data_set_ids=None): if data_set_ids is None: data_set_ids = np.ones(x.shape[0]) pl.close() fig = pl.figure() #axes = fig.add_subplot(111) axes = pl.subplot(111) to_use = false(x.shape[0]) max_to_use = 200 for i in np.unique(data_set_ids): I = (data_set_ids == i).nonzero()[0] if len(I) <= max_to_use: to_use[I] = True else: choice = np.random.choice(I, max_to_use, replace=False) to_use[choice] = True x = x[to_use,:] data_set_ids = data_set_ids[to_use] if y is not None: y = y[to_use] x = try_toarray(x) I_nonzero = x.sum(1).nonzero()[0] x = x[I_nonzero,:] data_set_ids = data_set_ids[I_nonzero] y = y[I_nonzero] ''' x = np.zeros(x.shape) x[data_set_ids==0,0] = 1 x[data_set_ids==1,1] = 1 x[data_set_ids==2,2] = 1 ''' W = pairwise.pairwise_distances(x,x,'cosine') #W = pairwise.pairwise_distances(x,x,'euclidean') W = make_rbf(x,sigma=10,metric='cosine') W = 1 - W #mds = sklearn.manifold.MDS(dissimilarity='precomputed',max_iter=1000,verbose=1,metric=False) mds = sklearn.manifold.MDS(dissimilarity='precomputed',max_iter=1000,verbose=1,n_init=4) x_mds = mds.fit_transform(W) #mds = sklearn.manifold.MDS() #x_mds = mds.fit_transform(x) colors = ['r','g','b'] labels = ['s','o'] for ind,i in enumerate(np.unique(data_set_ids)): if ind == 0 and False: continue if ind == 2 and False: continue I = data_set_ids == i alpha = 1 / float(I.sum()) alpha = alpha*50 alpha = .3 if i == 0: alpha = 1 y_curr = y[I] x_curr = x_mds[I,:] for ind2, j in enumerate(np.unique(y_curr)): I2 = y_curr == j axes.scatter(x_curr[I2,0],x_mds[I2,1], alpha=alpha,c=colors[ind],s=100,marker = labels[ind2]) #axes.scatter(x_mds[I,0],x_mds[I,1], alpha=alpha,c=colors[ind],s=60) move_fig(fig) pl.autoscale() pl.show(block=True) pass
def setup(subplt=None, figsize=None, ax=None, xr=None, xmin=None, xmax=None, yr=None, ymin=None, ymax=None, xlog=False, ylog=False, xoffset=None, yoffset=None, xlabel=None, ylabel=None, xtickv=None, xticknames=None, xtickrotate=None, ytickv=None, yticknames=None, ytickrotate=None, halfxlog=False, suptitle=None, suptitle_prop=None, subtitle=None, subtitle_prop=None, subtitleloc=1, title=None, xticks=None, yticks=None, autoticks=False, embiggenx=None, embiggeny=None, secondx=False, secondx_prop=None, secondy=False, secondy_prop=None, grid=True, tickmarks=True, font=True, adjust=True, hspace=0.1, wspace=0.1, aspect=None, rasterized=False, ): '''Setup some nice defaults so that we are all fancy like xr,yr -- xrange and yrange. by setting this it turns off the autoscale feature in that axis xlog,ylog -- T/F I do love me my log log plots xlabel,ylabel,title -- Set some nice text for the different axis xticks, yticks -- set to False if you want to hide the tickmarks and labels grid -- Turn on the grid in a nice way tickmarks -- make me some nick minor and major ticks you can pass in a gridspec ''' # if notebook: # # http://matplotlib.org/users/customizing.html # # http://damon-is-a-geek.com/publication-ready-the-first-time-beautiful-reproducible-plots-with-matplotlib.html # matplotlib.rcParams['savefig.dpi'] = 144 # matplotlib.rcParams.update({'font.size': 12}) # # matplotlib.rcParams['font.family'] = 'serif' # # matplotlib.rcParams['font.serif'] = ['Computer Modern Roman'] # # matplotlib.rcParams['text.usetex'] = True if figsize is not None: fig = pylab.figure(figsize=figsize) ## Handle subplot being an int `223`, tuple `(2,2,3)` or gridspec try: if subplt is None: if ax is None: ax = pylab.gca() elif isinstance(subplt, (int, gridspec.GridSpec, gridspec.SubplotSpec) ): ax = pylab.subplot(subplt) else: ax = pylab.subplot(*subplt) except Exception as e: raise ValueError('Failed to setup subplt:[{}] -- probably indexing error'.format(subplt)) # Ranges -- Setting either xr,yr stops the auto ranging if xr is not None: ax.set_xlim(xr) pylab.autoscale(False, 'x', True) if yr is not None: ax.set_ylim(yr) pylab.autoscale(False, 'y', True) # Log stuff -- do this afterwards to ensure the minor tick marks are updated # can set the specific ticks using subsx, subsy -- # ax.set_xscale('log', subsx=[2, 3, 4, 5, 6, 7, 8, 9]) # Look here: http://www.ianhuston.net/2011/02/minor-tick-labels-in-matplotlib # clip ensures that lines that go off the edge of the plot are shown but clipped if xlog: ax.set_xscale('log', nonposx='clip') if ylog: ax.set_yscale('log', nonposy='clip') # Labels if xlabel is not None: pylab.xlabel(xlabel) if ylabel is not None: pylab.ylabel(ylabel) if title is not None: pylab.title(title) if suptitle is not None: if suptitle_prop is None: suptitle_prop = {} pylab.suptitle(suptitle, **suptitle_prop) if subtitle is not None: prop = dict(transform=ax.transAxes) if subtitleloc == 1: prop.update({'location':(0.95,0.95), 'horizontalalignment':'right', 'verticalalignment':'top'}) elif subtitleloc == 3: prop.update({'location':(0.05,0.05), 'horizontalalignment':'left', 'verticalalignment':'bottom'}) else: raise NotImplementedError('Get to work adding the following subtitle location: %d'%(subtitleloc)) if subtitle_prop is not None: prop.update(subtitle_prop) loc = prop.pop('location') outline = prop.pop('outline',True) outlinewidth = prop.pop('linewidth',3.5) txt = pylab.text(loc[0], loc[1], subtitle, **prop) if outline: # pylab.setp(txt,path_effects=[PathEffects.Stroke(linewidth=outlinewidth, foreground="w")]) # Broken txt.set_path_effects( [PathEffects.Stroke(linewidth=outlinewidth, foreground="w"), PathEffects.Normal()]) # raise ValueError() if xtickv is not None: ax.xaxis.set_major_locator(matplotlib.ticker.FixedLocator(xtickv)) if xticknames is not None: ax.xaxis.set_major_formatter(matplotlib.ticker.FixedFormatter(xticknames)) if xtickrotate is not None: if xtickrotate == 'vertical': pylab.xticks(xtickv, xticknames, rotation='vertical') else: tmp = dict(rotation=30, ha='right') if isinstance(xtickrotate, dict): tmp.update(xtickrotate) else: tmp['rotation'] = xtickrotate pylab.setp(pylab.xticks()[1], **tmp) if ytickv is not None: ax.yaxis.set_major_locator(matplotlib.ticker.FixedLocator(ytickv)) if yticknames is not None: ax.yaxis.set_major_formatter(matplotlib.ticker.FixedFormatter(yticknames)) if ytickrotate is not None: pylab.setp(pylab.yticks()[1], rotation=ytickrotate, ha='right') # Axis hiding if autoticks: if not ( ( (isinstance(subplt, tuple) and len(subplt) == 3) ) or ( (isinstance(subplt, gridspec.SubplotSpec)) ) ): splog('Cannot setup auto ticks without a proper subplot') else: if isinstance(subplt, gridspec.SubplotSpec): rows,cols,i, _ = subplt.get_geometry() i += 1 # i is 0 indexed. else: rows,cols,i = subplt if ( (i%cols) != 1 ) and (not yticks): yticks = False # if ( i < (cols*(rows-1) + 1) ) and (not xticks): # xticks = False xticks = not ( ( i < (cols*(rows-1) + 1) ) and (not xticks) ) # Tickmark hiding -- used by autoticks as well. if xticks is False: # ax.set_xticklabels([]) ax.set_xlabel('') pylab.setp(ax.get_xticklabels(), visible=False) if yticks is False: # ax.set_yticklabels([]) ax.set_ylabel('') pylab.setp(ax.get_yticklabels(), visible=False) # some nice defaults if grid is True: pylab.grid(b=True, which='major', linestyle='solid', color='0.3', alpha=0.5) ax.set_axisbelow(True) if grid is False: pylab.grid('off') if tickmarks: ax.tick_params('both', which='major', length=5, width=2) ax.tick_params('both', which='minor', length=3, width=1) ax.minorticks_on() else: ax.minorticks_off() pylab.tick_params(axis='both',which='both', bottom='off', top='off', left='off', right='off') if adjust: pylab.subplots_adjust(hspace=hspace, wspace=wspace) if font: # this in theory should work, but fails becuase of the stupidify known as `_` # pylab.rc('font', **{'family':'sans-serif', 'sans-serif':['Helvetica']}) # prop = {'family' : 'normal', 'weight' : 'bold', 'size' : 22} # pylab.rc('font', **{'family':'serif', 'serif':['Computer Modern Roman']}) # pylab.rc('text', usetex=True) pass if aspect is not None: # 'auto', 'equal' ax.set_aspect(aspect) if embiggenx: setup(xr=math.embiggen(ax.axis()[:2],embiggenx)) if embiggeny: setup(yr=math.embiggen(ax.axis()[2:],embiggeny)) if (xticks) and (halfxlog): xax = ax.xaxis xax.set_minor_formatter(matplotlib.ticker.FormatStrFormatter('%g')) tmp = (10.0**(np.arange(-1,5,1)))*5.0 for x,label in zip(xax.get_minorticklocs(), xax.get_minorticklabels()): if x in tmp: label.set_fontsize(8) else: label.set_fontsize(0) pylab.setp(label, visible=False) if secondx: # second x axis tmp = dict(xlabel=None, fcn=lambda arr: arr, fmt='{}', xr=ax.axis()[:2], xlog=('log' in ax.get_xscale()), # yr=ax.axis()[2:] xtickv=ax.get_xticks(), ) # defaults if secondx_prop: tmp.update(secondx_prop) # adjust some nice things f = tmp.pop('fcn') fcn = lambda arr: [f(x) for x in arr] fmt = tmp.pop('fmt') if isinstance(fmt, str): fmt = (fmt,fmt) if 'xtickv' in tmp: tmp['xtickv'] = np.array(tmp['xtickv']) if 'xticknames' not in tmp: arr = np.array(fcn(tmp['xtickv'])) tmp['xticknames'] = np.array([fmt[0].format(x) for x in arr]) if tmp['xlog'] and tmp['nicelog']: ii = np.where((arr >= 1e1)& (arr < 1e4) )[0] tmp['xticknames'][ii] = [fmt[1].format(x) for x in arr[ii]] ii = np.where(arr >= 1e4) tmp['xticknames'][ii] = [fmt[2].format(np.log10(x)) for x in arr[ii]] ax2 = ax.twiny() # I have never understood this ax2.set_xlabel(tmp['xlabel']) ax2.set_xlim(tmp['xr']) if tmp['xlog']: ax2.set_xscale('log', nonposx='clip') if 'xtickv' in tmp: ax2.xaxis.set_major_locator(matplotlib.ticker.FixedLocator(tmp['xtickv'])) if 'xticknames' in tmp: ax2.xaxis.set_major_formatter(matplotlib.ticker.FixedFormatter(tmp['xticknames'])) ax.secondx = ax2 pylab.sca(ax) if secondy: # second x axis tmp = dict(ylabel=None, fcn=lambda arr: arr, fmt='{}', yr=ax.axis()[2:], ylog=('log' in ax.get_yscale()), ytickv=ax.get_yticks(), ) if secondy_prop: tmp.update(secondy_prop) # adjust some nice things f = tmp.pop('fcn') fcn = lambda arr: [f(x) for x in arr] fmt = tmp.pop('fmt') if isinstance(fmt, str): fmt = (fmt,fmt) if 'ytickv' in tmp: tmp['ytickv'] = np.array(tmp['ytickv']) if 'yticknames' not in tmp: arr = np.array(fcn(tmp['ytickv'])) tmp['yticknames'] = np.array([fmt[0].format(x) for x in arr]) if tmp['ylog'] and tmp['nicelog']: ii = np.where((arr >= 1e1)& (arr < 1e4) )[0] tmp['yticknames'][ii] = [fmt[1].format(x) for x in arr[ii]] ii = np.where(arr >= 1e4) tmp['yticknames'][ii] = [fmt[2].format(np.log10(x)) for x in arr[ii]] print(tmp) ax2 = ax.twinx() # I have never understood this ax2.set_ylabel(tmp['ylabel']) ax2.set_ylim(tmp['yr']) if tmp['ylog']: ax2.set_yscale('log', nonposx='clip') if 'ytickv' in tmp: ax2.yaxis.set_major_locator(matplotlib.ticker.FixedLocator(tmp['ytickv'])) if 'yticknames' in tmp: ax2.yaxis.set_major_formatter(matplotlib.ticker.FixedFormatter(tmp['yticknames'])) ax.secondy = ax2 pylab.sca(ax) if xoffset is False: tmp = matplotlib.ticker.ScalarFormatter(useOffset=False) tmp.set_scientific(False) ax.xaxis.set_major_formatter(tmp) if yoffset is False: tmp = matplotlib.ticker.ScalarFormatter(useOffset=False) tmp.set_scientific(False) ax.yaxis.set_major_formatter(tmp) if rasterized: ax.set_rasterized(True) # temp return ax
def plot_simple_demo_1(): pylab.clf() fig = pylab.figure(num=None, figsize=(10, 4)) pylab.subplot(131) title = "Original feature space" pylab.title(title) pylab.xlabel("$X_1$") pylab.ylabel("$X_2$") np.random.seed(3) x1 = np.arange(0, 10, .2) x2 = x1 + np.random.normal(loc=0, scale=1, size=len(x1)) good = (x1 > 5) | (x2 > 5) bad = ~good x1g = x1[good] x2g = x2[good] pylab.scatter(x1g, x2g, edgecolor="blue", facecolor="blue") x1b = x1[bad] x2b = x2[bad] pylab.scatter(x1b, x2b, edgecolor="red", facecolor="white") pylab.grid(True) pylab.subplot(132) X = np.c_[(x1, x2)] # X:[N, 2] pca = decomposition.PCA(n_components=2) Xtrans = pca.fit_transform(X) # Xtrans:[N, 1], 只有一个主成份 #Xtrans *=-1 Xg = Xtrans[good] Xb = Xtrans[bad] """ pylab.scatter( Xg[:, 0], np.zeros(len(Xg)), edgecolor="blue", facecolor="blue") pylab.scatter( Xb[:, 0], np.zeros(len(Xb)), edgecolor="red", facecolor="white") """ pylab.scatter(Xg[:, 0], Xg[:, 1], edgecolor="blue", facecolor="blue") pylab.scatter(Xb[:, 0], Xb[:, 1], edgecolor="red", facecolor="white") title = "Transformed feature space" pylab.title(title) pylab.xlabel("$X'$") fig.axes[1].get_yaxis().set_visible(False) title = "Transformed feature space(1-d)" pylab.subplot(133) pylab.scatter(Xg[:, 0], np.zeros(len(Xg)), edgecolor="blue", facecolor="blue") pylab.scatter(Xb[:, 0], np.zeros(len(Xb)), edgecolor="red", facecolor="white") pylab.title(title) pylab.xlabel("$X'$") fig.axes[2].get_yaxis().set_visible(False) print("======系统pca 数据1==========") print("特征值:", pca.explained_variance_) print("特征重要度比率:", pca.explained_variance_ratio_) # 可以看出, 手工pca与系统pca数据有个符号相反,并不影响分解, # 比如 X=U*S*(V^T) = (-U) S*(-V^T) # 对于二维数据, 其物理意义对应是按照顺时针旋转还是逆时针旋转,并不影响保持最大分量这一物理意义 print("系统Xg:", Xg) pylab.grid(True) pylab.autoscale(tight=True) filename = "pca_demo_1.png" pylab.savefig(os.path.join(CHART_DIR, filename), bbox_inches="tight")
callback = (odl.solvers.CallbackPrintIteration()) # Solve with initial guess x = 0. # Step size parameters are selected to ensure convergence. # See douglas_rachford_pd doc for more information. x = ray_trafo.domain.zero() odl.solvers.douglas_rachford_pd(x, f, g, lin_ops, tau=0.1, sigma=[0.1, 0.1, 0.02], lam=1.5, niter=100, callback=callback) name = 'reco_l2TV_datafunc__lam_' + lam_str + '__l2reg_' + l2_reg_param_str np.savetxt(path_load_result + name, x) fig = x.show(clim=[-1, 1]) plt.axis('off') fig.delaxes(fig.axes[1]) plt.autoscale(False) typefig = 'png' plt.savefig(path_load_result + name + '.' + typefig, transparent=True, bbox_inches='tight', pad_inches=0, format=typefig) plt.close('all')
def plot_correlation(): # rerun # plot 1 np.random.seed(66) pylab.clf() pylab.figure(num=None, figsize=(8, 8)) x = np.arange(0, 10, 0.2) # print x pylab.subplot(221) y = 0.5 * x + norm.rvs(1, scale=0.01, size=len(x)) # print y plot_correlation_func(x, y) pylab.subplot(222) y = 0.5 * x + norm.rvs(1, scale=.1, size=len(x)) plot_correlation_func(x, y) pylab.subplot(223) y = 0.5 * x + norm.rvs(1, scale=1, size=len(x)) plot_correlation_func(x, y) pylab.subplot(224) y = norm.rvs(1, scale=10, size=len(x)) plot_correlation_func(x, y) # pylab.autoscale(tight=True) pylab.grid(True) filename = "a_correlation_pearsonr_1_linear_ok.png" CHART_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "charts") if not os.path.exists(CHART_DIR): os.mkdir(CHART_DIR) pylab.savefig(os.path.join(CHART_DIR, filename), bbox_inches="tight") ######### # plot 2 pylab.clf() pylab.figure(num=None, figsize=(8, 8)) x = np.arange(-5, 5, 0.2) pylab.subplot(221) y = 0.5 * x ** 2 + norm.rvs(1, scale=.01, size=len(x)) plot_correlation_func(x, y) pylab.subplot(222) y = 0.5 * x ** 2 + norm.rvs(1, scale=.1, size=len(x)) plot_correlation_func(x, y) pylab.subplot(223) y = 0.5 * x ** 2 + norm.rvs(1, scale=1, size=len(x)) plot_correlation_func(x, y) pylab.subplot(224) y = 0.5 * x ** 2 + norm.rvs(1, scale=10, size=len(x)) plot_correlation_func(x, y) pylab.autoscale(tight=True) pylab.grid(True) filename = "a_correlation_pearsonr_2_quad_bad.png" pylab.savefig(os.path.join(CHART_DIR, filename), bbox_inches="tight")
def plot_correlation(): # rerun # plot 1 np.random.seed(66) pylab.clf() pylab.figure(num=None, figsize=(8, 8)) x = np.arange(0, 10, 0.2) # print x pylab.subplot(221) y = 0.5 * x + norm.rvs(1, scale=0.01, size=len(x)) # print y plot_correlation_func(x, y) pylab.subplot(222) y = 0.5 * x + norm.rvs(1, scale=.1, size=len(x)) plot_correlation_func(x, y) pylab.subplot(223) y = 0.5 * x + norm.rvs(1, scale=1, size=len(x)) plot_correlation_func(x, y) pylab.subplot(224) y = norm.rvs(1, scale=10, size=len(x)) plot_correlation_func(x, y) # pylab.autoscale(tight=True) pylab.grid(True) filename = "a_correlation_pearsonr_1_linear_ok.png" CHART_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "charts") if not os.path.exists(CHART_DIR): os.mkdir(CHART_DIR) pylab.savefig(os.path.join(CHART_DIR, filename), bbox_inches="tight") ######### # plot 2 pylab.clf() pylab.figure(num=None, figsize=(8, 8)) x = np.arange(-5, 5, 0.2) pylab.subplot(221) y = 0.5 * x**2 + norm.rvs(1, scale=.01, size=len(x)) plot_correlation_func(x, y) pylab.subplot(222) y = 0.5 * x**2 + norm.rvs(1, scale=.1, size=len(x)) plot_correlation_func(x, y) pylab.subplot(223) y = 0.5 * x**2 + norm.rvs(1, scale=1, size=len(x)) plot_correlation_func(x, y) pylab.subplot(224) y = 0.5 * x**2 + norm.rvs(1, scale=10, size=len(x)) plot_correlation_func(x, y) pylab.autoscale(tight=True) pylab.grid(True) filename = "a_correlation_pearsonr_2_quad_bad.png" pylab.savefig(os.path.join(CHART_DIR, filename), bbox_inches="tight")
#pylab.grid(True) #pylab.autoscale(tight=True) ##case 2 LDA good = x1 > x2 bad = ~good lda_inst = lda.LDA(n_components=1) Xtrans = lda_inst.fit_transform(X, good) pylab.clf() fig = pylab.figure(num=None, figsize=(10, 4)) pylab.subplot(121) pylab.xlabel("$X_1$") pylab.ylabel("$X_2$") pylab.scatter(x1[good], x2[good], edgecolors="blue", facecolor="blue") pylab.scatter(x1[bad], x2[bad], edgecolors="red", facecolor="white") pylab.grid(True) pylab.subplot(122) pylab.xlabel("$X'$") pylab.scatter(Xtrans[good], np.zeros(len(Xtrans[good])), edgecolors="blue", facecolor="blue") pylab.scatter(Xtrans[bad], np.zeros(len(Xtrans[bad])), edgecolors="red", facecolor="white") pylab.grid(True) pylab.autoscale(tight=True)
def density_along_orbit(self, ax=None, annotate=True, min_fp_local_length=0, bg_color='dimgrey', cmap=None, vmin=1., vmax=3.): if cmap is None: cmap = plt.cm.autumn cmap.set_bad('dimgrey',0.) cmap.set_under('dimgrey',0.) if ax is None: ax = plt.gca() fp_local_list = [d for d in self.digitization_list if np.isfinite(d.fp_local)] plt.sca(ax) mex.plot_planet(lw=3.) mex.plot_bs(lw=1., ls='dashed', color='k') mex.plot_mpb(lw=1., ls='dotted', color='k') ax.set_aspect('equal', 'box') plt.xlim(2,-2) plt.autoscale(False,tight=True) plt.ylim(0,1.9999) if annotate: plt.annotate('%d' % self.orbit, (0.05, 0.85), xycoords='axes fraction', va='top') def f_x(pos): return pos[0] / mex.mars_mean_radius_km def f_y(pos): return np.sqrt(pos[1]**2. + pos[2]**2.) / mex.mars_mean_radius_km # def f_y(pos): # return pos[2] / mex.mars_mean_radius_km plt.plot( f_x(self.mso_pos), f_y(self.mso_pos), color=bg_color, lw=1., zorder=-10) inx = np.interp(np.array([d.time for d in self.ionogram_list]), self.t, np.arange(self.t.shape[0])) inx = inx.astype(int) plt.plot( f_x(self.mso_pos[:,inx]), f_y(self.mso_pos[:,inx]), color=bg_color, ls='None',marker='o', ms=8.,mew=0., mec=bg_color, zorder=-9) if fp_local_list: val = np.empty_like(self.t) + np.nan # for t, v in [(float(f.time), np.log10(ais_code.fp_to_ne(f.maximum_fp_local))) for f in fp_local_list]: # val[np.abs(self.t - t) < ais_code.ais_spacing_seconds] = v for f in fp_local_list: t = float(f.time) # if (f.fp_local_error / f.fp_local) > 0.3: # v = np.log10(20.) # else: v = np.log10(ais_code.fp_to_ne(f.fp_local)) # print t, ais_code.fp_to_ne(f.fp_local), f.fp_local_error/f.fp_local > 0.3 val[np.abs(self.t - t) < ais_code.ais_spacing_seconds] = v points = np.array([f_x(self.mso_pos), f_y(self.mso_pos)]).T.reshape(-1, 1, 2) segments = np.concatenate([points[:-1], points[1:]], axis=1) lc = LineCollection(segments, cmap=cmap, norm=plt.Normalize(vmin=vmin, vmax=vmax, clip=True)) lc.set_array(val) lc.set_linewidth(5) plt.gca().add_collection(lc) else: lc = None plt.ylabel(r'$\rho / R_M$') # plt.ylabel(r'$z / R_M$') plt.xlabel(r'$x / R_M$') if lc: ticks = [i for i in range(10) if ((float(i)+0.1) > vmin) & ((float(i)-0.1) < vmax)] old_ax = plt.gca() plt.colorbar(lc, cax = celsius.make_colorbar_cax(offset=0.001, height=0.8), ticks=ticks).set_label(r'$log_{10}\;n_e / cm^{-3}$') plt.sca(old_ax)
def modb_along_orbit(self, ax=None, annotate=True, bg_color='dimgrey', cmap=None, vmin=0., vmax=20.): if ax is None: ax = plt.gca() if cmap is None: cmap = plt.cm.autumn cmap.set_bad('dimgrey',0.) cmap.set_under('dimgrey',0.) td_cyclotron_list = [d for d in self.digitization_list if np.isfinite(d.td_cyclotron)] plt.sca(ax) mex.plot_planet(lw=3.) mex.plot_bs(lw=1., ls='dashed', color='k') mex.plot_mpb(lw=1., ls='dotted', color='k') ax.set_aspect('equal','box') plt.xlim(2,-2) plt.autoscale(False,tight=True) plt.ylim(0., 1.999) if annotate: plt.annotate('%d' % self.orbit, (0.05, 0.85), xycoords='axes fraction', va='top') def f_x(pos): return pos[0] / mex.mars_mean_radius_km def f_y(pos): return np.sqrt(pos[1]**2. + pos[2]**2.) / mex.mars_mean_radius_km # def f_y(pos): # return pos[2] / mex.mars_mean_radius_km plt.plot( f_x(self.mso_pos), f_y(self.mso_pos), color=bg_color, lw=2., zorder=-10) inx = np.interp(np.array([d.time for d in self.ionogram_list]), self.t, np.arange(self.t.shape[0])) inx = inx.astype(int) plt.plot(f_x(self.mso_pos[:,inx]), f_y(self.mso_pos[:,inx]), color=bg_color, ls='None',marker='o', ms=8., mew=0., mec=bg_color, zorder=-9) if td_cyclotron_list: val = np.empty_like(self.t) + np.nan for t, v in [(float(f.time), 1E9 * ais_code.td_to_modb(f.td_cyclotron)) for f in td_cyclotron_list]: val[np.abs(self.t - t) < ais_code.ais_spacing_seconds] = v points = np.array([f_x(self.mso_pos), f_y(self.mso_pos)]).T.reshape(-1, 1, 2) segments = np.concatenate([points[:-1], points[1:]], axis=1) lc = LineCollection(segments, cmap=cmap, norm=plt.Normalize(vmin=vmin, vmax=vmax, clip=True)) lc.set_array(val) lc.set_linewidth(5) plt.gca().add_collection(lc) else: lc = None plt.ylabel(r'$\rho / R_M$') # plt.ylabel(r'$z / R_M$') plt.xlabel(r'$x / R_M$') if lc: old_ax = plt.gca() plt.colorbar(lc, cax = celsius.make_colorbar_cax(offset=0.001, height=0.8) ).set_label(r'$|B| / nT$') plt.sca(old_ax)
def plot_simple_demo_my_pca(): pylab.clf() fig = pylab.figure(num=None, figsize=(25, 4)) pylab.subplot(151) title = "Original feature space" pylab.title(title) pylab.xlabel("$X_1$") pylab.ylabel("$X_2$") np.random.seed(3) x1 = np.arange(0, 10, .2) x2 = x1 + np.random.normal(loc=0, scale=1, size=len(x1)) # 两者中,有一个>5就是good good = (x1 > 5) | (x2 > 5) bad = ~good x1g = x1[good] x2g = x2[good] pylab.scatter(x1g, x2g, edgecolor="blue", facecolor="blue") x1b = x1[bad] x2b = x2[bad] pylab.scatter(x1b, x2b, edgecolor="red", facecolor="white") pylab.grid(True) pylab.subplot(152) X = np.c_[(x1, x2)] # X:[N, 2] Xtrans, eigen_values, V, x_mean = my_pca(X, k_components=2, whiten=False) Xtrans_whiten, _, _, _ = my_pca(np.copy(X), k_components=2, whiten=True) Xg = Xtrans[good] Xb = Xtrans[bad] print("======手工pca==========") print("原始特征值 eigen_values:", eigen_values) print("手工Xg:", Xg) print("手工Xg(whiten):", Xtrans_whiten[good]) pylab.scatter(Xg[:, 0], Xg[:, 1], edgecolor="blue", facecolor="blue") pylab.scatter(Xb[:, 0], Xb[:, 1], edgecolor="red", facecolor="white") #Xb[:, 0], np.zeros(len(Xb)), edgecolor="red", facecolor="white") title = "Transformed feature space" pylab.title(title) pylab.xlabel("$X'$") fig.axes[1].get_yaxis().set_visible(True) fig.axes[1].set_xlim(-6, 6) fig.axes[1].set_ylim(-6, 6) #fig.axes.set_ylim(-6,6) # 白化 Xg = Xtrans_whiten[good] Xb = Xtrans_whiten[bad] title = "Transformed feature space(whiten)" pylab.subplot(153) pylab.scatter(Xg[:, 0], Xg[:, 1], edgecolor="blue", facecolor="blue") pylab.scatter(Xb[:, 0], Xb[:, 1], edgecolor="red", facecolor="white") pylab.title(title) pylab.xlabel("$X'$") fig.axes[2].get_yaxis().set_visible(True) fig.axes[2].set_xlim(-6, 6) fig.axes[2].set_ylim(-6, 6) # X_new_1d = X*V[:,0:1] title = "Transformed feature space(1-d)" pylab.subplot(154) pylab.scatter(Xg[:, 0], np.zeros(len(Xg)), edgecolor="blue", facecolor="blue") pylab.scatter(Xb[:, 0], np.zeros(len(Xb)), edgecolor="red", facecolor="white") pylab.title(title) pylab.xlabel("$X'$") fig.axes[3].get_yaxis().set_visible(True) fig.axes[3].set_xlim(-6, 6) fig.axes[3].set_ylim(-6, 6) # X_new_1d = X*V[:,0:1] # 重构信号 X_reconstruct = Xtrans[:, 0:1] * (V[:, 0:1].T) + x_mean Xg = X_reconstruct[good] Xb = X_reconstruct[bad] title = "reconstruct from transformed feature space(1-d)" pylab.xlabel("$X'$") pylab.subplot(155) pylab.scatter(Xg[:, 0], Xg[:, 1], edgecolor="blue", facecolor="blue") pylab.scatter(Xb[:, 0], Xb[:, 1], edgecolor="red", facecolor="white") pylab.title(title) pylab.xlabel("$X'$") fig.axes[4].get_yaxis().set_visible(True) fig.axes[4].set_xlim(-6, 10) fig.axes[4].set_ylim(-6, 10) #pylab.grid(True) pylab.autoscale(tight=True) filename = "my_pca_demo.png" pylab.savefig(os.path.join(CHART_DIR, filename), bbox_inches="tight")
def plot_mi_demo(): # determinism! np.random.seed(0) # plot1 pylab.clf() pylab.figure(num=None, figsize=(8, 8)) x = np.arange(0, 10, 0.2) pylab.subplot(221) y = 0.5 * x + norm.rvs(1, scale=.01, size=len(x)) plot_mi_func(x, y) pylab.subplot(222) y = 0.5 * x + norm.rvs(1, scale=.1, size=len(x)) plot_mi_func(x, y) pylab.subplot(223) y = 0.5 * x + norm.rvs(1, scale=1, size=len(x)) plot_mi_func(x, y) pylab.subplot(224) y = norm.rvs(1, scale=10, size=len(x)) plot_mi_func(x, y) pylab.autoscale(tight=True) pylab.grid(True) filename = "c_mutual_information_1_ok.png" pylab.savefig(os.path.join(CHART_DIR, filename), bbox_inches="tight") ############## # plot 2 pylab.clf() pylab.figure(num=None, figsize=(8, 8)) x = np.arange(-5, 5, 0.2) pylab.subplot(221) y = 0.5 * x**2 + norm.rvs(1, scale=.01, size=len(x)) plot_mi_func(x, y) pylab.subplot(222) y = 0.5 * x**2 + norm.rvs(1, scale=.1, size=len(x)) plot_mi_func(x, y) pylab.subplot(223) y = 0.5 * x**2 + norm.rvs(1, scale=1, size=len(x)) plot_mi_func(x, y) pylab.subplot(224) y = 0.5 * x**2 + norm.rvs(1, scale=10, size=len(x)) plot_mi_func(x, y) pylab.autoscale(tight=True) pylab.grid(True) filename = "c_mutual_information_2_ok.png" pylab.savefig(os.path.join(CHART_DIR, filename), bbox_inches="tight")
def diagnostic(pipe): """ Print a 1-page diagnostic plot of a given pipeline object Args: pipe (terra.pipeline.Pipeline) : pipeline object """ fig = plt.figure(figsize=(20,12)) gs_kw = dict(left=0.04,right=0.99) # Provision the upper plots gs1 = GridSpec(3,10) gs1.update(hspace=0.2, wspace=0.6,bottom=0.65,top=0.95, **gs_kw) axPeriodogram = fig.add_subplot(gs1[0,0:8]) axACF = fig.add_subplot(gs1[0,8]) # These 4 plots all share an xaxis ax_transit = fig.add_subplot(gs1[1,0:2]) ax_transit_zoom = fig.add_subplot(gs1[2,0:2],sharex=ax_transit) ax_phasefold_180 = fig.add_subplot(gs1[1,2:4],sharex=ax_transit) ax_phasefold_secondary = fig.add_subplot(gs1[2,2:4],sharex=ax_transit) ax_phasefold = fig.add_subplot(gs1[1:,4:8]) axTransitTimes = fig.add_subplot(gs1[1,8]) axTransitRp = fig.add_subplot(gs1[2,8]) # Provision the lower plots gs2 = GridSpec(1,5) gs2.update(hspace=0.05,wspace=0.2,bottom=0.05,top=0.6,**gs_kw) axSES = fig.add_subplot(gs2[0 ,0:4]) axTransitStack = fig.add_subplot(gs2[0 ,4]) # Periodogram plt.sca(axPeriodogram) periodogram(pipe) ax = plt.gca() at = AnchoredText('Periodogram',prop=tprop, frameon=True,loc=2) ax.add_artist(at) # Auto correlation plot, and header text, defined to the right plt.sca(axACF) ax = plt.gca() autocorr(pipe) AddAnchored("ACF",prop=tprop,frameon=True,loc=2) # Transit times, holding depth constant plt.sca(axTransitTimes) transits(pipe,mode='transit-times') # Transit radius ratio plt.sca(axTransitRp) transits(pipe,mode='transit-rp') # Phasefolded photometry plt.sca(ax_transit) phasefold_transit(pipe,mode='transit') plt.ylabel('Flux') # Zoomed phase folded photometry plt.sca(ax_transit_zoom) phasefold_transit(pipe,mode='transit-zoom') # Light curve 180 degrees out of phase plt.sca(ax_phasefold_180) phasefold_shifted(pipe, 0.5) # Phase-folded light curve at the most significant secondary eclipse plt.sca(ax_phasefold_secondary) phasefold_shifted(pipe, pipe.se_phase) # Phase-folded all phases plt.sca(ax_phasefold) lc, lcbin = phasefold_shifted(pipe, 0, xdata='phase') yl = [lcbin.fmed.min(),lcbin.fmed.max()] span = lcbin.fmed.ptp() yl[0] -= 0.25 * span yl[1] += 0.25 * span plt.ylim(*yl) plt.autoscale(axis='x',tight=True) plt.xlabel('Phase') plt.ylabel('Flux') # Single Event Statistic Stack plt.sca(axSES) mad = pipe.lc.f.abs().median() ystep = 3 * 1.6 * mad ystep = max(pipe.fit_rp**2 * 1.0, ystep) single_event_statistic_stack(pipe, ystep=ystep) pipe.se_phase plt.axvline(0, alpha=.1,lw=10,color='m',zorder=1) # Single Event Statistic Stack plt.sca(axTransitStack) transit_stack(pipe,ystep=ystep) axL = [ ax_transit, ax_transit_zoom, ax_phasefold_180, ax_phasefold_secondary, ax_phasefold, axTransitTimes, axTransitRp ] for ax in axL: ax.yaxis.set_major_locator(MaxNLocator(4)) axL = [ ax_transit, ax_transit_zoom, ax_phasefold_180, ax_phasefold, axTransitTimes, axTransitRp, axSES, axTransitStack, ] for ax in fig.get_axes(): ax.grid() plt.sca(axACF) text = header_text(pipe) plt.text( 1.1, 1.0, text, family='monospace', size='large', transform=axACF.transAxes, va='top', )
pylab.clf() pylab.figure(num=None, figsize=(8, 8)) x = np.arange(0, 10, 0.2) y = 0.5 * x + norm.rvs(1, scale=.01, size=len(x)) obj.set_data_set(x, y) obj.plot_correlation_func(221) y = 0.5 * x + norm.rvs(1, scale=.1, size=len(x)) obj.set_data_set(x, y) obj.plot_correlation_func(222) y = 0.5 * x + norm.rvs(1, scale=1, size=len(x)) obj.set_data_set(x, y) obj.plot_correlation_func(223) y = norm.rvs(1, scale=10, size=len(x)) obj.set_data_set(x, y) obj.plot_correlation_func(224) pylab.autoscale(tight=True) pylab.grid(True) filename = "corr_demo_1.png" pylab.savefig(os.path.join(CHART_DIR, filename), bbox_inches="tight") pylab.clf() pylab.figure(num=None, figsize=(8, 8)) x = np.arange(-5, 5, 0.2) y = 0.5 * x ** 2 + norm.rvs(1, scale=.01, size=len(x)) obj.set_data_set(x, y) obj.plot_correlation_func(221) y = 0.5 * x ** 2 + norm.rvs(1, scale=.1, size=len(x)) obj.set_data_set(x, y) obj.plot_correlation_func(222) y = 0.5 * x ** 2 + norm.rvs(1, scale=1, size=len(x))