def plot_exact(name, nx, nz, dx, dz): ''' 3D-plot of specified quantity ''' xp = [] for ix in range(nx): x = ix * dx + dx / 2 xp.append(x) zp = [] for iz in range(nz): z = iz * dz + dz / 2 zp.append(z) xp, zp = np.meshgrid(xp, zp) exact = [] rhs = [] for iz in range(nz): for ix in range(nx): x = ix * dx + dx / 2 z = iz * dz + dz / 2 x2 = math.pow(x, 2) x4 = math.pow(x, 4) z2 = math.pow(z, 2) z4 = math.pow(z, 4) exact.append((x2 - x4) * (z4 - z2)) rhs.append(2 * ((1 - 6 * x2) * z2 * (1 - z2) + (1 - 6 * z2) * x2 * (1 - x2))) print 'exact:', len(exact), exact print 'rhs:', len(rhs), rhs #---- First subplot fig = plt.figure() ax = fig.add_subplot(1, 1, 1, projection='3d') surf = ax.plot_surface(xp, zp, exact, rstride=1, cstride=1, cmap=cm.jet, antialiased=False) ax.set_xlabel('x') ax.set_ylabel('y') ax.set_zlabel('z') ax.zaxis.set_major_locator(LinearLocator(10)) ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) fig.colorbar(surf, shrink=0.5, aspect=5) picture = "pictures/error/3d/%s_exact.png" % (name) savefig(picture) plt.close(fig) #show() #---- Second subplot fig2 = plt.figure() ax2 = fig2.add_subplot(1, 1, 1, projection='3d') surf = ax2.plot_surface(xp, zp, rhs, rstride=1, cstride=1, cmap=cm.jet, antialiased=False) ax2.set_xlabel('x') ax2.set_ylabel('y') ax2.set_zlabel('z') ax2.zaxis.set_major_locator(LinearLocator(10)) ax2.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) fig2.colorbar(surf, shrink=0.5, aspect=5) picture = "pictures/error/3d/%s_rhs.png" % (name) savefig(picture) plt.close(fig2)
if __name__ == "__main__": g = twodgaussian(inpars=[0.2, 1.2, 0, 0, 1, 1, 0]) x = numpy.linspace(-5, 5, 30) y = numpy.linspace(-5, 5, 30) import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from matplotlib.ticker import LinearLocator X, Y = numpy.meshgrid(x, y) Z = g(X, Y) + 0.1 * numpy.random.rand(len(x), len(y)) out = gaussfit(Z) print "In pixel unit:", out #plt.pcolor(X, Y, Z) plt.imshow(Z, cmap=plt.cm.jet) plt.colorbar() plt.show() fig = plt.figure() ax = fig.gca(projection='3d') surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.cm.jet, linewidth=0., antialiased=False) ax.set_zlim3d(0, 1.5) ax.w_zaxis.set_major_locator(LinearLocator(11)) fig.colorbar(surf, shrink=0.5, aspect=5) plt.show()
def plot_crosscorr(self, pngfile=None, fitsfile=None): """ """ import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1.axes_divider import make_axes_locatable from matplotlib.colorbar import ColorbarBase from matplotlib.ticker import LinearLocator from matplotlib.colors import Normalize from os import path if fitsfile is not None: from astropy.io import fits prihdr = fits.Header() prihdr.set('INSTRU', 'NenuFAR') prihdr.set('SOFTWARE', 'nenupytv') prihdr.set('VERSION', nenupytv.__version__) prihdr.set('OBSTART', self.cross.time[0].isot) prihdr.set('OBSTOP', self.cross.time[-1].isot) prihdr.set('FREQ', np.mean(self.freq)) prihdr.set('CONTACT', '*****@*****.**') primhdu = fits.PrimaryHDU(data=None, header=prihdr) hdus = [primhdu] c_names = ['XX', 'XY', 'YX', 'YY'] c_indices = [0, 1, 2, 3] for ci, ni in zip(c_indices, c_names): di = np.absolute(np.mean(self.vis[..., ci].data, axis=(0, 1))) hdus.append(fits.ImageHDU(data=di, header=None, name=ni)) hdulist = fits.HDUList(hdus) hdulist.writeto(path.join( fitsfile, 'nenufartv_crossmat_{}.fits'.format( self.cross.time[0].isot.split('.')[0].replace(':', '-'))), overwrite=True) cross_matrix = np.absolute(np.mean(self.vis[..., 0].data, axis=(0, 1))) cross_matrix[np.arange(cross_matrix.shape[0]), np.arange(cross_matrix.shape[1])] = 0. cross_matrix = 10 * np.log10(cross_matrix) vmin = np.percentile(cross_matrix, 5.) vmax = np.percentile(cross_matrix, 99) fig, ax = plt.subplots(figsize=(10, 10)) im = ax.imshow(cross_matrix, origin='lower', cmap='YlGnBu_r', interpolation='nearest', vmin=vmin, vmax=vmax) ax_divider = make_axes_locatable(ax) cax = ax_divider.append_axes("right", size=0.3, pad=0.2) cb = ColorbarBase(cax, cmap='YlGnBu_r', orientation='vertical', norm=Normalize(vmin=vmin, vmax=vmax), ticks=LinearLocator()) cb.solids.set_edgecolor('face') cb.set_label('Amplitude XX (dB)') cb.formatter.set_powerlimits((0, 0)) ax.set_xlabel('MA') ax.set_ylabel('MA') ax.set_title('{}'.format(self.cross.time[self.cross.time.size // 2].iso.split('.')[0])) ax.set_xticks(np.arange(0, self.array.ant1[0, :].size, 1)) ax.set_yticks(np.arange(0, self.array.ant2[:, 0].size, 1)) ax.set_xticklabels(self.array.ant1[0, :], fontsize=7, rotation=45) ax.set_yticklabels(self.array.ant2[:, 0], fontsize=7) ax.grid(color='black', linestyle='-', linewidth=1, alpha=0.1) plt.tight_layout() if pngfile is None: plt.show() else: plt.savefig(pngfile, dpi=300) return
X = numpy.arange(-1, 1, 0.1) Y = numpy.arange(-1, 1, 0.1) X, Y = numpy.meshgrid(X, Y) Z = X**2 + Y**2 colortuple = ('w', 'b') colors = numpy.empty(X.shape, dtype=str) for x in range(len(X)): for y in range(len(Y)): colors[x, y] = colortuple[(x + y) % 2] surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=colors, linewidth=0) ax.set_xlim3d(-1, 1) ax.set_ylim3d(-1, 1) ax.set_zlim3d(0, 2) ax.w_xaxis.set_major_locator(LinearLocator(3)) ax.w_yaxis.set_major_locator(LinearLocator(3)) ax.w_zaxis.set_major_locator(LinearLocator(3)) ax.text(1.79, 0, 1.62, "$C$", fontsize=20) ax.text(0.05, -1.8, 0, "$v_1$", fontsize=20) ax.text(1.5, -0.25, 0, "$v_2$", fontsize=20) plt.show()
def plot(outfile, in_img, actdate, nstations, pyr, csk, ini,cmv, \ xsun, ysun, mask, csl, cmap, features, hist_flag=False, text_flag=False, params=None): fig = plt.figure(figsize=(16,9)) ncols = 5; nrows = 3 # get station index if ini.fcst_flag and nstations > 0: k = [j for j in range(0, nstations) if int(pyr[j].ind) == int(ini.statlist[0])][0] else: k = 0 # Cloud classification if ini.cloud_class_apply: CC_long = ['Cumulus','Cirrus','Altocumulus','Clear Sky','Stratocumulus', 'Stratus', 'Nimbostratus'] CC_short = ['Cu','Ci/Cs','Ac/Cc','Clear','Sc', 'St', 'Ns/Cb'] ccstr_long = CC_long[params['imgclass']-1] ccstr_short = CC_short[params['imgclass']-1] if meta['imgclass'] > 0: cpstr = str(np.round(params['imgprob'][params['imgclass']-1],2)) else: cpstr = "-1" else: ccstr_long = "" ccstr_short = "" cpstr = "" img = cmap.copy() if ini.cbh_flag: from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER #------------------------------------------------------------------- # create map #------------------------------------------------------------------- style = "satellite" # load OSM background image background = patch_image_cache(style, \ ini.rootdir + '/tmp') ax = plt.subplot2grid((nrows,ncols), (0,2), \ colspan=2, rowspan=2, projection=background.crs) # set boundaries of map ax.set_extent((ini.lon_min, ini.lon_max, ini.lat_min, ini.lat_max )) bnd = ax.get_extent() # Add the background to the map res = ini.x_res * ini.grid_size if res > 10000: ax.add_image(background,12,alpha=0.9) elif res > 5000 and res <= 10000: ax.add_image(background,13,alpha=0.9) else: ax.add_image(background,14,alpha=0.9) #ax.imshow(background) gl = ax.gridlines(draw_labels=True, linewidth=1, color='white', alpha=0.6, linestyle='--') gl.xlabels_top = gl.ylabels_right = False gl.xformatter = LONGITUDE_FORMATTER gl.yformatter = LATITUDE_FORMATTER gl.xlabel_style = {'size': 10, 'color': 'black'} gl.ylabel_style = {'size': 10, 'color': 'black'} # draw cloud/shadow map ax.imshow(img,cmap=plt.cm.gray,alpha=0.5, \ zorder=1, vmin=0, transform=background.crs, origin="upper",\ extent=bnd) # Draw a scale bar scale_bar(ax, ini.lat0, ini.lon0, 5, linewidth=10) # Mark camera position sct = ax.scatter(ini.lon0, ini.lat0, \ s=25, marker='x',c="red", transform=background.crs.as_geodetic()) else: # draw cloud map ax = plt.subplot2grid((nrows,ncols), (0,2), colspan=2, rowspan=2) sct = ax.imshow(img, vmin=0, cmap=plt.cm.get_cmap('RdBu_r')) ax.grid('off') plt.title('Irradiance Map') plt.axis('off') # Forecast arrow if ini.flow_flag: # Point forecast xvals = []; yvals = []; vals = [] cm = plt.cm.get_cmap('RdBu_r') cm = cmocean.cm.solar # Draw forecast arrow if ini.draw_forecast_path: for i in range(0, ini.fcst_horizon): inind = int(i / ini.fcst_res) x = int(pyr[k].fpos[i][1]) y = int(pyr[k].fpos[i][0]) if x > cmap.shape[0] - 2 or x <= 0 or y <= 0 or y > cmap.shape[1]-2: continue xvals.append(x); yvals.append(y) cskval = csk.ghi[csk.tind] vals.append(pyr[k].fghi[inind]) if ini.cbh_flag: xvals = np.array(xvals)[np.isfinite(vals)] yvals = np.array(yvals)[np.isfinite(vals)] vals = np.array(vals)[np.isfinite(vals)] lats, lons = misc.grid2latlon(ini.lat0,ini.lon0,ini.x_res, ini.y_res, ini.grid_size, xvals, yvals) if len(xvals) > 0: sct = ax.scatter(lons, lats, s=30, vmin=0.15 * cskval, vmax=cskval + 0.15 * cskval, marker='o', c=vals, cmap=cm, \ edgecolor='none', transform=background.crs.as_geodetic(),zorder=10) # Draw station dots sct2 = plot_stat(ax, ini, nstations, pyr, csk.ghi[csk.tind], k, transform=background.crs.as_geodetic()) else: sct = ax.scatter(xvals, yvals, s=30, vmin=0.15 * csk.ghi[csk.tind], vmax=csk.ghi[csk.tind] + 0.15 * csk.ghi[csk.tind], marker='o', c=vals, cmap=cm, edgecolor='none') # Colorbar try: cbar = plt.colorbar(mappable=sct, pad=.02, aspect=18, shrink=0.85) except ( AttributeError, TypeError, UnboundLocalError ): pass # Select area to cut from image imgsize = in_img.orig_color.shape x0 = int(ini.cy-ini.fx) if x0 < 0: x0 = 0 x1 = int(ini.cy+ini.fx) if x1 > imgsize[0]: x1 = imgsize[0] y0 = int(ini.cx-ini.fy) if y0 < 0: y0 = 0 y1 = int(ini.cx+ini.fy) if y1 > imgsize[1]: y1 = imgsize[1] # Origin Image plt.subplot2grid((nrows,ncols), (0,0), colspan=1, rowspan=1) img = in_img.orig_color_draw.copy() img = cv2.cvtColor(img,cv2.COLOR_RGB2BGR) #img = rotate(img[x0:x1,y0:y1],-np.degrees(ini.rot_angles[2])) cv2.circle(img,(ysun,xsun),15,0,-1) img = img[x0:x1,y0:y1] plt.axis('off') plt.imshow(img) plt.title('Original Image') del img # RBR ax = plt.subplot2grid((nrows,ncols), (1,1)) img = in_img.rbr.copy() * 1.0 img[mask] = np.nan img = img[x0:x1,y0:y1] a = ax.imshow(img,vmin=ini.rbr_thres-0.2, vmax=ini.rbr_thres+0.2,cmap=plt.cm.viridis) cbar = plt.colorbar(a,pad=.03,aspect=15,shrink=0.7, format="%.2f" ) plt.axis('off') if csl == 0: plt.title('RBR') if csl == 1: plt.title('RBR - CSL') if csl == 2: plt.title('RBR corrected') if hist_flag: in_img.rbr[mask]=np.nan plt.subplot2grid((nrows,ncols), (2,0),colspan=1) plt.hist((in_img.rbr.flatten()), \ range=(0.3,1.3), bins=125, color="red",alpha=0.5,normed=True) plt.ylim(0,15) plt.axvline(ini.rbr_thres, color='b', linestyle='dashed', linewidth=2) plt.legend(['RBR threshold','RBR'],loc=2) if ini.csi_mode == "hist" and ini.radiation: ind = pyr[k].tind y = np.divide( pyr[k].ghi[ind-ini.avg_csiminmax:ind], csk.ghi[csk.tind-ini.avg_csiminmax:csk.tind] ) y = y[np.isfinite(y)] if len(y) > (0.6*ini.avg_csiminmax): ax = plt.subplot2grid((nrows,ncols), (2,1),colspan=1) plt.hist((y),bins=ini.hist_bins, color="red",range=(0.0,1.5)) plt.axvline(pyr[k].csi_min, color='b', linestyle='dashed', linewidth=2) plt.axvline(pyr[k].csi_max, color='b', linestyle='dashed', linewidth=2) plt.xlim(0.2,1.5) ax.text(0.2,1.05,'k* histogram',fontsize=9,transform=ax.transAxes) # Clear Sky Reference if csl == 1: plt.subplot2grid((nrows,ncols), (1,0)) img = in_img.cslimage img[mask] = np.nan img = img[x0:x1,y0:y1] a = plt.imshow(img,vmin=0.5, vmax=1.2,cmap=plt.cm.viridis) plt.title('CSL') plt.axis('off') plt.colorbar(a,pad=.03, aspect=15,shrink=0.7) if ini.plot_features: for f in range(0,len(features.vec)): if f > len(features.vec)/2: xo = 0.7; yo = 0.3-(f-len(features.vec)/2)/50. else: xo = 0.43; yo = 0.3-f/50. txt = '%g' % (features.vec[f]) fig.text(xo,yo,features.names[f][0:26]) fig.text(xo+0.17,yo,txt) # RBR differences # img = in_img.rbr_orig - in_img.rbr # plt.subplot2grid((nrows,ncols), (1,0)) # img[mask] = np.nan # a = plt.imshow(img[x0:x1,y0:y1],cmap=plt.cm.get_cmap('bwr'),vmin=-0.2,vmax=0.2) # plt.axis('off') # plt.colorbar(a,pad=.03, aspect=15,shrink=0.7) # plt.title('RBR diff') # Binary cloud mask plt.subplot2grid((nrows,ncols), (0,1)) img = in_img.binary_color.copy() img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) img[in_img.mask_horizon] = 0 img = img[x0:x1,y0:y1] #img = rotate(img,np.degrees(ini.rot_angles[2])) plt.title('Cloud decision') plt.axis('off') plt.imshow(img) # Draw timeseries past = ini.plot_last_vals horizon = int(ini.fcst_horizon/ini.fcst_res) + 1 horizon = int(ini.fcst_horizon) if hist_flag: ax = plt.subplot2grid((nrows,ncols), (2,2),colspan=3) elif ini.plot_features: ax = plt.subplot2grid((nrows,ncols), (2,0),colspan=2) else: ax = plt.subplot2grid((nrows,ncols), (2,0),colspan=5) maxval = 0 i = 0 if ini.radiation: # Plot measurements if ini.live: slc = slice(pyr[k].tind-past,pyr[k].tind,ini.fcst_res) x = pyr[k].time[slc] y = pyr[k].ghi[slc] y2 = pyr[k].dhi[slc] else: slc = slice(pyr[k].tind-past,pyr[k].tind+horizon,ini.fcst_res) x = pyr[k].time[slc] y = pyr[k].ghi[slc] y2 = pyr[k].dhi[slc] dates=[datetime.utcfromtimestamp(ts) for ts in x ] if len(dates) > 0: plt.plot(dates, y, 'b-',lw=2.0, label="Measurement") if len(y2) > 0: fill_between(dates,0,y2,alpha=0.5,linewidth=0,facecolor="yellow", label="DHI") fill_between(dates,y2,y,alpha=0.5,linewidth=0,facecolor="orange", label="DNI") # Analysis Values nvals = ini.plot_last_vals / ini.camera_res / ini.rate x = pyr[k].aghi_time[-int(nvals):] dates=[datetime.utcfromtimestamp(ts) for ts in x if ~np.isnan(ts) ] if len(dates) > 0: y = pyr[k].aghi[-len(dates):] plt.plot(dates, y, 'gv', label="Analysis") # Clear sky irradiance slc = slice(csk.tind-ini.plot_last_vals, csk.tind+ini.fcst_horizon, ini.fcst_res) x = csk.time[slc] dates=[datetime.utcfromtimestamp(ts) for ts in x ] y = csk.ghi[slc] plt.plot(dates, y, '--', color='black', label="Clear Sky") maxval = 1.7 * csk.actval plt.ylabel('Irradiance in $Wm^{-2}$') # Forecast Values x = pyr[k].ftime dates=[ datetime.utcfromtimestamp(ts) for ts in x if ~np.isnan(ts) ] y = pyr[k].fghi[:len(dates)] plt.plot(dates,y,'r-',lw=2.0, label="Forecast") # Vertical line to plot current time instance plt.axvline(actdate, color='b', linestyle='--', lw=2.0) plt.xlabel('Time [UTC]') plt.legend(loc="upper left", ncol=3, fontsize=8) plt.ylim([0,maxval]) ax.xaxis.set_major_formatter(DateFormatter('%H:%M:%S')) ax.xaxis.set_major_locator(LinearLocator(numticks=6)) ax.xaxis_date() # Draw Text ax = plt.subplot2grid((nrows,ncols), (0,4),rowspan=2) ax.axis('off') nowtime = datetime.strftime(datetime.utcnow(),"%Y-%m-%d %H:%M:%S") acttime = datetime.strftime(actdate,"%Y-%m-%d %H:%M:%S") loctime = str(in_img.locdate.isoformat(' ')) ax.text(-0.3,0.95,acttime + str(' UTC'), weight="bold") ax.text(0.2,0.02,'Created:\n' + nowtime + str(' UTC'),fontsize=9) ax.text(-0.3,0.9,"Sun Zenith = " + str(round(params['sza'],1)) + '$^\circ$' ) ax.text(-0.3,0.86,"Sun Azimuth = " + str(round(params['saz'],1)) + '$^\circ$' ) if ini.cbh_flag: ax.text(-0.3,0.82,'Cloud Base Height: ' + \ str(int(params['cbh'])) + ' m ') ax.text(-0.3,0.79,'Cloud Type: ' + ccstr_long + ' ' + ccstr_short + ' ' + cpstr) ax.text(-0.3,0.72,'Radiation measurements \n' + params['txt'] + ':' ) ax.text(-0.3,0.65,"GHI = " + str(round(params['ghi'],1)) + ' $W/m^2$ (' + str(round(params['csi_ghi'],2))+')' ) ax.text(-0.3,0.61,"DHI = " + str(round(params['dhi'],1)) + ' $W/m^2$ (' + str(round(params['csi_dhi'],2))+')' ) ax.text(-0.3,0.57,"DNI = " + str(round(params['dni'],1)) + ' $W/m^2$ (' + str(round(params['csi_dni'],2))+')' ) if ini.mode <= 1: ax.text(-0.3,0.40,'Cloud Cover = ' + str(round(params['cc'],1)) + ' %' ) if ini.flow_flag: if ini.cbh_flag: unit = "m/s" else: unit = "pix/s" ax.text(-0.3, 0.34, '#CMV = ' + str(np.sum(cmv.flag))) um = cmv.speed[-1]; vm = cmv.direction[-1] ume = cmv.sspeed[-1]; vme = cmv.sdirection[-1] ax.text(-0.3,0.30,'All speed = ' + str(round(um,2)) + '$\pm$' + str(round(ume,2)) + unit) ax.text(-0.3,0.26,'All direction = ' + str(round(np.degrees(vm),2)) + '$\pm$' + str(round(np.degrees(vme),2)) +'$^\circ$') um = cmv.mean_speed; vm = cmv.mean_direction ume = cmv.std_speed; vme = cmv.std_direction ax.text(-0.3,0.22,'Global speed = ' + str(round(um,2)) + '$\pm$' + str(round(ume,2)) + unit) ax.text(-0.3,0.18,'Global direction = ' + str(round(np.degrees(vm),2)) + '$\pm$' + str(round(np.degrees(vme),2)) +'$^\circ$') ax.text(-0.3,0.14,'Lens Clear = ' + str(params['img_qc'])) if in_img.useful_image: qc = "OK" else: qc = "BAD" ax.text(-0.3,0.10,'Quality Flag = ' + qc) # Final settings fig.set_figwidth = 16. fig.set_figheight = 9. fig.set_dpi = 50. fig.subplots_adjust(hspace=0.15,wspace=0.4,left=0.05, right=0.97, top=0.95, bottom=0.08) plt.savefig(outfile,format=ini.outformat) plt.clf() plt.close('all')
def main(): log = logging.getLogger(__name__) experiment_name = "tunnel_simulation" setup_logger(f"logs/{experiment_name}.log", logging.WARNING) log.info(f"Running experiment: {experiment_name}") np.random.seed(2) num_iter = 3 # Meas model pos = np.array([100, -100]) # sigma_r = 2 # sigma_phi = 0.5 * np.pi / 180 sigma_r = 4 sigma_phi = 1 * np.pi / 180 R = np.diag([sigma_r**2, sigma_phi**2]) meas_model = RangeBearing(pos, R) # Generate data range_ = (0, None) tunnel_segment = [145, 165] # tunnel_segment = [None, None] states, measurements = get_states_and_meas(meas_model, R, range_, tunnel_segment) cartes_meas = np.apply_along_axis(partial(to_cartesian_coords, pos=pos), 1, measurements) prior_mean = np.array([0, 0, 1, 0, 0]) prior_cov = np.diag([0.1, 0.1, 1, 1, 1]) results = [] sigma_point_method = SphericalCubature() # cost_fn_ipls = partial( # slr_smoothing_cost_pre_comp, # measurements=measurements, # m_1_0=prior_mean, # P_1_0=prior_cov, # motion_model=motion_model, # meas_model=meas_model, # slr=SigmaPointSlr(sigma_point_method), # ) vs = np.array([3, 4, 5, 6, 7]) os = np.array([15, 17.5, 20, 22.5, 25]) rmses = np.empty((vs.shape[0], os.shape[0])) sampling_period = 0.1 eps = 0.1 # v_scale = 2 # omega_scale = 2 for v_iter, v_scale in enumerate(vs): for o_iter, omega_scale in enumerate(os): # Motion model sigma_v = v_scale * 1 sigma_omega = omega_scale * np.pi / 180 Q = np.diag([ eps, eps, sampling_period * sigma_v**2, eps, sampling_period * sigma_omega**2 ]) motion_model = CoordTurn(sampling_period, Q) cost_fn_eks = partial( analytical_smoothing_cost, meas=measurements, m_1_0=prior_mean, P_1_0=prior_cov, motion_model=motion_model, meas_model=meas_model, ) ms_gn_ieks, Ps_gn_ieks, cost_gn_ieks, tmp_rmse, tmp_nees = run_smoothing( Ieks(motion_model, meas_model, num_iter), states, measurements, prior_mean, prior_cov, cost_fn_eks) tmp = rmse(ms_gn_ieks[:, :2], states) print(v_scale, omega_scale, tmp) rmses[v_iter, o_iter] = tmp fig = plt.figure() ax = fig.gca(projection="3d") X, Y = np.meshgrid(vs, os) surf = ax.plot_surface(X, Y, rmses, linewidth=0, antialiased=False) from matplotlib.ticker import LinearLocator, FormatStrFormatter ax.zaxis.set_major_locator(LinearLocator(10)) ax.zaxis.set_major_formatter(FormatStrFormatter("%.02f")) ax.set_xlabel("v") ax.set_ylabel("o") # Add a color bar which maps values to colors. fig.colorbar(surf, shrink=0.5, aspect=5) plt.show() results.append((ms_gn_ieks, Ps_gn_ieks, cost_gn_ieks[1:], "GN-IEKS")) # ms_gn_ipls, Ps_gn_ipls, cost_gn_ipls, rmses_gn_ipls, neeses_gn_ipls = run_smoothing( # SigmaPointIpls(motion_model, meas_model, sigma_point_method, num_iter), # states, # measurements, # prior_mean, # prior_cov, # cost_fn_ipls, # None, # ) # results.append((ms_lm_ipls, Ps_lm_ipls, cost_lm_ipls[1:], "LM-IPLS")) plot_results( states, results, cartes_meas, ) plot_metrics( [ (cost_gn_ieks[1:], "GN-IEKS"), (cost_lm_ieks[1:], "LM-IEKS"), (cost_gn_ipls[1:], "GN-IPLS"), (cost_lm_ipls[1:], "LM-IPLS"), ], [ (rmses_gn_ieks, "GN-IEKS"), (rmses_lm_ieks, "LM-IEKS"), (rmses_gn_ipls, "LM-IPLS"), (rmses_lm_ipls, "LM-IPLS"), ], [ (neeses_gn_ieks, "GN-IEKS"), (neeses_lm_ieks, "LM-IEKS"), (neeses_gn_ipls, "LM-IPLS"), (neeses_lm_ipls, "LM-IPLS"), ], )
file.write(datastr) time.sleep(0.5 + 0.28) except Exception as e: print(Exception) except KeyboardInterrupt: p.stop() GPIO.cleanup() file.close() fig, ax = plt.subplots() ax.set_xlabel("time [s]") ax.set_ylabel("temperature [°C]", color='red') ax.plot(times, temp, label="Temperature", color='red') ax.plot(times, oxytemp, label="First Temperature", color='green') ax.get_yaxis().set_major_locator(LinearLocator(numticks=12)) ax2 = ax.twinx() ax2.set_ylabel("correction [-]", color='blue') ax2.plot(times, corr, label="Correction", color='blue') ax.grid() plt.show()
def format_ax(self, ax, **kwargs): """ ax = ax font = FONT0 # 字体 x_label = None # X 轴标签 y_label = None # Y 轴标签 x_label_font_size = 11 # X 轴标签字体 y_label_font_size = 11 # Y 轴标签字体 x_major_count = None # X 轴主刻度数量 x_minor_count = None # Y 轴主刻度数量 y_major_count = None # X 轴次刻度数量 y_minor_count = None # Y 轴次刻度数量 x_axis_min = None # X 轴最小值 x_axis_max = None # X 轴最大值 y_axis_min = None # Y 轴最小值 y_axis_max = None # Y 轴最大值 tick_font = FONT0 # 刻度字体 tick_font_size = 11 # 刻度文字字体大小 tick_font_color = '#000000' annotate = None # 注释 annotate_font_size = 11 # 注释字体大小 annotate_font_color = '#000000' timeseries = None :param ax: :param kwargs: (dict) :return: """ # 设置字体 if 'font' in kwargs: self.font = kwargs.get('font') if 'annotate_font' in kwargs: self.annotate_font = kwargs.get('annotate_font') if 'label_font' in kwargs: self.label_font = kwargs.get('label_font') # 设置 label if 'x_label' in kwargs: x_label = kwargs.get('x_label') if 'x_label_font_size' in kwargs: x_label_font_size = kwargs.get('x_label_font_size') else: x_label_font_size = self.x_label_font_size ax.set_xlabel(x_label, fontsize=x_label_font_size, fontproperties=self.label_font) if 'y_label' in kwargs: y_label = kwargs.get('y_label') if 'y_label_font_size' in kwargs: y_label_font_size = kwargs.get('y_label_font_size') else: y_label_font_size = self.y_label_font_size ax.set_ylabel(y_label, fontsize=y_label_font_size, fontproperties=self.label_font) # 设置 x 轴的范围 if 'x_axis_min' in kwargs and 'x_axis_max' in kwargs: x_axis_min = kwargs.get('x_axis_min') x_axis_max = kwargs.get('x_axis_max') ax.set_xlim(x_axis_min, x_axis_max) # 设置大刻度的数量 if 'x_interval' in kwargs: x_interval = kwargs.get('x_interval') x_major_count = int((x_axis_max - x_axis_min) / x_interval + 1) ax.xaxis.set_major_locator(LinearLocator(x_major_count)) # 设置小刻度的数量 if x_major_count <= 11: x_minor_count = 4 else: x_minor_count = 1 ax.xaxis.set_minor_locator( LinearLocator((x_major_count - 1) * (x_minor_count + 1) + 1)) # 如果是长时间序列图,设置长时间序列x轴日期坐标 if kwargs.get('timeseries'): self.set_timeseries_x_locator(ax, x_axis_min, x_axis_max) # 设置 y 轴的范围 if 'y_axis_min' in kwargs and 'y_axis_max' in kwargs: y_axis_min = kwargs.get('y_axis_min') y_axis_max = kwargs.get('y_axis_max') ax.set_ylim(y_axis_min, y_axis_max) # 设置大刻度的数量 if 'y_interval' in kwargs: y_interval = kwargs.get('y_interval') y_major_count = (y_axis_max - y_axis_min) / y_interval + 1 ax.yaxis.set_major_locator(LinearLocator(y_major_count)) # 设置小刻度的数量 if y_major_count <= 11: y_minor_count = 4 else: y_minor_count = 1 ax.yaxis.set_minor_locator( LinearLocator((y_major_count - 1) * (y_minor_count + 1) + 1)) if 'tick_font' in kwargs: self.tick_font = kwargs['tick_font'] if 'tick_font_color' in kwargs: self.tick_font_color = kwargs['tick_font_color'] if 'tick_font_size' in kwargs: self.tick_font_size = kwargs['tick_font_size'] set_tick_font(ax, font=self.tick_font, color=self.tick_font_color, font_size=self.tick_font_size) # 设置图片注释文字 if 'annotate' in kwargs: annotate = kwargs.get('annotate') self.annotate_font = kwargs.get('annotate_font', self.annotate_font) self.annotate_font_color = kwargs.get('annotate_font_color', self.annotate_font_color) self.annotate_font_size = kwargs.get('annotate_font_size', self.annotate_font_size) if 'font_size' in annotate: font_size = annotate.get('font_size') else: font_size = self.annotate_font_size if 'font_color' in annotate: font_color = annotate.get('font_color') else: font_color = self.annotate_font_color for k in annotate: add_annotate(ax, annotate[k], k, fontsize=font_size, color=font_color, font=self.annotate_font)
def vis_explanation(self, number): if len(self.explainVis) == 0: for i, batch in enumerate(self.test_loader): self.explainVis = batch break # oldIndices = self.test_loader.indices.copy() # self.test_loader.indices = self.test_loader.indices[:2] # datasetLoader = self.test_loader layer_gc = LayerGradCam(self.model, self.model.layer2[1].conv2) # for i, batch in enumerate(datasetLoader): lb = self.explainVis[1].to(device) # print(len(lb)) img = self.explainVis[0].to(device) # plt.subplot(2,1,1) # plt.imshow(img.squeeze().cpu().numpy()) pred = self.model(img) predlb = torch.argmax(pred, 1) imgCQ = img.clone() # print('Prediction label is :',predlb.cpu().numpy()) # print('Ground Truth label is: ',lb.cpu().numpy()) ##explain to me : gc_attr = layer_gc.attribute(imgCQ, target=predlb, relu_attributions=False) upsampled_attr = LayerAttribution.interpolate(gc_attr, (64, 64)) gc_attr = layer_gc.attribute(imgCQ, target=lb, relu_attributions=False) upsampled_attrB = LayerAttribution.interpolate(gc_attr, (64, 64)) if not os.path.exists('./pic'): os.mkdir('./pic') ####PLot################################################ plotMe = viz.visualize_image_attr( upsampled_attr[7].detach().cpu().numpy().transpose([1, 2, 0]), original_image=img[7].detach().cpu().numpy().transpose([1, 2, 0]), method='heat_map', sign='all', plt_fig_axis=None, outlier_perc=2, cmap='inferno', alpha_overlay=0.2, show_colorbar=True, title=str(predlb[7]), fig_size=(8, 10), use_pyplot=True) plotMe[0].savefig('./pic/' + str(number) + 'NotEQPred.jpg') ################################################ plotMe = viz.visualize_image_attr( upsampled_attrB[7].detach().cpu().numpy().transpose([1, 2, 0]), original_image=img[7].detach().cpu().numpy().transpose([1, 2, 0]), method='heat_map', sign='all', plt_fig_axis=None, outlier_perc=2, cmap='inferno', alpha_overlay=0.9, show_colorbar=True, title=str(lb[7].cpu()), fig_size=(8, 10), use_pyplot=True) plotMe[0].savefig('./pic/' + str(number) + 'NotEQLabel.jpg') ################################################ outImg = img[7].squeeze().detach().cpu().numpy() fig2 = plt.figure(figsize=(12, 12)) prImg = plt.imshow(outImg) fig2.savefig('./pic/' + str(number) + 'NotEQOrig.jpg') ################################################ fig = plt.figure(figsize=(15, 10)) ax = fig.add_subplot(111, projection='3d') z = upsampled_attr[7].squeeze().detach().cpu().numpy() x = np.arange(0, 64, 1) y = np.arange(0, 64, 1) X, Y = np.meshgrid(x, y) plll = ax.plot_surface(X, Y, z, cmap=cm.coolwarm) # Customize the z axis. # ax.set_zlim(np.min(z)+0.1*np.min(z),np.max(z)+0.1*np.max(z)) ax.set_zlim(-0.02, 0.1) ax.zaxis.set_major_locator(LinearLocator(10)) ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) # Add a color bar which maps values to colors. fig.colorbar(plll, shrink=0.5, aspect=5) fig.savefig('./pic/' + str(number) + 'NotEQ3D.jpg')
def plot_2D_marginal_probs(marginals, bins, sample_set, filename="file", lam_ref=None, plot_surface=False, interactive=False, lambda_label=None, file_extension=".png"): """ This makes plots of every pair of marginals (or joint in 2d case) of input probability measure on a rectangular grid. If the sample_set object is a discretization object, we assume that the probabilities to be plotted are from the input space. .. note:: Do not specify the file extension in the file name. :param marginals: 2D marginal probabilities :type marginals: dictionary with tuples of 2 integers as keys and :class:`~numpy.ndarray` of shape (nbins+1,) as values :param bins: Endpoints of bins used in calculating marginals :type bins: :class:`~numpy.ndarray` of shape (nbins+1,2) :param sample_set: Object containing samples and probabilities :type sample_set: :class:`~bet.sample.sample_set_base` or :class:`~bet.sample.discretization` :param filename: Prefix for output files. :type filename: str :param lam_ref: True parameters. :type lam_ref: :class:`~numpy.ndarray` of shape (ndim,) or None :param interactive: Whether or not to display interactive plots. :type interactive: bool :param lambda_label: Label for each parameter for plots. :type lambda_label: list of length nbins of strings or None :param string file_extension: file extenstion """ if isinstance(sample_set, sample.discretization): sample_obj = sample_set._input_sample_set elif isinstance(sample_set, sample.sample_set_base): sample_obj = sample_set else: raise bad_object("Improper sample object") if lam_ref is None: lam_ref = sample_obj._reference_value lam_domain = sample_obj.get_domain() from matplotlib import cm if plot_surface: from mpl_toolkits.mplot3d import Axes3D from matplotlib.ticker import LinearLocator, FormatStrFormatter if comm.rank == 0: pairs = copy.deepcopy(marginals.keys()) pairs.sort() for k, (i, j) in enumerate(pairs): fig = plt.figure(k) ax = fig.add_subplot(111) boxSize = (bins[i][1] - bins[i][0]) * (bins[j][1] - bins[j][0]) quadmesh = ax.imshow(marginals[(i, j)].transpose() / boxSize, interpolation='bicubic', cmap=cm.CMRmap_r, extent=[ lam_domain[i][0], lam_domain[i][1], lam_domain[j][0], lam_domain[j][1] ], origin='lower', vmax=marginals[(i, j)].max() / boxSize, vmin=0, aspect='auto') if lam_ref is not None: ax.plot(lam_ref[i], lam_ref[j], 'wo', markersize=10) if lambda_label is None: label1 = r'$\lambda_{' + str(i + 1) + '}$' label2 = r'$\lambda_{' + str(j + 1) + '}$' else: label1 = lambda_label[i] label2 = lambda_label[j] ax.set_xlabel(label1, fontsize=20) ax.set_ylabel(label2, fontsize=20) ax.tick_params(axis='both', which='major', labelsize=14) label_cbar = r'$\rho_{\lambda_{' + str(i + 1) + '}, ' label_cbar += r'\lambda_{' + str(j + 1) + '}' + '}$ (Lebesgue)' cb = fig.colorbar(quadmesh, ax=ax, label=label_cbar) cb.ax.tick_params(labelsize=14) cb.set_label(label_cbar, size=20) plt.axis([ lam_domain[i][0], lam_domain[i][1], lam_domain[j][0], lam_domain[j][1] ]) fig.savefig(filename + "_2D_" + str(i) + "_" + str(j) +\ file_extension, transparent=True) if interactive: plt.show() else: plt.close() if plot_surface: for k, (i, j) in enumerate(pairs): fig = plt.figure(k) ax = fig.gca(projection='3d') X = bins[i][:-1] + np.diff(bins[i]) / 2 Y = bins[j][:-1] + np.diff(bins[j]) / 2 X, Y = np.meshgrid(X, Y, indexing='ij') surf = ax.plot_surface(X, Y, marginals[(i, j)], rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0, antialiased=False) ax.zaxis.set_major_locator(LinearLocator(10)) ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) ax.set_xlabel(r'$\lambda_{' + str(i + 1) + '}$') ax.set_ylabel(r'$\lambda_{' + str(j + 1) + '}$') ax.set_zlabel(r'$P$') plt.backgroundcolor = 'w' fig.colorbar(surf, shrink=0.5, aspect=5, label=r'$P$') fig.savefig(filename + "_surf_" + str(i) + "_" + str(j) + \ file_extension, transparent=True) if interactive: plt.show() else: plt.close() plt.clf() comm.barrier()
def plot_field(name, directory, tstamps, samples, nr_samples, plot_fmt=None, table=None, verbose=False): global current_plot_fmt from matplotlib import use as muse muse('Agg') from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas from matplotlib.figure import Figure from matplotlib.ticker import FuncFormatter, FixedFormatter, LinearLocator from matplotlib.mlab import std as std_deviation from matplotlib.mlab import mean from time import asctime yfont = {'fontname': 'Bitstream Vera Sans', 'color': 'r', 'fontsize': 8} xfont = {'fontname': 'Bitstream Vera Sans', 'color': 'b', 'fontsize': 8} titlefont = { 'fontname': 'Bitstream Vera Sans', 'color': 'g', 'fontweight': 'bold', 'fontsize': 10 } inches = 0.00666667 width = 950 * inches height = 680 * inches fig = Figure(figsize=(width, height)) canvas = FigureCanvas(fig) ax = fig.add_subplot(111) ax.grid(False) xtickfontsize = 5 ytickfontsize = 5 current_plot_fmt = plot_fmt field_mean = None plot_type = 'b-' if current_plot_fmt == "filter_dev": std = std_deviation(samples) * 2 if verbose: print "filter_dev(%s) std=%d" % (name, std) for i in range(nr_samples): if samples[i] > std: if verbose: print "%s: filtering out %d" % (name, samples[i]) samples[i] = 0 field_mean = mean(samples) yaxis_plot_fmt = FuncFormatter(pylab_formatter) elif current_plot_fmt == "table": ax.grid(True) plot_type = 'bo-' max_value = max(samples) without_zero = 1 if table.has_key(0): without_zero = 0 max_value += 1 ax.yaxis.set_major_locator(LinearLocator(max_value)) tstamps = range(nr_samples) seq = [" "] * max_value for key in table.keys(): if key in samples: seq[key - without_zero] = "%s(%d)" % (table[key], key) ytickfontsize = 4 yaxis_plot_fmt = FixedFormatter(seq) else: field_mean = mean(samples) yaxis_plot_fmt = FuncFormatter(pylab_formatter) ax.plot(tstamps, samples, plot_type) ax.set_xlabel("time", xfont) yname = name if field_mean: yname += " (mean=%s)" % pylab_formatter(field_mean) ax.set_ylabel(yname, yfont) for label in ax.get_xticklabels(): label.set(fontsize=xtickfontsize) for label in ax.get_yticklabels(): label.set(fontsize=ytickfontsize) ax.yaxis.set_major_formatter(yaxis_plot_fmt) ax.set_title("%d %s samples (%s)" % (nr_samples, name, asctime()), titlefont) canvas.print_figure("%s/%s.png" % (directory, name)) del fig, canvas, ax
def plot_fem_solution(grid): from grid.Grid import Grid from grid.NodeTable import node_iterator from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter from matplotlib.pyplot import show, figure from scipy.interpolate import griddata from numpy import mgrid, array # # The connection values are set separately with respect to the location, for i in range(len(NodeID_Load)): grid.set_value(NodeID_Load[i]._id_no, value_vector[i]) for i in range(len(BoundaryNodeLoad_Ordered)): grid.set_value(BoundaryNodeLoad_Ordered[i].get_node_id()._id_no, boundary_value[i]) #grid=Grid() # #Find the position of the nodes and the values node_x = [] node_y = [] node_v = [] for node in node_iterator(grid): coord = node.get_coord() node_x.append(coord[0]) node_y.append(coord[1]) node_v.append(node.get_value()) # Store the results in an array node_x = array(node_x) node_y = array(node_y) node_v = array(node_v) print('node_x', node_x) print('node_value', node_v) # Initialise the figure fig = figure() ax = fig.gca(projection='3d') ax = fig.gca() # Interpolate the nodes onto a structured mesh X, Y = mgrid[node_x.min():node_x.max():10j, node_y.min():node_y.max():10j] Z = griddata((node_x, node_y), node_v, (X, Y), method='cubic') # Make a surface plot surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm, linewidth=0, antialiased=False) # Set the z axis limits ax.set_zlim(node_v.min(), node_v.max()) # Make the ticks looks pretty ax.zaxis.set_major_locator(LinearLocator(10)) ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) # Include a colour bar fig.colorbar(surf, shrink=0.5, aspect=5) # Show the plot show()
def getGraphWithMapping(fig, og, mapping, alpha=0.05, subsample=4, degree=None, showSurface=True, root=None, colormap='jet'): ax = Axes3D(fig) edges = og.edges(data=True) nodes = og.nodes(data=True) narray = np.zeros((len(nodes), 3), np.float32) axes = ax.get_axes() axes.set_axis_off() ax.set_axes(axes) pp.set_cmap(colormap) vmax = np.max(list(mapping.values())) vmin = np.min(list(mapping.values())) cNorm = colors.Normalize(vmin=vmin, vmax=vmax) scalarMap = cm.ScalarMappable(norm=cNorm, cmap=pp.get_cmap(colormap)) if (root): rcrd = og.node[root]["wcrd"] ax.text(rcrd[0], rcrd[1], root[2], "Root") ss = 4 for e in edges: try: sp = e[2]['d0'] mps = e[2]['mappedPoints'] clr = scalarMap.to_rgba(mapping[(e[0], e[1])]) print("%s with mapping %s maps to color %s" % ((e[0], e[1]), mapping[(e[0], e[1])], clr)) ax.plot(sp[0], sp[1], sp[2], color=clr) if showSurface: try: ax.scatter(mps[::ss, 0], mps[::ss, 1], mps[::ss, 2], color=clr, marker='+', alpha=alpha) except Exception: pass except KeyError: print("cannot plot edge (%s,%s)" % (e[0], e[1])) except TypeError: if (e[2]['d0'] == None): print("No fitted data for edge (%s,%s)" % (e[0], e[1])) ax.scatter(narray[:, 0], narray[:, 1], narray[:, 2], color='k', marker='o', linewidth=3, picker=5) ax.scatter([rcrd[0]], [rcrd[1]], [rcrd[2]], color='r', marker='o', linewidth=10, picker=5) ax.w_zaxis.set_major_locator(LinearLocator(3)) ax.w_xaxis.set_major_locator(LinearLocator(3)) ax.w_yaxis.set_major_locator(LinearLocator(3)) return ax
def viewGraphWithMidPlane(og, fignum=1, labelNodes=False, alpha=0.05, subsample=4, verbose=True, degree=None, showMidPlane=True, root=None): """view an ordered graph generated using the SkeltonGraph class""" if (verbose): print("viewing graph") edges = og.edges(data=True) nodes = og.nodes(data=True) narray = np.zeros((len(nodes), 3), np.float32) fig1 = pp.figure(fignum) ax = Axes3D(fig1) axes = ax.get_axes() axes.set_axis_off() ax.set_axes(axes) for i in range(len(nodes)): narray[i, :] = nodes[i][1]['wcrd'] dg = og.degree(nodes[i][0]) if (labelNodes and (degree == None or dg == degree)): ax.text( narray[i, 0], narray[i, 1], narray[i, 2], "(%d,%d,%d)" % (nodes[i][0][0], nodes[i][0][1], nodes[i][0][2])) print("root is", root) if (root): rcrd = og.node[root]["wcrd"] ax.text(rcrd[0], rcrd[1], root[2], "Root") colors = ['r', 'g', 'b', 'y', 'm', 'c'] counter = 0 ss = 4 for e in edges: sp = e[2]['d0'] pp = e[2]['planePoints'] clr = colors[counter % len(colors)] ax.plot(sp[0], sp[1], sp[2], color=clr) if (showMidPlane): midpoint = len(list(pp.keys())) / 2 mps = e[2]['planePoints'][midpoint] try: ax.scatter(mps[::ss, 0], mps[::ss, 1], mps[::ss, 2], color=clr, marker='+', alpha=0.05) except Exception as error: print("couldn't plot surface for edge (%s,%s). Surface points shape is %s. Error is %s"%\ (e[0],e[1],mps.shape,error)) counter += 1 ax.scatter(narray[:, 0], narray[:, 1], narray[:, 2], color='k', marker='o', linewidth=3) ax.w_zaxis.set_major_locator(LinearLocator(3)) ax.w_xaxis.set_major_locator(LinearLocator(3)) ax.w_yaxis.set_major_locator(LinearLocator(3)) pp.show()
def plotstorms(flowserie, rainserie, selected_storm, tsfreq=None, tsfrequnit=None, make_comparable=False, period_title=False): """ Plot Flow-Rain plots for every storm period selected, optimal sizes and configuration done for 1 till 5 subplots (storms) """ if len(selected_storm) > 6: raise Exception('Split plotting up in multiple figures') fig = plt.figure(facecolor='white', figsize=(12, _getsize(len(selected_storm)))) gs0 = gridspec.GridSpec(len(selected_storm), 1) gs0.update(hspace=0.35) for j, storm in enumerate(selected_storm): gs00 = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=gs0[j], hspace=0.0, height_ratios=[2, 4]) # RAIN PLOT ax0 = fig.add_subplot(gs00[0]) ax0.plot(rainserie[storm['startdate']:storm['enddate']].index. to_pydatetime(), rainserie[storm['startdate']:storm['enddate']].values, linestyle='steps') # FLOW PLOT stormflow = flowserie[storm['startdate']:storm['enddate']] ax1 = fig.add_subplot(gs00[1], sharex=ax0) ax1.plot(stormflow.index.to_pydatetime(), stormflow.values, label=r" Measured Flow ($m^3s^{-1}$)") # if single plots of flow/rain -> set specific color if flowserie.ndim == 1: ax1.lines[0].set_color('#08519c') if rainserie.ndim == 1: ax0.lines[0].set_color('#6baed6') # ADAPT ticks for storm-conditions (less than a month timeseries) ax0.yaxis.set_major_locator(LinearLocator(3)) ax1.yaxis.set_major_locator(LinearLocator(3)) ax1.xaxis.set_minor_locator(mpl.dates.DayLocator()) ax1.xaxis.set_minor_formatter(mpl.dates.DateFormatter('%d')) ax1.xaxis.set_major_locator(mpl.dates.MonthLocator(bymonthday= [1, storm['startdate'].day + \ _control_dayhour(storm['startdate'])])) ax1.xaxis.set_major_formatter(mpl.dates.DateFormatter('\n %b %Y')) # Add the labels of the different flows if j == 0: _add_labels_above(ax0, fig, flowserie.ndim, rainserie.ndim) # Print the start and end period as title above subplots if period_title: ax0.set_title(storm['startdate'].strftime("%d/%m/%y") + " - " + storm['enddate'].strftime("%d/%m/%y"), fontweight='bold', fontsize=12) # Looks of the rainplot ax0.set_xlabel('') ax0.invert_yaxis() ax0.yaxis.tick_right() ax0.spines['bottom'].set_visible(False) ax0.spines['top'].set_visible(False) plt.setp(ax0.get_xminorticklabels(), visible=False) plt.setp(ax0.get_xmajorticklabels(), visible=False) plt.setp(ax0.get_xminorticklabels(), visible=False) # looks of the flowplot ax1.spines['top'].set_visible(False) ax1.spines['bottom'].set_visible(False) ax1.set_xlabel('') plt.draw() all_axes = fig.get_axes() # Give all the subplots the same y-bounds if make_comparable: _make_comparable(all_axes) return fig, all_axes
def show_mesh(self, widget, profile): bm = self._printer.get_config_section(f"bed_mesh {profile}") if bm is False: logging.info(f"Unable to load profile: {profile}") return if profile == self.active_mesh: abm = self._printer.get_stat("bed_mesh") if abm is None: logging.info(f"Unable to load active mesh: {profile}") return x_range = [int(abm['mesh_min'][0]), int(abm['mesh_max'][0])] y_range = [int(abm['mesh_min'][1]), int(abm['mesh_max'][1])] minz_mesh = min(min(abm['mesh_matrix'])) maxz_mesh = max(max(abm['mesh_matrix'])) # Do not use a very small zscale, because that could be misleading minz_mesh = min(minz_mesh, -0.5) maxz_mesh = max(maxz_mesh, 0.5) z_range = [minz_mesh, maxz_mesh] counts = [len(abm['mesh_matrix'][0]), len(abm['mesh_matrix'])] deltas = [(x_range[1] - x_range[0]) / (counts[0] - 1), (y_range[1] - y_range[0]) / (counts[1] - 1)] x = [(i * deltas[0]) + x_range[0] for i in range(counts[0])] y = [(i * deltas[0]) + y_range[0] for i in range(counts[1])] x, y = np.meshgrid(x, y) z = np.asarray(abm['mesh_matrix']) else: x_range = [int(bm['min_x']), int(bm['max_x'])] y_range = [int(bm['min_y']), int(bm['max_y'])] z_range = [min(min(bm['points'])), max(max(bm['points']))] deltas = [(x_range[1] - x_range[0]) / (int(bm['x_count']) - 1), (y_range[1] - y_range[0]) / (int(bm['y_count']) - 1)] x = [(i * deltas[0]) + x_range[0] for i in range(bm['x_count'])] y = [(i * deltas[0]) + y_range[0] for i in range(bm['y_count'])] x, y = np.meshgrid(x, y) z = np.asarray(bm['points']) rc('axes', edgecolor="#e2e2e2", labelcolor="#e2e2e2") rc(('xtick', 'ytick'), color="#e2e2e2") fig = plt.figure(facecolor='#12121277') ax = Axes3D(fig, azim=245, elev=23) ax.set(title=profile, xlabel="X", ylabel="Y", facecolor='none') ax.spines['bottom'].set_color("#e2e2e2") fig.add_axes(ax) surf = ax.plot_surface(x, y, z, cmap=cm.coolwarm, vmin=-0.1, vmax=0.1) chartbox = ax.get_position() ax.set_position([ chartbox.x0, chartbox.y0 + 0.1, chartbox.width * .92, chartbox.height ]) ax.set_zlim(z_range[0], z_range[1]) ax.zaxis.set_major_locator(LinearLocator(5)) # A StrMethodFormatter is used automatically ax.zaxis.set_major_formatter('{x:.02f}') fig.colorbar(surf, shrink=0.7, aspect=5, pad=0.25) box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) box.set_hexpand(True) box.set_vexpand(True) title = Gtk.Label() title.set_markup(f"<b>{profile}</b>") title.set_hexpand(True) title.set_halign(Gtk.Align.CENTER) canvas_box = Gtk.Box() canvas_box.set_hexpand(True) canvas_box.set_vexpand(True) box.add(title) box.add(canvas_box) buttons = [{"name": _("Close"), "response": Gtk.ResponseType.CANCEL}] self._gtk.Dialog(self._screen, buttons, box, self._close_dialog) alloc = canvas_box.get_allocation() canvas = FigureCanvas(fig) canvas.set_size_request(alloc.width, self._screen.height / 3 * 2) canvas_box.add(canvas) canvas_box.show_all() # Remove the "matplotlib-canvas" class which forces a white background. # https://github.com/matplotlib/matplotlib/commit/3c832377fb4c4b32fcbdbc60fdfedb57296bc8c0 style_ctx = canvas.get_style_context() for css_class in style_ctx.list_classes(): style_ctx.remove_class(css_class)
def plot_energy_xy(it,ise,verlist,calc, folder='', type_plot = 'contourf', labels=(), lab_size=15, tick_size=15, fig_size=(), fig_title='', xlim = (), ylim = (), zlim = (), shag_a=0.001,shag_b=0.001,npoint_a=1000,npoint_b=1000): """ This function produces the plot of the energy surface corresponding to the structure deformed along the lattice vectors 1 and 2. The result can be presented as a 2D or 3D figure. Also the function has feature finding minimum of the energy surface based on bicubic spline scheme INPUT: - it (str) - the name of crystal structure - ise (str) - name of the parameters' set - verslist (list of int) - list of versions of new calculations - calc (.gbdm3) - database dictionary; could be provided; if not then are taken from header - folder (str) - directory where all the results will be built - type_plot (str) - represents kind of plot to build (2D,3D, contour etc.). Possible options are: '3D_proj_xyz_fill' - 3D plot with projections on xy, yz and xz planes in form 2D filled contour plots '3D_proj_z_contour' - 3D plot with projection on the xy plane only in form of 2D contour plot 'contourf' - 2D filled contour plot - labels (tuple of str) - has form ('label_x', 'label_y', 'label_z') and represents desirable names of axes - lab_size (int) - font size of labels - tick_size (int) - font size of ticks - fig_size (tuple of floats) - has form (height, width) set size of the figure in units of cm - fig_title (str) - optional, the title placed above the plot - xlim (tuple of floats) - has form (min_x, max_x) and represents limits of the plot along the 'x' axis - ylim (tuple of floats) - has form (min_y, max_y) and represents limits of the plot along the 'y' axis - zlim (tuple of floats) - has form (min_z, max_z) and represents limits of the plot along the 'z' axis - shag_a (float) - step of the fine grid of points along the lattice vector 1 of the structure for the bicubic spline - shag_b (float) - step of the fine grid of points along the lattice vector 2 of the structure for the bicubic spline - npoint_a (int) - number of points of the fine grid of points along the lattice vector 1 of the structure for the bicubic spline - npoint_b (int) - number of points of the fine grid of points along the lattice vector 2 of the structure for the bicubic spline RETURN: None SOURCE: None TODO: - Add more types of plots - Add plot based on fine bicubic spline grid """ acell_list = [] etotal_list = [] for v in verlist: id1 = (it,ise,v) acell = [] acell.append(calc[id1].rprimd[0][0]); acell.append(calc[id1].rprimd[1][1]); acell.append(calc[id1].rprimd[2][2]) acell_list.append( acell ) etotal_list.append ( calc[id1].energy_sigma0 ) f0 = open(folder+'/deformation_'+it+'_'+ise+'_xy.out', 'w') f0.write('Calc equilibrium acell for\n') a=[]; b=[]; for acell in acell_list: if acell[0] not in a: a.append(acell[0]) if acell[1] not in b: b.append(acell[1]) f0.write('Read number points acell = '+ str(len(a))+'\n') f0.write('Read number points ccell = '+ str(len(b))+'\n\n') print ('Read number points acell = '+ str(len(a))+'\n') print ('Read number points ccell = '+ str(len(b))+'\n\n') f0.write('Limits acell read from file = ' + str(min(a))+' '+ str(max(a)) + '; (max(a)-min(a))/2 = ' + str((max(a)-min(a))/2) +'\n') f0.write('Limits ccell read from file = ' + str(min(b))+' '+ str(max(b)) + '; (max(c)-min(c))/2 = ' + str((max(b)-min(b))/2) +'\n\n') etot = [None for i in range(len(a)*len(b))] for i in range(len(etotal_list)): acell = acell_list[i] jj=0 exit = False for j in b: if exit==True: break for k in a: if acell[0]==k and acell[1]==j: etot[jj] = etotal_list[i] exit = True break else: jj+=1 # Find position with minimum energy etot_min = min(etot) f0.write('Etot_min (without spline) = '+ str(etot_min)+' eV\n\n') ii=0; exit = False for j in b: if exit==True: break for k in a: if etot[ii]==etot_min: amin = k bmin = j exit = True break else: ii+=1 f0.write('xcell_min (without spline) = '+ str(amin)+' Angstrom\n') f0.write('ycell_min (without spline) = '+ str(bmin)+' Angstrom\n\n\n') e2 = ALB() e2.build_2d_bicubic_spline(a, len(a), b, len(b), etot, 1) e_min = 10**(8) for j in range(-npoint_b, npoint_b): b_cur = bmin+i*shag_b for k in range(-npoint_a, npoint_a): a_cur = amin+k*shag_a e_cur = e2.calc(a_cur,b_cur,0) if e_cur < e_min: e_min = e_cur a_min = a_cur b_min = b_cur f0.write('Etot_min (with spline) = '+ str(e_min)+' eV\n\n') f0.write('xcell_min (with spline) = '+ str(a_min)+' Angstrom\n') f0.write('ycell_min (with spline) = '+ str(b_min)+' Angstrom\n\n\n') f0.write('Found min etot for limitation:\n') f0.write('xcell = '+str(amin)+' +/- '+str(npoint_a*shag_a)+' Angstrom'+' (step = '+str(shag_a)+')'+'\n') f0.write('ycell = '+str(bmin)+' +/- '+str(npoint_b*shag_b)+' Angstrom'+' (step = '+str(shag_b)+')'+'\n\n\n') # Check etot assert None not in etot, 'None in etot' f0.close() # Chuan bi cac danh sach if len(acell_list)!=len(etotal_list): raise RuntimeError ('Не равны длины списков len(acell_list)!=len(etotal_list) для построения 3D рисунка!') acell_list1 = [tuple(i) for i in acell_list] setca_ab_dict = dict(zip(acell_list1, etotal_list)) x_setca, y_setca, z_setca = [], [], [] x1, y1, z1 = [], [], [] ii = 0 for key in sorted(setca_ab_dict): if ii==0: key_cur = key ii=1 if abs(key[0]-key_cur[0])<10**(-10): x1.append(key[0]) y1.append(key[1]) z1.append(setca_ab_dict[key]) else: key_cur = key x_setca.append(x1) y_setca.append(y1) z_setca.append(z1) x1 = [key[0]]; y1 = [key[1]]; z1 = [setca_ab_dict[key]]; else: x_setca.append(x1) y_setca.append(y1) z_setca.append(z1) # Chuan bi tep voi ket qua x_list = [] y_list = [] z_list = [] for i in range(len(x_setca)): x_list += x_setca[i] y_list += y_setca[i] z_list += z_setca[i] f = open(folder+'/plot_energy_xy_'+it+'_'+ise+'.out','w') f.write('# {0:^15s} {1:^15s} {2:^15s}'.format('x, Angstrom', 'y, Angstrom', 'E, eV')+'\n') for i in range(len(x_list)): f.write('{0:^15.5f} {1:^15.5f} {2:^15.5f}'.format(x_list[i], y_list[i], z_list[i])+'\n') f.close() # Tim gia tri it nhat min_x_list = [min(i) for i in x_setca] min_y_list = [min(i) for i in y_setca] min_z_list = [min(i) for i in z_setca] min_x = min(min_x_list) min_y = min(min_y_list) min_z = min(min_z_list) import matplotlib.pyplot as plt import numpy as np from mpl_toolkits.mplot3d import axes3d from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter, MultipleLocator, AutoLocator # Biến thành numpy.array x_setca_np = np.array(x_setca) y_setca_np = np.array(y_setca) z_setca_np = np.array(z_setca) if not xlim: xlim = (min(x_setca), max(x_setca)) if not ylim: ylim = (min(y_setca), max(y_setca)) if not zlim: zlim = (min(z_setca), max(z_setca)) # Vẽ bức tranh fig = plt.figure() font = matplotlib.font_manager.FontProperties() font.set_size(20) if type_plot == '3D_proj_xyz_fill': # ax = fig.gca(projection='3d') ax = fig.add_subplot(111, projection='3d') cset = ax.contourf(x_setca_np, y_setca_np, z_setca_np, zdir='z', offset=zlim[0], cmap=cm.rainbow, zorder=0.3) ax.plot_surface(x_setca_np, y_setca_np, z_setca_np, rstride=8, cstride=8, alpha=0.2, cmap=cm.rainbow, zorder=0.5) ax.scatter(x_setca_np, y_setca_np, z_setca_np, marker='o', color='red') cset = ax.contourf(x_setca_np, y_setca_np, z_setca_np, zdir='x', offset=xlim[0], cmap=cm.rainbow ) cset = ax.contourf(x_setca_np, y_setca_np, z_setca_np, zdir='y', offset=ylim[1], cmap=cm.rainbow ) ax.set_xlabel(labels[0], fontsize=lab_size, labelpad=10) ax.set_ylabel(labels[1], fontsize=lab_size, labelpad=10) ax.set_zlabel(labels[2], fontsize=lab_size, labelpad=10,rotation=0) ax.set_xlim(xlim[0], xlim[1]) ax.set_ylim(ylim[0], ylim[1]) ax.set_zlim(zlim[0], zlim[1]) ax.tick_params(axis = 'both', labelsize=tick_size) # ax.xaxis.set_major_locator(LinearLocator(4)) # ax.yaxis.set_major_locator(LinearLocator(4)) # ax.zaxis.set_major_locator(LinearLocator(4)) ax.xaxis.set_major_formatter(FormatStrFormatter('%.2f')) ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f')) ax.zaxis.set_major_formatter(FormatStrFormatter('%.2f')) ax.set_title(fig_title, fontsize=lab_size) if type_plot == '3D_proj_z_contour': # ax = fig.gca(projection='3d') ax = fig.add_subplot(111, projection='3d') # norm = plt.Normalize(z_setca_np.min(), z_setca_np.max()) # colors = cm.viridis(norm(z_setca_np)) # rcount, ccount, _ = colors.shape # cset = ax.contour(x_setca_np, y_setca_np, z_setca_np, zdir='z', offset=zlim[0], cmap=cm.rainbow, zorder=0.3) ax.plot_surface(x_setca_np, y_setca_np, z_setca_np, rstride=1, cstride=1, cmap=cm.rainbow, zorder=0.5) # surf = ax.plot_surface(x_setca_np, y_setca_np, z_setca_np, rcount=rcount, ccount=ccount, facecolors=colors, shade=False, zorder=0.5) # surf.set_facecolor((0,0,0,0)) ax.scatter(x_setca_np, y_setca_np, z_setca_np, marker='o', color='red') ax.set_xlabel(labels[0], fontsize=lab_size, labelpad=10) ax.set_ylabel(labels[1], fontsize=lab_size, labelpad=10) ax.set_zlabel(labels[2], fontsize=lab_size, labelpad=10) ax.set_xlim(xlim[0], xlim[1]) ax.set_ylim(ylim[0], ylim[1]) ax.set_zlim(zlim[0], zlim[1]) ax.tick_params(axis = 'both', labelsize=tick_size) # ax.xaxis.set_major_locator(LinearLocator(4)) # ax.yaxis.set_major_locator(LinearLocator(4)) # ax.zaxis.set_major_locator(LinearLocator(4)) ax.xaxis.set_major_formatter(FormatStrFormatter('%.2f')) ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f')) ax.zaxis.set_major_formatter(FormatStrFormatter('%.1f')) ax.azim = 225 ax.zaxis.set_rotate_label(False) # disable automatic rotation ax.set_zlabel(labels[2], fontsize=lab_size, rotation=90) ax.text2D(0.17,0.73, fig_title, fontproperties=font, transform=ax.transAxes) elif type_plot == 'contourf': cs = plt.contourf(x_setca, y_setca, z_setca, locator=LinearLocator(numticks = 50), cmap=cm.rainbow) cs.ax.set_xlabel(labels[0], fontsize=lab_size) cs.ax.set_ylabel(labels[1], fontsize=lab_size) cs.ax.tick_params(axis = 'both', labelsize=tick_size) cbar = plt.colorbar(format = FormatStrFormatter('%.2f')) cbar.set_label('$'+labels[2]+'$', fontsize = lab_size) cbar.ax.tick_params(labelsize=tick_size) # cs.title(fig_title, fontsize=lab_size) # plt.show() fig.set_figheight(fig_size[0]) fig.set_figwidth(fig_size[1]) fig.savefig(folder+'/'+it+'_'+ise+'_deform_xy_'+type_plot+'.pdf', format='pdf', dpi=600) fig.savefig(folder+'/'+it+'_'+ise+'_deform_xy_'+type_plot+'.eps', format='eps', dpi=600)
def DrawFigure(x_values, y_values, y_max, legend_labels, x_label, y_label, filename, id, allow_legend): # plt.rc('text', usetex=True) # plt.rc('font', family='serif') # plt.rcParams['pdf.fonttype'] = 42 # you may change the figure size on your own. fig = plt.figure(figsize=(10, 4)) figure = fig.add_subplot(111) FIGURE_LABEL = legend_labels if not os.path.exists(FIGURE_FOLDER): os.makedirs(FIGURE_FOLDER) # values in the x_xis index = np.arange(len(x_values)) # the bar width. # you may need to tune it to get the best figure. width = 0.5 # draw the bars bottom_base = np.zeros(len(y_values[0])) bars = [None] * (len(FIGURE_LABEL)) for i in range(len(y_values)): # if (i != 4): bars[i] = plt.bar(index + width / 2, y_values[i], width, hatch=PATTERNS[i], color=LINE_COLORS[i], label=FIGURE_LABEL[i], bottom=bottom_base, edgecolor='black', linewidth=3) bottom_base = np.array(y_values[i]) + bottom_base # else: # bars[i] = plt.bar(index + width / 2, y_values[i], 0, hatch='', linewidth=0, fill=False) # sometimes you may not want to draw legends. if allow_legend == True: plt.legend(bars, FIGURE_LABEL, prop=LEGEND_FP, loc='upper center', ncol=len(legend_labels), mode='expand', bbox_to_anchor=(0.45, 1.1), shadow=False, frameon=False, borderaxespad=0.0, handlelength=2, labelspacing=0.2) plt.ylim(bottom=0) yfmt = ScalarFormatterForceFormat() yfmt.set_powerlimits((0, 0)) figure.get_yaxis().set_major_formatter(yfmt) plt.ticklabel_format(axis="y", style="sci", scilimits=(0, 0), useMathText=True) plt.grid(axis='y', color='gray') figure.yaxis.set_major_locator(LinearLocator(3)) # you may need to tune the xticks position to get the best figure. plt.xticks(index + 0.5 * width, x_values) # plt.autofmt_xdate() plt.xticks(rotation=30) # if id == 38: # stock: all algorithms are idle most of the time. # plt.yscale('log') # figure.yaxis.set_major_locator(matplotlib.ticker.LogLocator(numticks=5)) # else: # figure.yaxis.set_major_locator(pylab.LinearLocator(5)) plt.grid(axis='y', color='gray') # figure.yaxis.set_major_locator(LogLocator(base=10)) # # figure.get_xaxis().set_tick_params(direction='in', pad=10) # figure.get_yaxis().set_tick_params(direction='in', pad=10) plt.xlabel(x_label, fontproperties=LABEL_FP) plt.ylabel(y_label, fontproperties=LABEL_FP) size = fig.get_size_inches() dpi = fig.get_dpi() plt.tight_layout() plt.savefig(FIGURE_FOLDER + '/' + filename + '.pdf')
def surface_plot(self, x, y, data, fig=None): """plot a surface around the specified location "ncolumns":[21,"Number of columns"], "nlines":[21,"Number of lines"], "angh":[-33.,"Horizontal viewing angle in degrees"], "angv":[25.,"Vertical viewing angle in degrees"], "floor":[None,"Minimum value to be contoured"], "ceiling":[None,"Maximum value to be contoured"], """ from mpl_toolkits.mplot3d import Axes3D from matplotlib.ticker import LinearLocator, FormatStrFormatter if fig is None: fig = plt.figure(self._figure_name) fig.clf() fig.add_subplot(111) ax = fig.gca(projection='3d') if self.surface_pars["title"][0]: ax.set_title(self.surface_pars["title"][0]) ax.set_xlabel(self.surface_pars["xlabel"][0]) ax.set_ylabel(self.surface_pars["ylabel"][0]) if self.surface_pars["zlabel"][0]: ax.set_zlabel("Flux") fancy = self.surface_pars["fancy"][0] deltax = self.surface_pars["ncolumns"][0] deltay = self.surface_pars["nlines"][0] X = np.arange(x - deltax, x + deltax, 1) Y = np.arange(y - deltay, y + deltay, 1) X, Y = np.meshgrid(X, Y) Z = data[y - deltay:y + deltay, x - deltax:x + deltax] if self.surface_pars["floor"][0]: zmin = float(self.surface_pars["floor"][0]) else: zmin = np.min(Z) if self.surface_pars["ceiling"][0]: zmax = float(self.surface_pars["ceiling"][0]) else: zmax = np.max(Z) stride = int(self.surface_pars["stride"][0]) if fancy: surf = ax.plot_surface( X, Y, Z, rstride=stride, cstride=stride, cmap=self.surface_pars["cmap"][0], alpha=0.6) else: surf = ax.plot_surface(X, Y, Z, rstride=stride, cstride=stride, cmap=self.surface_pars[ "cmap"][0], linewidth=0, antialiased=False) ax.zaxis.set_major_locator(LinearLocator(10)) ax.zaxis.set_major_formatter(FormatStrFormatter('%.0f')) ax.set_zlim(zmin, zmax) if fancy: xmin = x - deltax ymax = y + deltay cset = ax.contour( X, Y, Z, zdir='z', offset=zmax, cmap=self.surface_pars["cmap"][0]) cset = ax.contour( X, Y, Z, zdir='x', offset=xmin, cmap=self.surface_pars["cmap"][0]) cset = ax.contour( X, Y, Z, zdir='y', offset=ymax, cmap=self.surface_pars["cmap"][0]) fig.colorbar(surf, shrink=0.5, aspect=5) if self.surface_pars["azim"][0]: ax.view_init(elev=10., azim=float(self.surface_pars["azim"][0])) plt.draw() plt.show(block=False) time.sleep(self.sleep_time)
def plot_terrain(): terrain1 = imread('SRTM_data_Norway_1.tif') t_max = np.amax(np.amax(terrain1)) t_min = np.amin(np.amin(terrain1)) print(np.shape(terrain1)) print(t_min, t_max) terrain1 = terrain1 - t_min terrain1 = terrain1 * 1.0 / (t_max - t_min) # Show the terrain plt.figure() plt.title('Terrain over Norway') plt.imshow(terrain1, cmap='gray', vmin=0.0, vmax=1.0) plt.xlabel('X') plt.ylabel('Y') #plt.show() plt.savefig('figs/ter1_full' + fig_format, bbox_inches='tight', pad_inches=0.1) plt.clf() #reduce terrain shape_ter = np.shape(terrain1) nx = shape_ter[1] ny = shape_ter[0] res = 50 deg = 40 res2 = res**2 nxr = nx // res nyr = ny // res n2 = nxr * nyr nmax = max(nxr, nyr) x = (np.arange(0, nxr)) / nmax * 2.0 y = (np.arange(0, nyr)) / nmax * 2.0 #x=(np.arange(0,nxr)-nmax//2)/nmax # centered on (0,0) #y=(np.arange(0,nyr)-nmax//2)/nmax # max extension = 0.5 x3d, y3d = np.meshgrid(x, y) ter_res = np.zeros(shape=(nyr, nxr)) print('rescale') for i in range(nyr): for j in range(nxr): ter_res[i, j] = np.sum(terrain1[i * res:(i + 1) * res, j * res:(j + 1) * res]) / res2 tr_max = np.amax(np.amax(ter_res)) tr_min = np.amin(np.amin(ter_res)) print(tr_min, tr_max) print(np.sum(ter_res) / n2) plt.figure(1) plt.title('Rescaled terrain') plt.imshow(ter_res, cmap='gray', vmin=0.0, vmax=1.0) plt.xlabel('X') plt.ylabel('Y') #plt.show() plt.savefig('figs/ter1_rescale' + fig_format, bbox_inches='tight', pad_inches=0.1) plt.clf() if (False): return print('vectorize') x_ter, y_ter, t_ter = init_xy_vectors(1, False, rearr=True, ter=True, x=x, y=y, z=ter_res) mean_ter = np.sum(terrain1) / (nx * ny) # dter=t_max-t_min k = 4 print('split') xk, yk, tk, nk = split_data_kfold(x_ter, y_ter, t_ter, k) #k=number of data groups l_vec = [0.0, 1e-10, 1e-8, 1e-4] n_l = len(l_vec) #d_vec=np.arange(10,50,5,dtype='int') #with centered values d_vec = np.arange(5, 26, 5, dtype='int') #with scale 2 not certered n_d = len(d_vec) MSEs = np.zeros(shape=(n_l, n_d, 2)) for i in range(n_l): lamb = l_vec[i] for j in range(n_d): deg = d_vec[j] print('fit lambda %.2e, deg %i' % (lamb, deg)) msek, r2k, betak, var_bk = polfit_kfold(xk, yk, tk, nk, k, n2, deg=deg, lamb=lamb) beta = np.mean(betak, axis=1) MSEs[i, j, :] = np.mean(msek, axis=0) print('evaluate') ter_fit = eval_terrain(beta, x, y, deg, nxr, nyr) #ter_fit_3d=eval_pol3D(beta,x3d,y3d,deg) print('plot') plt.figure(1) lstr, powstr = get_pow_str(lamb, 1) plt.title(r'Terrain fit') plt.imshow(ter_fit, cmap='gray', vmin=0.0, vmax=1.0) plt.xlabel('X') plt.ylabel('Y') #plt.show() outfile = 'ter_fit_scale2_lamb_%.1e_deg_%i' % (lamb, deg) + fig_format print(outfile) plt.savefig('figs/' + outfile, bbox_inches='tight', pad_inches=0.1) plt.clf() plt.figure(1) cols = plt.rcParams['axes.prop_cycle'].by_key()['color'] for i in range(n_l): lstr, powstr = get_pow_str(l_vec[i], 1) plt.plot(d_vec, MSEs[i, :, 0], ls='-', marker='.', label='$\lambda = \mathrm{%s}\cdot 10^{%s}$' % (lstr, powstr), color=cols[i]) plt.plot(d_vec, MSEs[i, :, 1], ls='--', marker='.', color=cols[i]) plt.xlabel('Polynomial degree', fontsize=14) plt.ylabel('Mean Square Error', fontsize=14) outfile = 'ter_mse_scale2' + fig_format plt.legend(loc='upper right') plt.savefig('figs/' + outfile) plt.clf() if (True): return #Other plotting (3D) fig = plt.figure(1) ax = fig.gca(projection='3d') surf = ax.plot_surface(x3d, y3d, ter_fit_3d, cmap=cm.gray, linewidth=0, antialiased=False, alpha=1.0) # Customize the z axis. ax.set_zlim(0.0, 1.0) ax.zaxis.set_major_locator(LinearLocator(10)) ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) ax.view_init(elev=10., azim=65.) # Add a color bar which maps values to colors. plt.xlabel('y', fontsize=14) plt.ylabel('x', fontsize=14, labelpad=10) plt.yticks(rotation=45) #plt.show() plt.savefig('ter1_fit_3d.png') plt.clf() return
parser.add_argument("--export", metavar="file name", dest="output_file", default="", help="export plot to file " + "(disable interactive window)") args = parser.parse_args() directories = args.directories # prepare plot of data plt.figure(figsize=(10, 5)) plt.title("charge conservation over time", fontsize=22) major_locator1 = LinearLocator() major_locator2 = LinearLocator() major_formatter = FormatStrFormatter('%1.1e') ax1 = plt.subplot(121) ax1.set_xlabel(r"$t\,[\Delta t]$", fontsize=20) ax1.set_ylabel(r"$\mathrm{max}|d|\,[\rho_\mathrm{max}(0)]$", fontsize=20) plt.xticks(fontsize=14) plt.yticks(fontsize=14) # always use scientific notation ax1.yaxis.set_major_locator(major_locator1) ax1.yaxis.set_major_formatter(major_formatter) ax2 = plt.subplot(122) ax2.set_xlabel(r"$t\,[\Delta t]$", fontsize=20) ax2.set_ylabel(r"$\left<|d|\right> \pm \sigma_d\,[\rho_\mathrm{max}(0)]$",
return np.convolve(interval, window, 'same') y_av = movingaverage(download_speeds_floats, 15) #create plots for the visualization fig, ax = plt.subplots(figsize=(17, 10)) #scatterpoints ax.plot_date(dates, download_speeds) #average trend ax.plot_date(dates[6:-6], y_av[6:-6], ls='solid', color='r', label="running average") #This finds the correct locations for certain hours to place the ticks, and also #does the formatting for the labels ax.xaxis.set_major_locator(LinearLocator(numticks=20)) ax.xaxis.set_major_formatter(DateFormatter('%b %dth\n')) plt.ylabel("(Mb/s)", fontsize=23) plt.title("Wireless Speed Over Time", fontsize=28) plt.xticks(rotation=70) plt.legend() plt.grid(which="major", axis="x") date_and_time_string = time.strftime("%b_%d_%H:%M_") fig.savefig(base_path + '/figures/wireless_speeds_' + date_and_time_string + '.pdf') fig.savefig(base_path + '/figures/wireless_speeds_' + date_and_time_string + '.png')
def plot_merit_order(): "Shows how merit order is affected w.r.t emissions intensities, SRMCs, and net liability under a REP scheme" # Only consider fossil units df_gp = df_g[df_g['FUEL_CAT'] == 'Fossil'].copy() # Permit prices permit_prices = range(2, 71, 2) # Number of rectanlges n = len(permit_prices) # Gap as a fraction of rectangle height gap_fraction = 1 / 10 # Rectangle height rectangle_height = 1 / (gap_fraction * (n - 1) + n) # Gap between rectangles y_gap = rectangle_height * gap_fraction # Initial y offset y_offset = 0 # Container for rectangle patches rectangles = [] # Container for colours corresponding to patches colours_emissions_intensity = [] colours_net_liability = [] colours_srmc_rep = [] colours_srmc_carbon_tax = [] # Construct rectangles to plot for each permit price scenario for permit_price in permit_prices: # Baseline corresponding to BAU price targeting scenario baseline = df_baseline_vs_permit_price.loc[permit_price, 1] # Net liability faced by generator under REP scheme df_gp['NET_LIABILITY'] = (df_gp['EMISSIONS'] - baseline) * permit_price # Compute updated SRMC and sort from least cost to most expensive (merit order) df_gp['SRMC_REP'] = df_gp['SRMC_2016-17'] + df_gp['NET_LIABILITY'] df_gp.sort_values('SRMC_REP', inplace=True) # Carbon tax SRMCs (baseline = 0 for all permit price scenarios) df_gp['SRMC_TAX'] = df_gp['SRMC_2016-17'] + (df_gp['EMISSIONS'] * permit_price) # Normalising registered capacities df_gp['REG_CAP_NORM'] = (df_gp['REG_CAP'] / df_gp['REG_CAP'].sum()) x_offset = 0 # Plotting rectangles for index, row in df_gp.iterrows(): rectangles.append( patches.Rectangle((x_offset, y_offset), row['REG_CAP_NORM'], rectangle_height)) # Colour for emissions intensity plot colours_emissions_intensity.append(row['EMISSIONS']) # Colour for net liability under REP scheme for each generator colours_net_liability.append(row['NET_LIABILITY']) # Colour for net generator SRMCs under REP scheme colours_srmc_rep.append(row['SRMC_REP']) # Colour for SRMCs under carbon tax colours_srmc_carbon_tax.append(row['SRMC_TAX']) # Offset for placement of next rectangle x_offset += row['REG_CAP_NORM'] y_offset += rectangle_height + y_gap # Merit order emissions intensity patches patches_emissions_intensity = PatchCollection(rectangles, cmap='Reds') patches_emissions_intensity.set_array( np.array(colours_emissions_intensity)) # Net liability under REP scheme patches patches_net_liability = PatchCollection(rectangles, cmap='bwr') patches_net_liability.set_array(np.array(colours_net_liability)) # SRMCs under REP scheme patches patches_srmc_rep = PatchCollection(rectangles, cmap='Reds') patches_srmc_rep.set_array(np.array(colours_srmc_rep)) # SRMCs under carbon tax patches patches_srmc_carbon_tax = PatchCollection(rectangles, cmap='Reds') patches_srmc_carbon_tax.set_array(np.array(colours_srmc_carbon_tax)) # Format tick positions # --------------------- # y-ticks # ------- # Minor ticks yminorticks = [] for counter, permit_price in enumerate(permit_prices): if counter == 0: position = rectangle_height / 2 else: position = yminorticks[-1] + y_gap + rectangle_height yminorticks.append(position) yminorlocator = FixedLocator(yminorticks) # Major ticks ymajorticks = [] for counter in range(0, 7): if counter == 0: position = (4.5 * rectangle_height) + (4 * y_gap) else: position = ymajorticks[-1] + (5 * rectangle_height) + (5 * y_gap) ymajorticks.append(position) ymajorlocator = FixedLocator(ymajorticks) # x-ticks # ------- # Minor locator xminorlocator = LinearLocator(21) # Major locator xmajorlocator = LinearLocator(6) # Emissions intensity and net liability figure # -------------------------------------------- plt.clf() # Initialise figure fig1 = plt.figure() # Axes on which to construct plots ax1 = plt.axes([0.065, 0.185, 0.40, .79]) ax2 = plt.axes([0.57, 0.185, 0.40, .79]) # Add emissions intensity patches ax1.add_collection(patches_emissions_intensity) # Add net liability patches patches_net_liability.set_clim([-35, 35]) ax2.add_collection(patches_net_liability) # Add colour bars with labels cbar1 = fig1.colorbar(patches_emissions_intensity, ax=ax1, pad=0.015, aspect=30) cbar1.set_label('Emissions intensity (tCO${_2}$/MWh)', fontsize=8, fontname='Helvetica') cbar2 = fig1.colorbar(patches_net_liability, ax=ax2, pad=0.015, aspect=30) cbar2.set_label('Net liability (\$/MWh)', fontsize=8, fontname='Helvetica') # Label axes ax1.set_ylabel('Permit price (\$/tCO$_{2}$)', fontsize=9, fontname='Helvetica') ax1.set_xlabel('Normalised cumulative capacity\n(a)', fontsize=9, fontname='Helvetica') ax2.set_ylabel('Permit price (\$/tCO$_{2}$)', fontsize=9, fontname='Helvetica') ax2.set_xlabel('Normalised cumulative capacity\n(a)', fontsize=9, fontname='Helvetica') # Format ticks # ------------ # y-axis ax1.yaxis.set_minor_locator(yminorlocator) ax1.yaxis.set_major_locator(ymajorlocator) ax2.yaxis.set_minor_locator(yminorlocator) ax2.yaxis.set_major_locator(ymajorlocator) # y-tick labels ax1.yaxis.set_ticklabels(['10', '20', '30', '40', '50', '60', '70']) ax2.yaxis.set_ticklabels(['10', '20', '30', '40', '50', '60', '70']) # x-axis ax1.xaxis.set_minor_locator(xminorlocator) ax1.xaxis.set_major_locator(xmajorlocator) ax2.xaxis.set_minor_locator(xminorlocator) ax2.xaxis.set_major_locator(xmajorlocator) # Format figure size width = 180 * mmi height = 75 * mmi fig1.set_size_inches(width, height) # Save figure fig1.savefig( os.path.join(output_dir, 'figures', 'emissions_liability_merit_order.pdf')) # SRMCs under REP and carbon tax # ------------------------------ # Initialise figure fig2 = plt.figure() # Axes on which to construct plots ax3 = plt.axes([0.065, 0.185, 0.40, .79]) ax4 = plt.axes([0.57, 0.185, 0.40, .79]) # Add REP SRMCs patches_srmc_rep.set_clim([25, 200]) ax3.add_collection(patches_srmc_rep) # Add carbon tax net liability patches_srmc_carbon_tax.set_clim([25, 200]) ax4.add_collection(patches_srmc_carbon_tax) # Add colour bars with labels cbar3 = fig2.colorbar(patches_srmc_rep, ax=ax3, pad=0.015, aspect=30) cbar3.set_label('SRMC (\$/MWh)', fontsize=8, fontname='Helvetica') cbar4 = fig2.colorbar(patches_srmc_carbon_tax, ax=ax4, pad=0.015, aspect=30) cbar4.set_label('SRMC (\$/MWh)', fontsize=8, fontname='Helvetica') # Label axes ax3.set_ylabel('Permit price (\$/tCO$_{2}$)', fontsize=9, fontname='Helvetica') ax3.set_xlabel('Normalised cumulative capacity\n(a)', fontsize=9, fontname='Helvetica') ax4.set_ylabel('Permit price (\$/tCO$_{2}$)', fontsize=9, fontname='Helvetica') ax4.set_xlabel('Normalised cumulative capacity\n(a)', fontsize=9, fontname='Helvetica') # Format ticks # ------------ # y-axis ax3.yaxis.set_minor_locator(yminorlocator) ax3.yaxis.set_major_locator(ymajorlocator) ax4.yaxis.set_minor_locator(yminorlocator) ax4.yaxis.set_major_locator(ymajorlocator) # y-tick labels ax3.yaxis.set_ticklabels(['10', '20', '30', '40', '50', '60', '70']) ax4.yaxis.set_ticklabels(['10', '20', '30', '40', '50', '60', '70']) # x-axis ax3.xaxis.set_minor_locator(xminorlocator) ax3.xaxis.set_major_locator(xmajorlocator) ax4.xaxis.set_minor_locator(xminorlocator) ax4.xaxis.set_major_locator(xmajorlocator) # Format figure size width = 180 * mmi height = 75 * mmi fig2.set_size_inches(width, height) # Save figure fig2.savefig(os.path.join(output_dir, 'figures', 'srmc_merit_order.pdf')) plt.show()
N = 1000 vals = np.linspace(-1, 1, N) X, Y = np.meshgrid(vals, vals) Z = np.zeros(X.shape) w = np.load("VFA_params_vishamC.npy") def baseline_basis(x): return np.array([[x[0]], [x[1]], [1], [x[0]**2], [x[1]**2], [x[1]*x[0]]]) def baseline(s, w): phi = baseline_basis(s) return (phi.T).dot(w), phi for i in range(N): for j in range(N): x = X[i,j]; y = Y[i,j] Z[i,j], _ = baseline(np.array([x,y]), w) fig = plt.figure() ax = fig.gca(projection='3d') surf = ax.plot_surface(X, Y, Z, cmap='summer', edgecolors='none') ax.xaxis.set_major_locator(LinearLocator(5)) ax.yaxis.set_major_locator(LinearLocator(5)) ax.set_xlim(-1, 1) ax.set_ylim(-1, 1) ax.set_xlabel("x") ax.set_ylabel("y") ax.set_zlabel(r"$\hat{V}(s, w)$") plt.show()
def __init__(self, fig, cmap=None, norm=None, limit_func=None, auto_redraw=True, interpolation=None, aspect='equal'): self._cursor_position_cbs = [] if interpolation is None: interpolation = _INTERPOLATION[0] self._interpolation = interpolation # used to determine if setting properties should force a re-draw self._auto_redraw = auto_redraw # clean defaults if limit_func is None: limit_func = fullrange_limit_factory() if cmap is None: cmap = 'gray' # stash the color map self._cmap = cmap # set the default norm if not passed if norm is None: norm = Normalize() # always set the vmin/vmax as we can not auto-limit with an empty array # below. When we set the data we are going to fully rescale this # anyway. norm.vmin, norm.vmax = 0, 1 self._norm = norm # save a copy of the limit function, we will need it later self._limit_func = limit_func # this is used by the widget logic self._active = True self._dirty = True self._cb_dirty = True # work on setting up the mpl axes self._fig = fig # blow away what ever is currently on the figure fig.clf() # Configure the figure in our own image # # +----------------------+ # | H cross section | # +----------------------+ # +---+ +----------------------+ # | V | | | # | | | | # | x | | | # | s | | Main Axes | # | e | | | # | c | | | # | t | | | # | i | | | # | o | | | # | n | | | # +---+ +----------------------+ # make the main axes self._im_ax = fig.add_subplot(1, 1, 1) self._im_ax.set_aspect(aspect) self._im_ax.xaxis.set_major_locator(NullLocator()) self._im_ax.yaxis.set_major_locator(NullLocator()) self._imdata = None self._im = self._im_ax.imshow( [[]], cmap=self._cmap, norm=self._norm, interpolation=self._interpolation, aspect=aspect, ) # make it dividable divider = make_axes_locatable(self._im_ax) # set up all the other axes # (set up the horizontal and vertical cuts) self._ax_h = divider.append_axes('top', .5, pad=0.1, sharex=self._im_ax) self._ax_h.yaxis.set_major_locator(LinearLocator(numticks=2)) self._ax_v = divider.append_axes('left', .5, pad=0.1, sharey=self._im_ax) self._ax_v.xaxis.set_major_locator(LinearLocator(numticks=2)) self._ax_cb = divider.append_axes('right', .2, pad=.5) # add the color bar self._cb = fig.colorbar(self._im, cax=self._ax_cb) # add the cursor place holder self._cur = None # turn off auto-scale for the horizontal cut self._ax_h.autoscale(enable=False) # turn off auto-scale scale for the vertical cut self._ax_v.autoscale(enable=False) # create line artists self._ln_v, = self._ax_v.plot([], [], 'k-', animated=True, visible=False) self._ln_h, = self._ax_h.plot([], [], 'k-', animated=True, visible=False) # backgrounds for blitting self._ax_v_bk = None self._ax_h_bk = None # stash last-drawn row/col to skip if possible self._row = None self._col = None # make attributes for callback ids self._move_cid = None self._click_cid = None self._clear_cid = None
X = np.arange(-5, 5, 0.25) Y = np.arange(-5, 5, 0.25) X, Y = np.meshgrid(X, Y) R = np.sqrt(X**2 + Y**2) Z = np.sin(R) # Plot the surface. surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm, linewidth=0, antialiased=False) X = np.arange(-10, 10, 0.25) Y = np.arange(-10, 10, 0.25) X, Y = np.meshgrid(X, Y) newZ = np.zeros_like(X) ax.plot_surface(X, Y, newZ) # Customize the z axis. ax.set_zlim(-1.01, 1.01) ax.zaxis.set_major_locator(LinearLocator(10)) ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) # Add a color bar which maps values to colors. # fig.colorbar(surf, shrink=0.5, aspect=5) plt.show()
def PlotResultsXGBoost(self, *args): # getting the inputs CDOM = args[0] CDOM_sorted = args[1] X_CDOM_diag_mesh = args[2] CDOM_pred_fine_mesh = args[3] CDOM_pred = args[4] outputPath = args[5] y_pred = args[6] y_pred_train = args[7] MSE_per_epoch = args[8] y_train = args[9] y_test = args[10] XGboost_best_model_index = args[11] # Making 3d plots fontsize = 10 # Plot Bray distance by CDOM x1 = (CDOM_sorted.loc[:, "x"]) x2 = x1.copy() x1, x2 = np.meshgrid(x1, x2) CDOM.mesh[x1 - x2 == 0] = np.nan fig = plt.figure(figsize=plt.figaspect( 0.25)) #figsize=(15, 5))#figsize=plt.figaspect(0.5)) ax = fig.add_subplot(1, 3, 1, projection='3d') ax.set_title("BCC Bray distances by sites' CDOM", fontsize=fontsize) #plt.subplots_adjust(left=0, bottom=0, right=2, top=2, wspace=0, hspace=0) ax.view_init(elev=30.0, azim=300.0) surf = ax.plot_trisurf(CDOM.loc[:, "CDOM.x1"], CDOM.loc[:, "CDOM.x2"], CDOM.loc[:, "ASV.dist"], cmap='viridis', edgecolor='none') # Customize the z axis. ax.set_zlim(0.3, 1) ax.zaxis.set_major_locator(LinearLocator(10)) ax.zaxis.set_major_formatter(FormatStrFormatter("%.02f")) ax.tick_params(labelsize=8) ax.set_zlabel(zlabel="Bray distance") ax.set_ylabel(ylabel="CDOM site 2") ax.set_xlabel(xlabel="CDOM site 1") # Set up the axes for the second plot ax = fig.add_subplot(1, 3, 2, projection='3d') ax.set_title( "Predicted BCC Bray distances by sites' CDOM, \n CDOM 0.01 step meshgrid", fontsize=fontsize) ax.view_init(elev=30.0, azim=300.0) # Plot the surface. ax.plot_trisurf( X_CDOM_diag_mesh.loc[:, "CDOM.x1"], X_CDOM_diag_mesh.loc[:, "CDOM.x2"], CDOM_pred_fine_mesh, #197109 datapoints cmap='viridis', edgecolor='none') # Customize the z axis. ax.set_zlim(0.3, 1) ax.zaxis.set_major_locator(LinearLocator(10)) ax.zaxis.set_major_formatter(FormatStrFormatter("%.02f")) ax.tick_params(labelsize=8) ax.set_zlabel(zlabel="Bray distance") ax.set_ylabel(ylabel="CDOM site 2") ax.set_xlabel(xlabel="CDOM site 1") # Set up the axes for the fourth plot ax = fig.add_subplot(1, 3, 3, projection='3d') ax.set_title( "Predicted BCC Bray distances by sites' CDOM, \n dataset CDOM coordinates", fontsize=fontsize) ax.view_init(elev=30.0, azim=300.0) # Plot the surface. ax.plot_trisurf( CDOM.loc[:, "CDOM.x1"], CDOM.loc[:, "CDOM.x2"], CDOM_pred, #197109 datapoints cmap='viridis', edgecolor='none') # Customize the z axis. ax.set_zlim(0.3, 1) ax.zaxis.set_major_locator(LinearLocator(10)) ax.zaxis.set_major_formatter(FormatStrFormatter("%.02f")) ax.tick_params(labelsize=8) ax.set_zlabel(zlabel="Bray distance") ax.set_ylabel(ylabel="CDOM site 2") ax.set_xlabel(xlabel="CDOM site 1") filename = outputPath + 'xgboost' + '_3d' + '.png' fig.savefig(filename) # evaluate predictions def TruePredictedTable(T, P): table = pd.DataFrame(np.concatenate((T, P), axis=1)) table.columns = ["True values", "Predicted values"] return table MSE_XGboost = mean_squared_error(y_test, y_pred) MSE_XGboost_train = mean_squared_error(y_train, y_pred_train) print("MSE on test data:", MSE_XGboost) print(TruePredictedTable(y_test[:, np.newaxis], y_pred[:, np.newaxis])) print("MSE on train data:", MSE_XGboost_train) print( TruePredictedTable(y_train[:, np.newaxis], y_pred_train[:, np.newaxis])) print("Best test MSE at epoch", XGboost_best_model_index) fig, ax = plt.subplots(1, 1) #plt.figure() ax.plot(list(range(0, len(MSE_per_epoch['validation_0']['rmse']), 1)), MSE_per_epoch['validation_1']['rmse'], '-', label='MSE test', linewidth=1) ax.plot(list(range(0, len(MSE_per_epoch['validation_0']['rmse']), 1)), MSE_per_epoch['validation_0']['rmse'], '-', label='MSE train', linewidth=1) ax.set_ylabel("MSE") ax.set_xlabel("Number of epochs") plt.grid(False) plt.legend(fontsize="medium") plt.show() filename = outputPath + 'xgboost' + '_best_mse' + '.png' fig.savefig(filename)
def plot_quantity(name, quan, nx, nz, dx, dz): ''' 3D-plot of specified quantity ''' xp = [] for ix in range(nx): x = ix * dx + dx / 2 xp.append(x) zp = [] for iz in range(nz): z = iz * dz + dz / 2 zp.append(z) xp, zp = np.meshgrid(xp, zp) val = [] max_val = -10000.0 min_val = 10000.0 for iz in range(nz): line = [] for ix in range(nx): pos = iz * nx + ix max_val = max(max_val, quan[pos]) min_val = min(min_val, quan[pos]) line.append(quan[pos]) val.append(line) #---- First subplot fig = plt.figure() ax = fig.add_subplot(1, 1, 1, projection='3d') #print 'max, min:', max_val, min_val surf = ax.plot_surface(xp, zp, val, rstride=1, cstride=1, cmap=cm.jet, antialiased=False) #surf = ax.plot_surface(xp, zp, val, rstride=8, cstride=8, cmap=cm.jet) ax.set_zlim(min_val - 0.01, max_val + 0.01) #if "err" in name: # ax.set_zlim(min_val-0.01, 0.201) #else: # ax.set_zlim(min_val-0.01, 1.501) ax.set_xlabel('x') ax.set_ylabel('y') ax.set_zlabel('z') #ax.set_zlim(0.0, 0.025) ax.zaxis.set_major_locator(LinearLocator(10)) ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) fig.colorbar(surf, shrink=0.5, aspect=5) picture = "pictures/error/3d/%s.png" % (name) #print 'plotting picture ', picture savefig(picture) plt.close(fig) #show() #---- Second subplot fig2 = plt.figure() ax2 = fig2.add_subplot(1, 1, 1, projection='3d') ax2.plot_wireframe(xp, zp, val, rstride=1, cstride=1, cmap=cm.jet) #ax2.set_zlim(min_val-0.01, max_val+0.01) #if "err" in name: # ax.set_zlim(min_val-0.01, 0.201) #else: # ax.set_zlim(min_val-0.01, 1.501) ax2.set_xlabel('x') ax2.set_ylabel('y') ax2.set_zlabel('z') ax2.set_zlim(0.0, 0.025) picture = "pictures/error/3d_wf/%s.png" % (name) #print 'plotting picture ', picture savefig(picture) plt.close(fig2)
def PlotResultsManualFFNN(self, *args): # getting inputs NN_reg_original = args[0] CDOM = args[1] # getting network type type = args[2] # output path were to save figures outputPath = args[3] # epochs epochs = args[4] # batch size batch = args[5] test_predict = NN_reg_original.predict(NN_reg_original.XTest) #best_prediction = NN_reg_original.models[NN_reg_original.accuracy_list.index(min(NN_reg_original.accuracy_list))] print(NN_reg_original.accuracy_list) print( "Minimum MSE :", min(NN_reg_original.accuracy_list), "reached at epoch ", NN_reg_original.accuracy_list.index( min(NN_reg_original.accuracy_list))) fig, ax = plt.subplots(1, 1) #plt.figure() ax.plot(list(range(0, len(NN_reg_original.accuracy_list), 1)), NN_reg_original.accuracy_list, '-', label='MSE', linewidth=1) ax.set_ylabel("MSE") ax.set_xlabel("Number of epochs") plt.grid(False) plt.legend(fontsize="medium") plt.legend() plt.yscale('log') plt.show() filename = outputPath + type + '_e' + str(epochs).zfill( 4) + '_l' + self.lossName + '_b' + str(batch) + '_mse' + '.png' fig.savefig(filename) #Use log-transformed CDOM values for creating design matrix, then plot on original values x_mesh = np.log10( np.arange(min(CDOM.loc[:, "CDOM.x1"]), max(CDOM.loc[:, "CDOM.x2"]) + 0.01, 0.01)) + 1 y_mesh = x_mesh.copy() x_mesh, y_mesh = np.meshgrid(x_mesh, y_mesh) X_CDOM_mesh = self.pdCat(x_mesh.ravel()[:, np.newaxis], y_mesh.ravel()[:, np.newaxis]).to_numpy() best_prediction = NN_reg_original.model_prediction( X_CDOM_mesh, NN_reg_original.accuracy_list.index( min(NN_reg_original.accuracy_list))) x_mesh = np.arange(min(CDOM.loc[:, "CDOM.x1"]), max(CDOM.loc[:, "CDOM.x2"]) + 0.01, 0.01) y_mesh = x_mesh.copy() x_mesh, y_mesh = np.meshgrid(x_mesh, y_mesh) ff_pred_original = best_prediction.copy() ff_pred_original = np.reshape(ff_pred_original, (363, 363)) ff_pred_original[x_mesh - y_mesh == 0] = np.nan ff_pred_original[x_mesh > y_mesh] = np.nan #print(pd.DataFrame(ff_pred_original)) # Plot the NN-smoothed surface fig = plt.figure(figsize=(15, 12)) ax = fig.gca(projection="3d") ax.set_title("Predicted BCC Bray distances by sites' CDOM", fontsize=12) ax.view_init(elev=30.0, azim=300.0) surf = ax.plot_surface(x_mesh, y_mesh, ff_pred_original, cmap='viridis', antialiased=True, vmin=np.nanmin(ff_pred_original), vmax=np.nanmax(ff_pred_original), rstride=1, cstride=1) # Customize the z axis. z_range = (np.nanmax(ff_pred_original) - np.nanmin(ff_pred_original)) ax.set_zlim( np.nanmin(ff_pred_original) - z_range, np.nanmax(ff_pred_original) + z_range) ax.zaxis.set_major_locator(LinearLocator(10)) ax.zaxis.set_major_formatter(FormatStrFormatter("%.02f")) # Add a color bar which maps values to colors. fig.colorbar(surf, shrink=0.5, aspect=5) ax.tick_params(labelsize=8) plt.show() filename = outputPath + type + '_e' + str(epochs).zfill( 4) + '_l' + self.lossName + '_b' + str(batch) + '_3d' + '.png' fig.savefig(filename)
def __init__(self, exponent, **kwargs): LinearLocator.__init__(self, **kwargs) self.exponent = exponent self.numticks = 5
def get3DPlotAnatomy(fig, og, labelNodes=False, alpha=0.05, subsample=4, verbose=True, degree=None, showSurface=True, showMidPlane=True, root=None): ax = Axes3D(fig) edges = og.edges(data=True) nodes = og.nodes(data=True) narray = np.zeros((len(nodes), 3), np.float32) axes = ax.get_axes() axes.set_axis_off() ax.set_axes(axes) for i in range(len(nodes)): narray[i, :] = nodes[i][1]['wcrd'] dg = og.degree(nodes[i][0]) if (labelNodes and (degree == None or dg == degree)): ax.text( narray[i, 0], narray[i, 1], narray[i, 2], "(%d,%d,%d)" % (nodes[i][0][0], nodes[i][0][1], nodes[i][0][2])) print("root is", root) if (root): rcrd = og.node[root]["wcrd"] ax.text(rcrd[0], rcrd[1], root[2], "Root") colors = ['r', 'g', 'b', 'y', 'm', 'c'] counter = 0 ss = 4 for e in edges: try: sp = e[2]['d0'] mps = e[2]['mappedPoints'] edgeDepth = nx.shortest_path_length(og, root, e[1]) if edgeDepth == 1: clr = 'r' elif edgeDepth == 2: clr = 'g' else: clr = 'b' ax.plot(sp[0], sp[1], sp[2], color=clr) if (showSurface): try: ax.scatter(mps[::ss, 0], mps[::ss, 1], mps[::ss, 2], color=clr, marker='+', alpha=0.05) except Exception as error: print("couldn't plot surface for edge (%s,%s). Surface points shape is %s. Error is %s"%\ (e[0],e[1],mps.shape,error)) if (showMidPlane): try: planePoints = e[2]['planePoints'] midpoint = len(list(planePoints.keys())) / 2 mps = np.array(e[2]['planePoints'][midpoint]) ax.scatter(mps[:, 0], mps[:, 1], mps[:, 2], color=clr, marker='+', alpha=0.5) except Exception as error: print("couldn't plot midplane points for edge (%s,%s). Plane points shape is %s. Error is %s"%\ (e[0],e[1],mps.shape,error)) counter += 1 except KeyError: print("cannot plot edge (%s,%s)" % (e[0], e[1])) except TypeError: if (e[2]['d0'] == None): print("No fitted data for edge (%s,%s)" % (e[0], e[1])) ax.scatter(narray[:, 0], narray[:, 1], narray[:, 2], color='k', marker='o', linewidth=3, picker=5) ax.scatter([rcrd[0]], [rcrd[1]], [rcrd[2]], color='r', marker='o', linewidth=10, picker=5) ax.w_zaxis.set_major_locator(LinearLocator(3)) ax.w_xaxis.set_major_locator(LinearLocator(3)) ax.w_yaxis.set_major_locator(LinearLocator(3)) return ax