def set_formatter(self, frmt = 'sci', axes = 'all', useOffset = True, limits = (-3, 3), index=None): """ Sets the formatter of the axes. Default is to set scientific notation for all axes of all subplots. keyword arguments: frmt -- Sets the type of formatter used, valid values are: 'sci', 'log', 'plain' (default: 'sci') axes -- which axes should the formatter be used for, valid values are: 'all', 'x', 'y' (default: 'all') useOffset -- Should offset be used to make the tickers more meaningful (default: True) limits -- Limits for scientific notation as a tuple (default: (-3, 3)) index -- a integer or list of integers with the index of sub-plots for which the formatter should set. When 'None' the formatter is set for all sub-plots (default: None) """ frmt = frmt.lower() axes = axes.lower() if frmt == 'log': formatter = LogFormatter() else: sci = frmt == 'sci' formatter = ScalarFormatter(useOffset = useOffset) formatter.set_powerlimits(limits) formatter.set_scientific(sci) # format axes if type(index) == list: for i in index: self.sub_plots(i).set_formatter(formatter, axes) elif type(index) == int: self.sub_plots(index).set_formatter(formatter, axes) else: # do all for sub_plot in self.sub_plots.sub_plots: sub_plot.set_formatter(formatter, axes) #set default formatter self.sub_plots.set_default_formatter(formatter, axes) # redraw screen self.canvas.draw()
cmap='RdYlBu', format='%.0e') #plt.contourf(x,y,data,ticks3,norm=colors.LogNorm(vmin=1e-5,vmax=8e4),cmap='RdYlBu_r', format = '%.0e') #plt.contourf(x,y,data,ticks3,norm=colors.LogNorm(vmin=pow(10,1),vmax=pow(10,6)),cmap=plt.cm.jet,resolution='c', format = '%.0e') #plt.clim((pow(10,3),pow(10,7))) plt.plot(lat1, trop_height.data, 'k--') mn = 0.1 mx = 100000 md = (mx - mn) / 2 print('\nThe maximum value of particle number is = ', np.max(data)) print('\nThe mean value of particle number at 11.5km = ', np.mean(data[39, :]), '\n') #plt.contourf(x,y,data,cmap=,resolution='c') formatter = LogFormatter(10, labelOnlyBase=False) cbar = plt.colorbar() #cbar=plt.colorbar(ticks=ticks3, format=formatter) #cbar.set_ticks(ticks4) cbar.set_ticks(ticks5) #ticks3=['1','5','10','20','50','1E2','2e2','5e2','1e3','2e3','5e3','1e4','3e4','6e4','1e5','2e5'] cbar.set_ticklabels(ticks5_label) #cbar.set_ticklabels(ticks3_label) #plt.yticks(['1','10']) #plt.clim((pow(10,3),pow(10,7))) plt.xlabel('latitude') plt.ylabel('altitude (km)') #plt.title('Total Particle number concentration (cm'+u'\u207B\u00B3'+')') plt.title('Change in Particle number concentration (cm' +
def plot_final_res(fname=None, work_dir=None, region_xyz=None, topo_xyz=None, method='constant', nxny=[1e2, 1e2], inv_col=3, plt_opts={}, miny=None, plt_opts2={}, region_plot=False, plot_buffer=1., cmap=plt.cm.inferno_r, save_dict=None, fwd_transfer_bool=False, invert_y=False): if fwd_transfer_bool == True: nheader = 1 else: nheader = 0 if isinstance(cmap, str): cmap = plt.get_cmap(cmap) if fname is None and work_dir is not None: inv_data = load_inv_output(work_dir=work_dir, nheader=nheader) elif os.path.dirname(fname) in ['']: inv_data = load_inv_output(fname=os.path.join(work_dir, fname), nheader=nheader) else: inv_data = load_inv_output(fname=fname, nheader=nheader) # Make regular grid for plotting surface x = np.linspace(np.min(inv_data[:, 0]), np.max(inv_data[:, 0]), nxny[0]) if miny is None: miny = np.min(inv_data[:, 1]) y = np.linspace(0., miny, nxny[1]) X, Y = np.meshgrid(x, y) if 'vmin' in plt_opts.keys(): vmin = 10.**plt_opts['vmin'] _ = plt_opts.pop('vmin', None) else: vmin = 10. if 'vmax' in plt_opts.keys(): vmax = 10.**plt_opts['vmax'] _ = plt_opts.pop('vmax', None) else: vmax = 1e3 if 'cmap' not in plt_opts.keys(): plt_opts['cmap'] = cmap formatter = LogFormatter(10, labelOnlyBase=False) ticks = log_steps([vmin, vmax]) if region_xyz is not None and topo_xyz is not None and region_plot: # Use region data to separate plotting zones # 1) Find elevation of divider (originally depth) div_y = extrap(region_xyz[:, 0], topo_xyz[:, 0], topo_xyz[:, 2], method) + region_xyz[:, 1] # 2) Find y locations above region divider div_y_all = extrap(X, region_xyz[:, 0], div_y, method) # 3) Find y shift for topography yshift = extrap(X, topo_xyz[:, 0], topo_xyz[:, 2], method) Y = Y + yshift lower_mask = (Y > div_y_all) | ((X > region_xyz[:, 0].max()) | (X < region_xyz[:, 0].min())) upper_mask = (Y < div_y_all) | ((X > region_xyz[:, 0].max()) | (X < region_xyz[:, 0].min())) all_ER = griddata(inv_data[:, :2], inv_data[:, inv_col], (X, Y), method='linear') if inv_col == 3: # Log transform already applied, change to untransformed all_ER = 10.**(all_ER) upper_ER = np.ma.masked_array(all_ER, mask=upper_mask) fig, ax = plt.subplots() s1 = ax.pcolormesh(X, Y, upper_ER, norm=LogNorm(vmin, vmax), **plt_opts) # ax.plot(X,Y,'g.') ax.plot(topo_xyz[:, 0], topo_xyz[:, 2], 'k-') ax.plot(region_xyz[:, 0], div_y, '-o', color='lightgrey') ax.set_xlim([ region_xyz[:, 0].min() - plot_buffer, region_xyz[:, 0].max() + plot_buffer ]) ax.set_ylim([miny - plot_buffer, np.max(Y) + plot_buffer]) plt.colorbar(s1, ax=ax, ticks=ticks, extend='both', format=formatter) ax2 = ax.twinx() lower_ER = np.ma.masked_array(all_ER, mask=lower_mask) s2 = ax.pcolormesh(X, Y, lower_ER, norm=LogNorm(vmin, vmax), **plt_opts2) ax2.set_xlim([ region_xyz[:, 0].min() - plot_buffer, region_xyz[:, 0].max() + plot_buffer ]) ax2.set_ylim([miny - plot_buffer, np.max(Y) + plot_buffer]) plt.colorbar(s2, ax=ax2, ticks=ticks, extend='both', format=formatter) elif topo_xyz is not None and region_xyz is not None: # 1) Find elevation of divider (originally depth) div_y = extrap(region_xyz[:, 0], topo_xyz[:, 0], topo_xyz[:, 2], method) + region_xyz[:, 1] # Find y shift for topography yshift = extrap(X, topo_xyz[:, 0], topo_xyz[:, 2], method) Y = Y + yshift upper_mask = (X > topo_xyz[:, 0].max()) | (X < topo_xyz[:, 0].min()) all_ER = griddata(inv_data[:, :2], inv_data[:, inv_col], (X, Y), method='linear') if inv_col == 3: # Log transform already applied, change to untransformed all_ER = 10.**(all_ER) upper_ER = np.ma.masked_array(all_ER, mask=upper_mask) fig, ax = plt.subplots() s1 = ax.pcolormesh(X, Y, upper_ER, norm=LogNorm(vmin, vmax), **plt_opts) # ax.plot(X,Y,'g.') ax.plot(topo_xyz[:, 0], topo_xyz[:, 2], 'k-') ax.plot(region_xyz[:, 0], div_y, '-o', color='lightgrey') ax.set_xlim([ topo_xyz[:, 0].min() - plot_buffer, topo_xyz[:, 0].max() + plot_buffer ]) ax.set_ylim([miny - plot_buffer, np.max(Y) + plot_buffer]) plt.colorbar(s1, ax=ax, ticks=ticks, extend='both', format=formatter) else: if topo_xyz is None: # Copy inv data output y coordinates topo_xyz = np.zeros_like(inv_data) topo_xyz[:, :2] = inv_data[:, :2] # Find y shift for topography yshift = extrap(X, topo_xyz[:, 0], topo_xyz[:, 2], method) Y = Y + yshift all_ER = griddata(inv_data[:, :2], inv_data[:, inv_col], (X, Y), method='linear') if inv_col == 3: # Log transform already applied, change to untransformed all_ER = 10.**(all_ER) upper_ER = np.ma.masked_invalid(all_ER) fig, ax = plt.subplots() s1 = ax.pcolormesh(X, Y, upper_ER, norm=LogNorm(vmin, vmax), **plt_opts) # ax.plot(X,Y,'g.') ax.plot(topo_xyz[:, 0], topo_xyz[:, 2], 'k-') ax.set_xlim([ topo_xyz[:, 0].min() - plot_buffer, topo_xyz[:, 0].max() + plot_buffer ]) ax.set_ylim([miny - plot_buffer, np.max(Y) + plot_buffer]) plt.colorbar(s1, ax=ax, ticks=ticks, extend='both', format=formatter) if invert_y: ax.set_ylim(ax.get_ylim()[::-1]) # put back in dictionary plt_opts['vmin'] = np.log10(vmin) plt_opts['vmax'] = np.log10(vmax) if save_dict is not None: fig.savefig(save_dict['fig_fname'], **save_dict['fig_opts']) plt.close('all') else: return fig, ax
def plotObsSF(interp, realm='SF', save=False, saven='', title='', scat=False, scatdata=[], fig_given=False, ax=None, fig=None, view_contours=True, ncol=100, nmag=120, **kwargs): options = { 'col_range': interp.SF_colrange, 'mag_range': interp.SF_magrange } options.update(kwargs) # Array for the coordinates in each dimension colmin, colmax = options['col_range'][0], options['col_range'][1] magmin, magmax = options['mag_range'][0], options['mag_range'][1] # Slight deviation inside boundaries to avoid going outside limits colmod = np.linspace(colmin + 1e-4, colmax - 1e-4, ncol) magmod = np.linspace(magmin + 1e-4, magmax - 1e-4, nmag) options = {'col': colmod, 'mag': magmod, 'pointings': []} options.update(kwargs) colmod = options['col'] magmod = options['mag'] # Labels and ticks for the grid plots axis_ticks = {'col': colmod, 'mag': magmod} axis_labels = {'col': r"J - K", 'mag': r"H"} Tfont = {'fontname': 'serif', 'weight': 100, 'fontsize': 20} Afont = {'fontname': 'serif', 'weight': 700, 'fontsize': 20} # Create 3D grids to find values of interpolants over col, mag, s col2d = np.transpose(np.stack([ colmod, ] * len(magmod)), (1, 0)) mag2d = np.transpose(np.stack([ magmod, ] * len(colmod)), (0, 1)) if realm == 'SF': grid = interp((mag2d, col2d)) elif realm == 'spectro': grid = interp.SF_interp((mag2d, col2d)) elif realm == 'photo': grid = interp.DF_interp((mag2d, col2d)) else: print('realm not correctly specified: SF or spectro or photo') # Contourf takes the transpose of the matrix grid = np.transpose(grid) lvls = np.logspace(-10, np.log10(np.max(grid) * 10), 20) lvls = lvls[lvls != np.inf] #lvls = np.logspace(-10, 8, 20 ) #lvls = np.logspace(np.log( np.min( grid ) ), np.log( np.max(grid) ), 20 ) print(np.max(grid)) if not fig_given: # Set up figure with correct number of plots fig = plt.figure(figsize=(10, 10)) print("oldfig") if view_contours: # Plot the grid on the plotting instance at the given inde im = plt.contourf(axis_ticks.get('col'), axis_ticks.get('mag'), grid, colormap='YlGnBu', levels=lvls, norm=LogNorm()) if not fig_given: # Add a colour bar to show the variation over the seleciton function. fig.colorbar(im) formatter = LogFormatter(10, labelOnlyBase=False) fig.colorbar(im, ticks=[1, 5, 10, 20, 50], format=formatter) else: #formatter = LogFormatter(10, labelOnlyBase=False) cb = fig.colorbar(im, ax=ax, ticks=[1e-10, 1E-5, 1E0, 1E5, 1E10], format='%.0E') # ticks=tks, format=formatter) cb.ax.set_yticklabels([ r'$10^{-10}$', r'$10^{-5}$', r'$10^{0}$', r'$10^{5}$', r'$10^{10}$' ], fontsize=25) plt.xlabel(r'$J-K$') plt.ylabel(r'$m_H$') plt.title(title) if scat: plt.scatter(scatdata[1], scatdata[0], zorder=1) # If the fig can be saved if save: fig.savefig(saven, bbox_inches='tight')
if len(random_data[proc]) > 0: random_mean[proc] = sum(random_data[proc]) / float( len(random_data[proc])) else: del stack_mean[proc] # plot stuff figure = plt.figure() axes = figure.add_subplot(111) axes.set_xlabel("Thread Count") axes.set_ylabel("Throughput (ops / ms)") #axes.set_xticks([0] + sizes) axes.set_xscale('log') axes.set_yscale('log') axes.xaxis.set_major_locator(LogLocator(base=2.0)) axes.xaxis.set_major_formatter(LogFormatter(base=2.0)) axes.yaxis.set_major_locator(LogLocator(base=10.0)) axes.yaxis.set_major_formatter(LogFormatter(base=10.0)) # plot MPI speedups x_data = sorted(stack_mean.keys()) y_data = [stack_mean[c] for c in sorted(stack_mean.keys())] axes.plot(x_data, y_data, "o-", color="red", label="Stack Access Pattern") x_data = sorted(queue_mean.keys()) y_data = [queue_mean[c] for c in sorted(queue_mean.keys())] axes.plot(x_data, y_data, "s-", color="blue", label="Queue Access Pattern") x_data = sorted(random_mean.keys()) y_data = [random_mean[c] for c in sorted(random_mean.keys())] axes.plot(x_data, y_data, "^-", color="green",
def plot_variable(u, name, direc, cmap='viridis', scale='lin', numLvls=12, umin=None, umax=None, tp=False, tpAlpha=0.5, show=True, hide_ax_tick_labels=False, label_axes=True, title='', use_colorbar=False, hide_axis=True, colorbar_loc='top'): """ """ mesh = u.function_space().mesh() v = u.compute_vertex_values(mesh) x = mesh.coordinates()[:,0] y = mesh.coordinates()[:,1] t = mesh.cells() d = os.path.dirname(direc) if not os.path.exists(d): os.makedirs(d) if umin != None: vmin = umin else: vmin = v.min() if umax != None: vmax = umax else: vmax = v.max() # countour levels : if scale == 'log': v[v < vmin] = vmin + 1e-12 v[v > vmax] = vmax - 1e-12 from matplotlib.ticker import LogFormatter levels = np.logspace(np.log10(vmin), np.log10(vmax), numLvls) formatter = LogFormatter(10, labelOnlyBase=False) norm = colors.LogNorm() elif scale == 'lin': v[v < vmin] = vmin + 1e-12 v[v > vmax] = vmax - 1e-12 from matplotlib.ticker import ScalarFormatter levels = np.linspace(vmin, vmax, numLvls) formatter = ScalarFormatter() norm = None elif scale == 'bool': from matplotlib.ticker import ScalarFormatter levels = [0, 1, 2] formatter = ScalarFormatter() norm = None fig = plt.figure(figsize=(width,height)) # ax = fig.add_subplot(111) ax = fig.gca(projection='3d') # ax.add_patch( # patches.Circle( # (np.pi, np.pi), # 1., # fill=False # remove background # ) # ) c = ax.plot_trisurf(x, y, v, triangles = t, cmap=cm.viridis, linewidth=0.2) # # # c = ax.tricontourf(x, y, t, v, levels=levels, norm=norm, # cmap=plt.get_cmap(cmap)) plt.axis('equal') if tp == True: p = ax.triplot(x, y, t, '-', lw=0.2, alpha=tpAlpha) ax.set_xlim([x.min(), x.max()]) ax.set_ylim([y.min(), y.max()]) if label_axes: ax.set_xlabel(r'$x$') ax.set_ylabel(r'$y$') ax.set_zlabel(r'Electric potential') if hide_ax_tick_labels: ax.set_xticklabels([]) ax.set_yticklabels([]) if hide_axis: plt.axis('off') # include colorbar : if scale != 'bool' and use_colorbar: divider = make_axes_locatable(plt.gca()) cax = divider.append_axes(colorbar_loc, "5%", pad="3%") cbar = fig.colorbar(c)#, cax=cax, format=formatter)#,ticks=levels) tit = plt.title(title) if use_colorbar: plt.tight_layout(rect=[.03,.03,0.97,0.97]) else: cbar = fig.colorbar(c) cbar.set_label(r'$\varPhi [V]$') plt.tight_layout() fig.set_tight_layout(True) plt.savefig(direc + name + '.eps',bbox_inches='tight', dpi=600) plt.savefig(direc + name + '.png',bbox_inches='tight', dpi=600) if show: plt.show() plt.close(fig)
def plotAnalysis(log_process, log_base, gamma): dir_path = "results/" process_data_path = "data/processed_data/" diagram_path = "results/" if (os.path.exists(diagram_path) == False): os.mkdir(diagram_path) p = np.load(dir_path + "test_prediction.npy") t = np.load(dir_path + "test_truth.npy") parameters = np.load(dir_path + "test_parameters.npy") sun = np.load(dir_path + "test_sun_p.npy") dgps_p = np.load(dir_path + "dgps_p.npy") dgps_t = np.load(dir_path + "dgps_t.npy") num_images = p.shape[0] dgps_acc_p = np.zeros(num_images) dgps_acc_t = np.zeros(num_images) dgps_acc_diff = np.zeros(num_images) dgps_mean_diff = np.zeros(num_images) dgps_max_diff = np.zeros(num_images) for i in range(num_images): for j in range(10): index = i * 10 + j dgps_acc_p[i] = dgps_acc_p[i] + dgps_p[index] dgps_acc_t[i] = dgps_acc_t[i] + dgps_t[index] dgps_acc_diff[i] = dgps_acc_diff[i] + abs(dgps_p[index] - dgps_t[index]) dgps_max_diff[i] = max(dgps_max_diff[i], abs(dgps_p[index] - dgps_t[index])) dgps_mean_diff[i] = dgps_acc_diff[i] / 10 dgps_acc_p[i] = dgps_acc_p[i] / 10 dgps_acc_t[i] = dgps_acc_t[i] / 10 diff_origin = abs(p - t) diff_process, t, p = postProcessIms(t, p, "test_", num_images, gamma, process_data_path) al = parameters[:, :, :, 2] al = al[:, 0, 0] az = parameters[:, :, :, 3] az = az[:, 0, 0] dir = parameters[:, :, :, 4] dir = dir[:, 0, 0] dif = parameters[:, :, :, 5] dif = dif[:, 0, 0] AL_MIN, AL_MAX, AZ_MIN, AZ_MAX, DIR_MIN, DIR_MAX, DIF_MIN, DIF_MAX = getMaxMinPrams( "test_", process_data_path) al = restorNorm(al, AL_MAX, AL_MIN) az = restorNorm(az, AZ_MAX, AZ_MIN) dir = restorNorm(dir, DIR_MAX, DIR_MIN) dif = restorNorm(dif, DIF_MAX, DIF_MIN) angle = im_angle().reshape(230, 115) mse_angle = [] rer_angle = [] for i in range(num_images): mse_angle_im = np.mean((t[i, :] * angle - p[i, :] * angle)**2) rer_angle_im = get_relative_error(t[i, :] * angle, p[i, :] * angle) mse_angle.append(mse_angle_im) rer_angle.append(rer_angle_im) #3)solid angle mse scatter plot; fig = plt.figure() ax = plt.axes(projection='3d') ax.scatter3D(al, az, mse_angle, s=1, c='red') # Plot settings: ax.set_xlim3d(min(al), max(al)) ax.set_ylim3d(min(az), max(az)) ax.set_zlim3d(min(mse_angle), max(mse_angle)) plt.xlabel("Sun Altitude") plt.ylabel("Sun Azimuth") plt.title("Solid Angle MSE 3D Scatter Plot") #ax.set_zlabel("Solid Angle Weighted MSE") plt.savefig(diagram_path + "mse_scatter.png") plt.close() #4)solid angle rer scatter plot; fig = plt.figure() ax = plt.axes(projection='3d') ax.scatter3D(al, az, rer_angle, s=1, c='red', vmin=min(rer_angle), vmax=max(rer_angle), marker='o') # Plot settings: ax.set_xlim3d(min(al), max(al)) ax.set_ylim3d(min(az), max(az)) ax.set_zlim3d(min(rer_angle), max(rer_angle)) plt.xlabel("Sun Altitude") plt.ylabel("Sun Azimuth") plt.title("Solid Angle RER 3D Scatter Plot") #ax.set_zlabel("Solid Angle Weighted MSE") plt.savefig(diagram_path + "rer_scatter.png") plt.close() diff_log, t, p = post_process_log(t, p, log_process, log_base, num_images) diff_process = diff_process.reshape(num_images, 230, 115, 1) diff_log = diff_log.reshape(num_images, 230, 115, 1) print("individual mse values:") for i in range(t.shape[0]): print(np.mean(np.square(t[i, :] - p[i, :]))) print("finish individual mse values:") print("individual rer values:") for i in range(t.shape[0]): print(get_relative_error(t[i, :], p[i, :])) print("finish individual rer values:") #delete pixel when diff > e+06 for i in range(diff_log.shape[0]): for j in range(diff_log.shape[1]): for x in range(diff_log.shape[2]): if diff_log[i, j, x] > 10**6: diff_log[i, j, x] = 0 t[i, j, x] = 0 p[i, j, x] = 0 print("new mse is: ") print(np.mean(np.square(t - p))) #1)accumulated mse: origin_sum = np.sum(diff_origin, axis=0) / num_images fig = plt.figure(figsize=(10, 10)) plt.imshow(origin_sum.reshape(230, 115), cmap="jet", norm=LogNorm(vmin=origin_sum.min(), vmax=origin_sum.max())) # add color index bar cbaxes = fig.add_axes([0.8, 0.1, 0.03, 0.8]) formatter = LogFormatter(2, labelOnlyBase=False) #cb = plt.colorbar(ticks=[1, 4.1, 12.2, 36.5, 109, 325.8, 973.4, 3000], format=formatter, cax=cbaxes) cb = plt.colorbar(format=formatter, cax=cbaxes) cb.set_label('Luminance (cd/m2)', rotation=90) # save the image plt.savefig( os.path.join(diagram_path + "average_accumulated_absolute_error_origin.png")) plt.close() #1)accumulated mse: process_sum = np.sum(diff_process, axis=0) / num_images fig = plt.figure(figsize=(10, 10)) plt.imshow(process_sum.reshape(230, 115), cmap="jet", norm=LogNorm(vmin=process_sum.min(), vmax=process_sum.max())) # add color index bar cbaxes = fig.add_axes([0.8, 0.1, 0.03, 0.8]) formatter = LogFormatter(2, labelOnlyBase=False) #cb = plt.colorbar(ticks=[1, 4.1, 12.2, 36.5, 109, 325.8, 973.4, 3000], format=formatter, cax=cbaxes) cb = plt.colorbar(format=formatter, cax=cbaxes) cb.set_label('Luminance (cd/m2)', rotation=90) # save the image plt.savefig( os.path.join(diagram_path + "average_accumulated_absolute_error_process.png")) plt.close() #1)accumulated mse: log_sum = np.sum(diff_log, axis=0) / num_images fig = plt.figure(figsize=(10, 10)) plt.imshow(log_sum.reshape(230, 115), cmap="jet", norm=LogNorm(vmin=log_sum.min(), vmax=log_sum.max())) # add color index bar cbaxes = fig.add_axes([0.8, 0.1, 0.03, 0.8]) formatter = LogFormatter(10, labelOnlyBase=False) #cb = plt.colorbar(ticks=[1, 4.1, 12.2, 36.5, 109, 325.8, 973.4, 3000], format=formatter, cax=cbaxes) cb = plt.colorbar(format=formatter, cax=cbaxes) cb.set_label('Luminance (cd/m2)', rotation=90) # save the image plt.savefig( os.path.join(diagram_path + "average_accumulated_absolute_luminance_error_log.png")) plt.close() mse_angle = [] rer_angle = [] for i in range(num_images): mse_angle_im = np.mean((t[i, :] * angle - p[i, :] * angle)**2) rer_angle_im = get_relative_error(t[i, :] * angle, p[i, :] * angle) mse_angle.append(mse_angle_im) rer_angle.append(rer_angle_im) #3)solid angle mse scatter plot; fig = plt.figure() ax = plt.axes(projection='3d') ax.scatter3D(al, az, mse_angle, s=1, c='red') # Plot settings: ax.set_xlim3d(min(al), max(al)) ax.set_ylim3d(min(az), max(az)) ax.set_zlim3d(min(mse_angle), max(mse_angle)) plt.xlabel("Sun Altitude") plt.ylabel("Sun Azimuth") plt.title("Solid Angle MSE 3D Scatter Plot") #ax.set_zlabel("Solid Angle Weighted MSE") plt.savefig(diagram_path + "mse_scatter_after.png") plt.close() #4)solid angle rer scatter plot; fig = plt.figure() ax = plt.axes(projection='3d') ax.scatter3D(al, az, rer_angle, s=1, c='red', vmin=min(rer_angle), vmax=max(rer_angle), marker='o') # Plot settings: ax.set_xlim3d(min(al), max(al)) ax.set_ylim3d(min(az), max(az)) ax.set_zlim3d(min(rer_angle), max(rer_angle)) plt.xlabel("Sun Altitude") plt.ylabel("Sun Azimuth") plt.title("Solid Angle RER 3D Scatter Plot") #ax.set_zlabel("Solid Angle Weighted MSE") plt.savefig(diagram_path + "rer_scatter_after.png") plt.close() #dgp scatter 3d plot fig = plt.figure() ax = plt.axes(projection='3d') ax.scatter3D(al, az, dgps_mean_diff, s=3, c='red', vmin=min(dgps_mean_diff), vmax=max(dgps_mean_diff), marker='o') # Plot settings: ax.set_xlim3d(min(al), max(al)) ax.set_ylim3d(min(az), max(az)) ax.set_zlim3d(min(dgps_mean_diff), max(dgps_mean_diff)) plt.xlabel("Sun Altitude") plt.ylabel("Sun Azimuth") plt.title("Mean DGP Diff 3D Scatter Plot") #ax.set_zlabel("Solid Angle Weighted MSE") plt.savefig(diagram_path + "DGP_scatter_after.png") plt.close() #dgp scatter 3d plot fig = plt.figure() ax = plt.axes(projection='3d') ax.scatter3D(al, az, dgps_acc_diff, s=3, c='red', vmin=min(dgps_acc_diff), vmax=max(dgps_acc_diff), marker='o') # Plot settings: ax.set_xlim3d(min(al), max(al)) ax.set_ylim3d(min(az), max(az)) ax.set_zlim3d(min(dgps_acc_diff), max(dgps_acc_diff)) plt.xlabel("Sun Altitude") plt.ylabel("Sun Azimuth") plt.title("Accumulated DGP Diff 3D Scatter Plot") #ax.set_zlabel("Solid Angle Weighted MSE") plt.savefig(diagram_path + "acc_DGP_scatter_after.png") plt.close() #5)dgp scatter plot per hour fig = plt.figure() plt.scatter(np.arange(num_images), dgps_mean_diff, s=3, c='red', vmin=min(dgps_mean_diff), vmax=max(dgps_mean_diff), marker='o') # Plot settings: ax.set_xlim3d(min(al), max(al)) ax.set_ylim3d(min(az), max(az)) ax.set_zlim3d(min(dgps_mean_diff), max(dgps_mean_diff)) plt.xlabel("Point of Time") plt.ylabel("Mean DGP Diff") plt.title("Mean DGP Diff 2D Scatter Plot") #ax.set_zlabel("Solid Angle Weighted MSE") plt.savefig(diagram_path + "DGP_2dscatter_after.png") plt.close() #5)dgp scatter plot per hour fig = plt.figure() plt.scatter(np.arange(num_images), dgps_max_diff, s=3, c='red', vmin=min(dgps_max_diff), vmax=max(dgps_max_diff), marker='o') # Plot settings: ax.set_xlim3d(min(al), max(al)) ax.set_ylim3d(min(az), max(az)) ax.set_zlim3d(min(dgps_max_diff), max(dgps_max_diff)) plt.xlabel("Point of Time") plt.ylabel("Max DGP Diff") plt.title("Max DGP Diff 2D Scatter Plot") #ax.set_zlabel("Solid Angle Weighted MSE") plt.savefig(diagram_path + "max_DGP_2dscatter_after.png") plt.close() #5)dgp scatter plot per al fig = plt.figure() plt.scatter(al, dgps_max_diff, s=3, c='red', vmin=min(dgps_max_diff), vmax=max(dgps_max_diff), marker='o') plt.xlabel("Altitude") plt.ylabel("Max DGP Diff") plt.title("Max DGP Diff 2D Scatter Plot") #ax.set_zlabel("Solid Angle Weighted MSE") plt.savefig(diagram_path + "max_DGP_2dscatter_al_after.png") plt.close() #5)dgp scatter plot per az fig = plt.figure() plt.scatter(az, dgps_max_diff, s=3, c='red', vmin=min(dgps_max_diff), vmax=max(dgps_max_diff), marker='o') # Plot settings: plt.xlabel("Azimuth") plt.ylabel("Max DGP Diff") plt.title("Max DGP Diff 2D Scatter Plot") #ax.set_zlabel("Solid Angle Weighted MSE") plt.savefig(diagram_path + "max_DGP_2dscatter_az_after.png") plt.close()
def plot_variable(u, name, direc, cmap='gist_yarg', scale='lin', numLvls=12, umin=None, umax=None, tp=False, tpAlpha=0.5, show=True, hide_ax_tick_labels=False, label_axes=True, title='', use_colorbar=True, hide_axis=False, colorbar_loc='right'): """ """ mesh = u.function_space().mesh() v = u.compute_vertex_values(mesh) x = mesh.coordinates()[:, 0] y = mesh.coordinates()[:, 1] t = mesh.cells() d = os.path.dirname(direc) if not os.path.exists(d): os.makedirs(d) if umin != None: vmin = umin else: vmin = v.min() if umax != None: vmax = umax else: vmax = v.max() # countour levels : if scale == 'log': v[v < vmin] = vmin + 1e-12 v[v > vmax] = vmax - 1e-12 from matplotlib.ticker import LogFormatter levels = np.logspace(np.log10(vmin), np.log10(vmax), numLvls) formatter = LogFormatter(10, labelOnlyBase=False) norm = colors.LogNorm() elif scale == 'lin': v[v < vmin] = vmin + 1e-12 v[v > vmax] = vmax - 1e-12 from matplotlib.ticker import ScalarFormatter levels = np.linspace(vmin, vmax, numLvls) formatter = ScalarFormatter() norm = None elif scale == 'bool': from matplotlib.ticker import ScalarFormatter levels = [0, 1, 2] formatter = ScalarFormatter() norm = None fig = plt.figure(figsize=(5, 5)) ax = fig.add_subplot(111) c = ax.tricontourf(x, y, t, v, levels=levels, norm=norm, cmap=plt.get_cmap(cmap)) plt.axis('equal') if tp == True: p = ax.triplot(x, y, t, '-', lw=0.2, alpha=tpAlpha) ax.set_xlim([x.min(), x.max()]) ax.set_ylim([y.min(), y.max()]) if label_axes: ax.set_xlabel(r'$x$') ax.set_ylabel(r'$y$') if hide_ax_tick_labels: ax.set_xticklabels([]) ax.set_yticklabels([]) if hide_axis: plt.axis('off') # include colorbar : if scale != 'bool' and use_colorbar: divider = make_axes_locatable(plt.gca()) cax = divider.append_axes(colorbar_loc, "5%", pad="3%") cbar = plt.colorbar(c, cax=cax, format=formatter, ticks=levels) tit = plt.title(title) if use_colorbar: plt.tight_layout(rect=[.03, .03, 0.97, 0.97]) else: plt.tight_layout() plt.savefig(os.path.join(direc, name + '.png'), dpi=300) if show: plt.show() plt.close(fig)
def plotBaseTriples(dfUnqClusters, dfMut, field='params2.median', transform=None, ref=None, orderBaseTriple=None, figsize=(12, 12), suptitle=None, actLabel=None, vmin=None, vmax=None, cmap=None, c_bad='0.55', robust=True, logscale=False, unit='min', show=True, titlefontsize=28, suptitlefontsize=26, **kwargs): # Define unit. Default is min if unit in ['second', 's', 'sec']: unitTime = 1 else: unitTime = 60 # Make dfUnqClusters indexed by annotation if not already if dfUnqClusters.index.name == 'annotation': dfUnqClusters2 = dfUnqClusters.copy() else: dfUnqClusters2 = dfUnqClusters.set_index('annotation') # See if user has provided an reference annotation; if so, compute # reference activity from reference annotation computeRefFromAnnt = isinstance(ref, basestring) # Define some commonly used transform functions if transform == 'kobs': def transformFunc(x): return 1. / x * unitTime logscale = False actLabel = r'$\mathrm{\mathsf{k_{obs}\ (min^{-1})}}$' if computeRefFromAnnt: ref = transformFunc(dfUnqClusters2.loc[ref][field]) if ref: default_cmap = 'RdBu' else: default_cmap = 'YlOrRd' elif transform == 'logkobs': def transformFunc(x): return 1. / x * unitTime logscale = True actLabel = r'$\mathrm{\mathsf{k_{obs}\ (min^{-1})}}$' if computeRefFromAnnt: ref = transformFunc(dfUnqClusters2.loc[ref][field]) if ref: default_cmap = 'RdBu' else: default_cmap = 'YlOrRd' elif transform == 'kobsfold': if computeRefFromAnnt: ref_kobs = (1. / dfUnqClusters2.loc[ref][field] * unitTime) def transformFunc(x): return ref_kobs / (1. / x * unitTime) logscale = False actLabel = r'$\mathrm{\mathsf{k_{obs}\ fold\ change}}$' ref = 1 default_cmap = 'RdBu_r' elif transform == 'logkobsfold': if computeRefFromAnnt: ref_kobs = (1. / dfUnqClusters2.loc[ref][field] * unitTime) def transformFunc(x): return ref_kobs / (1. / x * unitTime) logscale = True actLabel = r'$\mathrm{\mathsf{k_{obs}\ fold\ change}}$' ref = 1 default_cmap = 'RdBu_r' elif transform is None: def transformFunc(x): return x if computeRefFromAnnt: ref = transformFunc(dfUnqClusters2.loc[ref][field]) if ref: default_cmap = 'RdBu' else: default_cmap = 'YlOrRd' else: transformFunc = transform if computeRefFromAnnt: ref = transformFunc(dfUnqClusters2.loc[ref][field]) if ref: default_cmap = 'RdBu' else: default_cmap = 'YlOrRd' # Get the reordering index of 3 bases within the base triple if orderBaseTriple is provided # First base is the non-base-paired base, last two bases are base paired if orderBaseTriple is not None: listSeqPos = [mut[:-1] for mut in dfMut['mutations'][0]] orderListSeqPos = [ listSeqPos.index(seqPos) for seqPos in orderBaseTriple ] else: orderListSeqPos = [0, 1, 2] # Construct a new series of the transformed activity as specified by field # of the all the base triple variants seriesAct = transformFunc(dfUnqClusters2.loc[dfMut['annotation']][field]) # Get multiindex from the list of tuples of mutations in dfMut # and reassign index as multiindex seriesAct.index = pd.MultiIndex.from_tuples(dfMut['mutations']) # Reorder index levels according to orderListSeqPos seriesAct = seriesAct.reorder_levels(orderListSeqPos) # Get the finite numbers for calculations seriesAct_finite = seriesAct[np.isfinite(seriesAct)] # Get all 4 mutations at the each of the base position listMutFirstBase = seriesAct.index.levels[0] listMutSecondBase = seriesAct.index.levels[1] listMutThirdBase = seriesAct.index.levels[2][::-1] # Find the multi-index of the WT variant WT_firstbase = [ i for i, mut in enumerate(listMutFirstBase) if mut[0] == mut[-1] ][0] WT_secondbase = [ i for i, mut in enumerate(listMutSecondBase) if mut[0] == mut[-1] ][0] WT_thirdbase = [ i for i, mut in enumerate(listMutThirdBase) if mut[0] == mut[-1] ][0] # Make figure fig, axes = plt.subplots(2, 2, figsize=figsize) axes = axes.flatten() fig.tight_layout(rect=[0, 0, .87, 0.96]) cbar_ax = fig.add_axes([.865, .1, .03, .8]) # Set parameters for plotting # Set robust vmin and vmax if vmin is None: vmin = np.percentile(seriesAct_finite, 2) if robust else min(seriesAct_finite) if vmax is None: vmax = np.percentile(seriesAct_finite, 98) if robust else max(seriesAct_finite) # Take log if requested. Transform vmin and vmax if ref is provided if logscale: seriesAct = np.log10(seriesAct) vmin, vmax = np.log10(vmin), np.log10(vmax) if ref: ref = np.log10(ref) vlim = max(abs(vmin - ref), abs(vmax - ref)) vmin, vmax = -vlim + ref, vlim + ref ticks = np.hstack( [np.logspace(vmin, ref, 8), np.logspace(ref, vmax, 8)]) else: ticks = np.logspace(vmin, vmax, 8) cbar_norm = mpl.colors.LogNorm(vmin=10**vmin, vmax=10**vmax) formatter = LogFormatter(10, labelOnlyBase=False) else: if ref: vlim = max(abs(vmin - ref), abs(vmax - ref)) vmin, vmax = -vlim + ref, vlim + ref ticks = None cbar_norm = mpl.colors.Normalize(vmin=vmin, vmax=vmax) formatter = None # Define colormap if cmap is None: cmap = plt.get_cmap(default_cmap) elif isinstance(cmap, basestring): cmap = plt.get_cmap(cmap) cmap.set_bad(c_bad, 0.8) # Plotting the 4 different heatmaps for i, mut in enumerate(listMutFirstBase): # Get the 2D-ized DF of all the variants with the particular mutation in the first base currDF = seriesAct.loc[mut].unstack(level=-1).sort_index( axis=1, ascending=False) mask = ~np.isfinite(currDF) # Plot heatmap sns.heatmap(currDF, ax=axes[i], square=True, mask=mask, cbar=False, vmin=vmin, vmax=vmax, cmap=cmap, center=ref) # Draw a box around the WT variant if i == WT_firstbase: _drawBox(axes[i], WT_thirdbase, 3 - WT_secondbase) setproperties(ax=axes[i], yticklabelrot=90, title=mut, titlefontsize=titlefontsize, suptitle=suptitle, suptitlefontsize=suptitlefontsize, borderwidth=0, tight=False, **kwargs) # Plot colorbar if i == 0: cbar = mpl.colorbar.ColorbarBase(cbar_ax, cmap=cmap, norm=cbar_norm, ticks=ticks, format=formatter) cbar.ax.tick_params(labelsize=17) if actLabel is not None: cbar.set_label(actLabel, fontsize=24) if show: plt.show(block=False) return fig, axes, cbar_ax
def show_slice( ax, filename, axis, where, method='nearest', rsx=100, rsy=100, rsz=100, window=(0., 1.), nlevels=100, vmin=None, vmax=None, extend='neither', logscale=False, save=False, zorder=0, **kwargs ): """ Opens a file 'filename' and shows the data at a slice perpendicular to the 'axis' axis at a relative position 'where', where 'where' ranges from 0. to 1. """ # Keyword arguments if len(kwargs) >= 2: xyzunits = kwargs['xyzunits'] vunits = kwargs['vunits'] else: xyzunits = '' vunits = '' try: cmap = kwargs['cmap'] except KeyError: cmap = plt.cm.jet try: show_data_points = kwargs['show_data_points'] except KeyError: show_data_points = False try: show_intp_points = kwargs['show_intp_points'] except KeyError: show_intp_points = False # Set colors for bad values if False: cmap.set_under(color='black') cmap.set_over(color='black') if False: cmap.set_bad(color='black') # Load data data = np.loadtxt(filename, delimiter=',') x = data[:,0] y = data[:,1] z = data[:,2] v = data[:,3] # Sides xspan = x.max() - x.min() yspan = y.max() - y.min() zspan = z.max() - z.min() # Window wl, wr = window # Crop the window limits just in case wl = max(wl, 0.) wl = min(wl, 1.) wr = max(wr, 0.) wr = min(wr, 1.) # Regular mesh xi_ = np.linspace(x.min(), x.max(), rsx) yi_ = np.linspace(y.min(), y.max(), rsy) zi_ = np.linspace(z.min(), z.max(), rsz) if axis == 'x': # Indices for the windows wl_i = int(rsx * wl) wr_i = int(rsx * wr) # Create the mask for the data xleft = x.min() + xspan * wl xrght = x.min() + xspan * wr mask = np.ma.masked_where((x >= xleft) & (x <= xrght), x).mask # Crop the corresponding array for the regular mesh xi_ = xi_[wl_i:wr_i] elif axis == 'y': # Indices for the windows wl_i = int(rsy * wl) wr_i = int(rsy * wr) # Create the mask for the data yleft = y.min() + yspan * wl yrght = y.min() + yspan * wr mask = np.ma.masked_where((y >= yleft) & (y <= yrght), y).mask # Crop the corresponding array for the regular mesh yi_ = yi_[wl_i:wr_i] elif axis == 'z': # Indices for the windows wl_i = int(rsz * wl) wr_i = int(rsz * wr) # Create the mask for the data zleft = z.min() + zspan * wl zrght = z.min() + zspan * wr mask = np.ma.masked_where((z >= zleft) & (z <= zrght), z).mask # Crop the corresponding array for the regular mesh zi_ = zi_[wl_i:wr_i] # 'mesh' the regular mesh xi, yi, zi = np.meshgrid(xi_, yi_, zi_) # Mask data x = x[mask] y = y[mask] z = z[mask] v = v[mask] # Interpolation t0 = datetime.now() vi = griddata((x,y,z), v, (xi,yi,zi), method=method) t1 = datetime.now() print('Time needed: %f s'%((t1 - t0).total_seconds())) # Figure # Levels for colouring vi_mkd_cpd = np.ma.masked_where(np.isnan(vi), vi).compressed() vmin_, vmax_ = vi_mkd_cpd.min(), vi_mkd_cpd.max() if vmax is None: vmax = vmax_ if vmin is None: vmin = vmin_ # levels_i = np.linspace(vmin, vmax, nlevels) # Avoid an error due to a flat level list if vmin == vmax: vmin -= 0.1 vmax += 0.1 # Re-build the levels print('Limits:', vmin, vmax) levels_i = np.linspace(vmin, vmax, nlevels) loglevels = np.logspace( np.log10(min(np.abs(vmin), np.abs(vmax))), np.log10(max(np.abs(vmin), np.abs(vmax))), nlevels ) # Minimum and maximum exponents min_exp = int(np.log10(min(np.abs(vmin), np.abs(vmax)))) max_exp = int(np.log10(max(np.abs(vmin), np.abs(vmax)))) + 1 # Ticks for the colorbar in case it's logscale cbticks = 10. ** np.arange(min_exp, max_exp + 1, 1) cbticks_ = cbticks.tolist() for i in range(2, 10, 1): cbticks_ = cbticks_ + (float(i) * cbticks).tolist() cbticks_.sort() # print(cbticks_, min_exp, max_exp) cbticklabels = [] for c in cbticks_: if c in (20, 50, 100, 200, 500, 1000): cbticklabels.append('%i'%c) else: cbticklabels.append('') if axis == 'x': s = int(where * rsx) - wl_i if not logscale: cf = ax.contourf( yi[:, s, :], zi[:, s, :], vi[:, s, :], levels_i, cmap=cmap, extend=extend, zorder=zorder ) else: cf = ax.contourf( yi[:, s, :], zi[:, s, :], np.abs(vi[:, s, :]), loglevels, cmap=cmap, norm=LogNorm( vmin=min(np.abs(vmin), np.abs(vmax)), vmax=max(np.abs(vmin), np.abs(vmax)) ), # locator=ticker.LogLocator(), zorder=zorder ) if show_data_points: ax.plot(y, z, 'k.', markersize=0.6) if show_intp_points: ax.scatter(yi, zi, c='k', s=1) ax.set_title('x = %f'%xi[s, s, s] + ' ' + xyzunits) ax.set_xlabel('y (' + xyzunits + ')') ax.set_ylabel('z (' + xyzunits + ')') elif axis == 'y': s = int(where * rsy) - wl_i if not logscale: cf = ax.contourf( xi[s, :, :], zi[s, :, :], vi[s, :, :], levels_i, cmap=cmap, extend=extend, zorder=zorder ) else: cf = ax.contourf( xi[s, :, :], zi[s, :, :], np.abs(vi[s, :, :]), loglevels, cmap=cmap, norm=LogNorm( vmin=min(np.abs(vmin), np.abs(vmax)), vmax=max(np.abs(vmin), np.abs(vmax)) ), # locator=ticker.LogLocator(), zorder=zorder ) if show_data_points: ax.plot(x, z, 'k.', markersize=0.6) if show_intp_points: ax.scatter(xi, zi, c='k', s=1) ax.set_title('y = %f'%yi[s, s, s] + ' ' + xyzunits) ax.set_xlabel('x (' + xyzunits + ')') ax.set_ylabel('z (' + xyzunits + ')') elif axis == 'z': s = int(where * rsz) - wl_i if not logscale: cf = ax.contourf( xi[:, :, s], yi[:, :, s], vi[:, :, s], levels_i, cmap=cmap, extend=extend, zorder=zorder ) else: cf = ax.contourf( xi[:, :, s], yi[:, :, s], np.abs(vi[:, :, s]), loglevels, cmap=cmap, norm=LogNorm( vmin=min(np.abs(vmin), np.abs(vmax)), vmax=max(np.abs(vmin), np.abs(vmax)) ), # locator=ticker.LogLocator(), zorder=zorder ) if show_data_points: ax.plot(x, y, 'k.', markersize=0.6) if show_intp_points: ax.scatter(xi, yi, c='k', s=1) ax.set_title('z = %f'%zi[s, s, s] + ' ' + xyzunits) ax.set_xlabel('x (' + xyzunits + ')') ax.set_ylabel('y (' + xyzunits + ')') ax.set_aspect('equal') if logscale: l_f = LogFormatter(10, labelOnlyBase=False) # cb = plt.colorbar(cf, ticks=cbticks_, format=l_f) cb = plt.colorbar(cf, format=l_f) # cb = plt.colorbar(cf, ticks=cbticks) # cb = plt.colorbar(cf) cb.set_ticks(cbticks_) # cb.set_ticklabels(cbticklabels) # cb.set_norm(LogNorm()) # print(cbticklabels) else: cb = plt.colorbar(cf, ax=ax) cb.set_label(vunits)
def plot_daily_trends(df, minpop=2e+5, ndays=100, lastday=-1, mun_regexp=None, use_r7=True): """Plot daily-case trends. - df: DataFrame with processed per-municipality data. - minpop: minimum city population - lastday: up to this day. - use_r7: whether to use 7-day rolling average. """ fig, ax = plt.subplots(figsize=(12, 6)) fig.subplots_adjust(top=0.945, bottom=0.085, left=0.09, right=0.83) # dict: municitpality -> population muns = df_mun.loc[df_mun['Inwoners'] > minpop]['Inwoners'].to_dict() muns['Nederland'] = float(df_mun.sum()) labels = [] # tuples (y, txt)f citystats = [] # tuples (Rt, T2, cp100k, cwk, popk, city_name) for mun, n_inw in muns.items(): if mun_regexp and not re.match(mun_regexp, mun): continue df1 = get_mun_data(df, mun, n_inw, lastday=lastday) df1 = df1.iloc[-ndays:] fmt = 'o-' if ndays < 70 else '-' psize = 5 if ndays < 30 else 3 dnc_column = 'Delta7r' if use_r7 else 'Delta' ax.semilogy(df1[dnc_column] * 1e5, fmt, label=mun, markersize=psize) delta_t = 7 i0 = -3 if use_r7 else -1 t_double, Rt = get_t2_Rt(df1, delta_t, i0=i0, use_r7=use_r7) citystats.append( (np.around(Rt, 2), np.around(t_double, 2), np.around(df1['Delta'][-1] * 1e5, 2), int(df1['Delta7r'][-4] * n_inw * 7 + 0.5), int(n_inw / 1e3 + .5), mun)) if abs(t_double) > 60: texp = f'Stabiel' elif t_double > 0: texp = f'×2: {t_double:.3g} d' elif t_double < 0: texp = f'×½: {-t_double:.2g} d' ax.semilogy(df1.index[[i0 - delta_t, i0]], df1[dnc_column].iloc[[i0 - delta_t, i0]] * 1e5, 'k--', zorder=-10) labels.append((df1[dnc_column][-1] * 1e5, f' {mun} ({texp})')) y_lab = ax.get_ylim()[0] for res_t, res_d in df_restrictions['Description'].iteritems(): # if res_t >= t_min: ax.text(res_t, y_lab, f' {res_d}', rotation=90, horizontalalignment='center') dfc = pd.DataFrame.from_records( sorted(citystats), columns=['Rt', 'T2', 'C/100k', 'C/wk', 'Pop/k', 'Gemeente']) dfc.set_index('Gemeente', inplace=True) print(dfc) lab_x = df1.index[-1] + pd.Timedelta('1.2 d') add_labels(ax, labels, lab_x) ax.grid(which='both') if use_r7: ax.axvline(df1.index[-4], color='gray') # ax.text(df1.index[-4], 0.3, '3 dagen geleden - extrapolatie', rotation=90) ax.set_title( '7-daags voortschrijdend gemiddelde; laatste 3 dagen zijn een schatting' ) ax.set_ylabel('Nieuwe gevallen per 100k per dag') #ax.set_ylim(0.05, None) ax.set_xlim(None, df1.index[-1] + pd.Timedelta('1 d')) from matplotlib.ticker import LogFormatter, FormatStrFormatter ax.yaxis.set_major_formatter(FormatStrFormatter('%g')) # Monkey-patch to prevent '%e' formatting. LogFormatter._num_to_string = lambda _0, x, _1, _2: ('%g' % x) ax.yaxis.set_minor_formatter(LogFormatter(minor_thresholds=(3, 1))) #plt.xticks(pd.to_dateTime(['2020-0{i}-01' for i in range(1, 9)])) ax.legend() # loc='lower left') for tl in ax.get_xticklabels(): tl.set_ha('left') fig.canvas.set_window_title(f'Case trends (ndays={ndays})') fig.show()
# flip longitude to the astro convention image = plt.pcolormesh(longitude[::-1], latitude, submap[grid_pix], vmin=vmin, vmax=vmax, rasterized=True, cmap=cmap, norm=norm) plt.title(s_title, fontsize='small') # remove tick labels ax.xaxis.set_ticklabels([]) ax.yaxis.set_ticklabels([]) # remove grid ax.xaxis.set_ticks([]) ax.yaxis.set_ticks([]) # colorbar cax = fig.add_axes([0.2, 0.08, 0.6, 0.04]) fmtr = LogFormatter(linthresh=linthresh, labelOnlyBase=False) #fmtr = ScalarFormatter() cb = fig.colorbar(image, cax=cax, #ticks=[vmin, -10, -1, -0.1, 0.1, 1, 10, vmax], orientation='horizontal', norm=norm) cb.ax.xaxis.set_label_text(unit) #cb.ax.xaxis.labelpad = -8 # workaround for issue with viewers, see colorbar docstring cb.solids.set_edgecolor("face") plt.subplots_adjust(left=0, right=1, top=.9, wspace=.1, hspace=.01, bottom=.14) plt.savefig(_plot_dir / "example_multifreq_obs_placeholder.pdf", bbox_inches='tight', pad_inches=0.02)
def __init__( self, model_name=None, centerxy=np.array([]), blocks=np.array([]), values=np.array([]), values_log=0, bck=1, ): """ :param model_name: path to the file to be created containing the output :param centerxy: x-y coordinates of the center of the cells :param blocks: coordinates of the corners of the different blocks :param values: array containing the value assigned to each block :param values_log: flag indicating if values should be log transformed or not :param bck: background value """ if centerxy.any( ): # If block center coordinates are provided, plot them. xs = centerxy[:, 0] ys = centerxy[:, 1] fig, ax = plt.subplots() pts = ax.scatter(xs, ys, c="black", alpha=0.5) else: # Else, if only blocks are provided, the centers are computed based on the mean of the coordinates of # their corners try: centerxy = np.array([np.mean(b, axis=0) for b in blocks]) # Mean computed xs = centerxy[:, 0] ys = centerxy[:, 1] fig, ax = plt.subplots() pts = ax.scatter(xs, ys, c="black", alpha=0.5) except Exception as e: print(e) exit() # I use this option to put the points in first plan, as to always see them. pts.set_zorder(2) self.points = centerxy self.bck = bck self.model_name = model_name self.ax = ax # Axes object self.blocks = blocks self.ax.set_title("Now setting value: 0.0") # Initial title self.ax.set_facecolor((0.86, 0.86, 0.86)) # Set gray background # Adjusts plt dimensions to insert colorbar, textbox... plt.subplots_adjust(bottom=0.2, right=0.8) self.vals = ( [] ) # List that will contain the values assigned to the different polygons self.cmap = cm.get_cmap("jet") # Color map function to be later used # It is necessary to define a different axes for the box (normalized) axbox = plt.axes([0.4, 0.07, 0.1, 0.07]) # 4 - tuple of floats # rect = [left, bottom, width, height]. A new axes is added # with dimensions rect in normalized (0, 1) units using ~.Figure.add_axes on the current figure. # Location of colorbar for the user-defined polygons self.axcb = plt.axes([0.82, 0.07, 0.015, 0.83]) bounds = [0] + [1] # Adding one bound more to have enough intervals ticks = [0] # Ticks - default value cols = colors.ListedColormap([self.cmap(v) for v in ticks]) cbnorm = colors.BoundaryNorm(bounds, cols.N) mcb = colorbar.ColorbarBase( self.axcb, cmap=cols, norm=cbnorm, boundaries=bounds, ticks=ticks, ticklocation="right", orientation="vertical", ) # # textstr = """Select points in the figure by enclosing them within a polygon. # Press the 'esc' key to start a new polygon. # Try holding the 'shift' key to move all of the vertices. # Try holding the 'ctrl' key to move a single vertex.""" # # #axtxt = plt.axes([0.15, 0.0, 0.2, 0.15]) # props = dict(boxstyle='round', facecolor='green', alpha=0.5) # ax.text(0, -0.2, textstr, transform=ax.transAxes, fontsize=10, bbox=props) # Text box to input the value to input self.vinput = TextBox(axbox, label=None, initial="0") # What happens when pressing enter self.vinput.on_submit(self.button_submit) self.index = [] # List that will contain the final results ! # Creates a canvas from the ax of the scatter plot self.canvas = self.ax.figure.canvas self.collection = pts # 'collection' is the scatter plot # Necessary for later to define if points enclosed by polygon - basically self.xys = pts.get_offsets() # equals to the - x-y coordinates of the different points. self.Npts = len(self.xys) # Number of points # Ensure that we have separate colors for each object self.fc = pts.get_facecolors() # Gets the rgb of the points if not values.any(): facecolors = "gray" alpha = 0.35 # Opacity of the polygons - if no values assigned - soft gray else: cmap2 = cm.get_cmap("coolwarm") if values_log: # Making a nice linear space itv = 10**np.linspace(np.log10(min(values)), np.log10(max(values)), 12) # out ouf log values to represent some ticks on the colorbar norm2 = colors.LogNorm( vmin=min(values), vmax=max(values)) # Log norm for color bar # Necessary arg to produce the color scale formatter = LogFormatter(10, labelOnlyBase=False) else: itv = np.linspace(min(values), max(values), 8) # Linear space norm2 = colors.Normalize(vmin=min(values), vmax=max(values)) formatter = None # Individual color of each polygon facecolors = [cmap2(norm2(v)) for v in values] alpha = 0.6 # Opacity of the polygons # Colorbar if initial values present - plotting a nice color bar ticks2 = [round(v, 1) for v in itv] plt.subplots_adjust(left=0.2, bottom=0.2, right=0.8) axcb1 = plt.axes([0.15, 0.07, 0.015, 0.83]) cb1 = colorbar.ColorbarBase( axcb1, cmap=cmap2, norm=norm2, ticks=ticks2, boundaries=None, ticklocation="left", format=formatter, orientation="vertical", ) cb1.set_ticklabels(ticks2) # Setting the proper labels if ( self.blocks.any() ): # If blocks are provided. I should change this as the direction this code is going is # to provide blocks by default xs = self.blocks[:, :, 0] # x-coordinates blocks corners ys = self.blocks[:, :, 1] # y-coordinates blocks corners patches = [ ] # Coloring the blocks, in gray or with different colors for b in blocks: polygon = Polygon(b, closed=True) patches.append(polygon) p = PatchCollection(patches, alpha=alpha, facecolors=facecolors, edgecolors="black") p.set_zorder(0) self.ax.add_collection(p) # 5% padding in x-direction for visualization padx = (xs.max() - xs.min()) * 0.05 pady = (ys.max() - ys.min()) * 0.05 # 5% padding self.ax.set_xlim(xs.min() - padx, xs.max() + padx) self.ax.set_ylim(ys.min() - pady, ys.max() + pady) self.collection.set_facecolors(facecolors) # Coloring the points # Polygon selector object self.poly = PolygonSelector(self.ax, self.onselect) self.ind = [] # Initiates the ind list for a new polygon! def handle_close(evt): # Function that disconnects the PolygonSelector self.disconnect() # When closing window, finishes the job self.canvas.mpl_connect("close_event", handle_close) # Final results, array filled with background value. self.final_results = np.ones(len(self.points)) * self.bck
def plotHeatmap(ma, chrBinBoundaries, fig, position, args, cmap, xlabel=None, ylabel=None, start_pos=None, start_pos2=None, pNorm=None, pAxis=None, pPca=None): log.debug("plotting heatmap") if ma.shape[0] < 5: log.info("Matrix for {} too small to plot. Matrix size: {}".format( chrBinBoundaries.keys()[0], ma.shape)) return if pAxis is not None: axHeat2 = pAxis else: axHeat2 = fig.add_axes(position) if args.title: axHeat2.set_title(toString(args.title)) if start_pos is None: start_pos = np.arange(ma.shape[0]) if start_pos2 is None: start_pos2 = start_pos xmesh, ymesh = np.meshgrid(start_pos, start_pos2) img3 = axHeat2.pcolormesh(xmesh.T, ymesh.T, ma, vmin=args.vMin, vmax=args.vMax, cmap=cmap, norm=pNorm) axHeat2.invert_yaxis() img3.set_rasterized(True) xticks = None if args.region: xtick_lables = relabel_ticks(axHeat2.get_xticks()) axHeat2.get_xaxis().set_tick_params(which='both', bottom='on', direction='out') axHeat2.set_xticklabels(xtick_lables, size='small', rotation=45) ytick_lables = relabel_ticks(axHeat2.get_yticks()) axHeat2.get_yaxis().set_tick_params(which='both', bottom='on', direction='out') axHeat2.set_yticklabels(ytick_lables, size='small') xticks = [xtick_lables] """ axHeat2.set_xticks([0, ma.shape[0]]) axHeat2.set_xticklabels([args.region[1], args.region[2]], size=4, rotation=90) axHeat2.set_axis_off() """ else: ticks = [ int(pos[0] + (pos[1] - pos[0]) / 2) for pos in itervalues(chrBinBoundaries) ] labels = list(chrBinBoundaries) axHeat2.set_xticks(ticks) axHeat2.set_yticks(ticks) labels = toString(labels) xticks = [labels, ticks] if len(labels) > 20: axHeat2.set_xticklabels(labels, size=4, rotation=90) axHeat2.set_yticklabels(labels, size=4) else: axHeat2.set_xticklabels(labels, size=8) axHeat2.set_yticklabels(labels, size=8) if pPca is None: divider = make_axes_locatable(axHeat2) cax = divider.append_axes("right", size="2.5%", pad=0.09) else: cax = pPca['axis_colorbar'] if args.log1p: from matplotlib.ticker import LogFormatter formatter = LogFormatter(10, labelOnlyBase=False) # get a useful log scale # that looks like [1, 2, 5, 10, 20, 50, 100, ... etc] aa = np.array([1, 2, 5]) tick_values = np.concatenate([aa * 10**x for x in range(10)]) cbar = fig.colorbar(img3, ticks=tick_values, format=formatter, cax=cax) else: cbar = fig.colorbar(img3, cax=cax) cbar.solids.set_edgecolor( "face") # to avoid white lines in the color bar in pdf plots if args.scoreName: cbar.ax.set_ylabel(args.scoreName, rotation=270, size=8) if ylabel is not None: ylabel = toString(ylabel) axHeat2.set_ylabel(ylabel) if xlabel is not None: xlabel = toString(xlabel) axHeat2.set_xlabel(xlabel) if pPca: axHeat2.xaxis.set_label_position("top") axHeat2.xaxis.tick_top() if args.region: plotEigenvector(pPca['axis'], pPca['args'].pca, pRegion=pPca['args'].region, pXticks=xticks) else: plotEigenvector(pPca['axis'], pPca['args'].pca, pXticks=xticks, pChromosomeList=labels)
def plot_panel_1x3_seperate_colorbar(plot_items, column_titles): """"Plot panel with 3 columns of individual plots using solely seperate plot properties. Args: plot_items (list of dict): Individual properties of the plots. column_titles (list): Plot titles per column. """ # Set up figure, calculate figure height corresponding to desired width. plot_frame_top, plot_frame_bottom, plot_frame_left, plot_frame_right = .92, 0.17, 0., 1. width_colorbar = .27 bottom_pos_colorbar = .1 fig_width = 9. * (0.88 - .035) if column_titles is None: plot_frame_top = 1. column_titles = [None] * 3 plot_frame_width = plot_frame_right - plot_frame_left fig_height = calc_fig_height(fig_width, (1, 3), plot_frame_top, plot_frame_bottom, plot_frame_left, plot_frame_right) fig, axs = plt.subplots(1, 3, figsize=(fig_width, fig_height), dpi=150) fig.subplots_adjust(top=plot_frame_top, bottom=plot_frame_bottom, left=plot_frame_left, right=plot_frame_right, hspace=0.0, wspace=0.0) # Plot the data. for i, (ax, title, plot_item) in enumerate(zip(axs, column_titles, plot_items)): # Mapping individual properties of the plots. z = plot_item['data'] cf_lvls = plot_item['contour_fill_levels'] cl_lvls = plot_item['contour_line_levels'] cb_ticks = plot_item['colorbar_ticks'] cb_tick_fmt = plot_item['colorbar_tick_fmt'] apply_log_scale = plot_item.get('log_scale', False) extend = plot_item.get('extend', "neither") cl_label_fmt = plot_item.get('contour_line_label_fmt', None) if cl_label_fmt is None: cl_label_fmt = cb_tick_fmt.replace("{:", "%").replace("}", "") plt.axes(ax) plt.title(title) contour_fills = individual_plot(z, cf_lvls, cl_lvls, cline_label_format=cl_label_fmt, log_scale=apply_log_scale, extend=extend) # Add axis for colorbar. left_pos_colorbar = plot_frame_width / 3 * i + ( plot_frame_width / 3 - width_colorbar) / 2 + plot_frame_left cbar_ax = fig.add_axes( [left_pos_colorbar, bottom_pos_colorbar, width_colorbar, 0.035]) if apply_log_scale: formatter = LogFormatter(10, labelOnlyBase=False) else: formatter = None cbar = plt.colorbar(contour_fills, orientation="horizontal", cax=cbar_ax, ticks=cb_ticks, format=formatter) cbar.ax.set_xticklabels([cb_tick_fmt.format(t) for t in cb_ticks]) cbar.set_label(plot_item['colorbar_label'])
def __init__(self, base, sign, zero): LogFormatter.__init__ (self, base) self.sign = sign self.zero = zero
def plot_glm_grid(fig, glm_grids, tidx, fields, subplots=(2, 3), axes_facecolor=(0., 0., 0.), map_color=(.8, .8, .8)): fig.clf() glmx = glm_grids.x.data[:] glmy = glm_grids.y.data[:] proj_var = glm_grids['goes_imager_projection'] x = glmx * proj_var.perspective_point_height y = glmy * proj_var.perspective_point_height glm_xlim = x.min(), x.max() glm_ylim = y.min(), y.max() country_boundaries = cfeature.NaturalEarthFeature(category='cultural', name='admin_0_countries', scale='50m', facecolor='none') state_boundaries = cfeature.NaturalEarthFeature( category='cultural', name='admin_1_states_provinces_lakes', scale='50m', facecolor='none') globe = ccrs.Globe(semimajor_axis=proj_var.semi_major_axis, semiminor_axis=proj_var.semi_minor_axis) proj = ccrs.Geostationary( central_longitude=proj_var.longitude_of_projection_origin, satellite_height=proj_var.perspective_point_height, globe=globe) cbars = [] for fi, f in enumerate(fields): glm_norm = display_params[f]['glm_norm'] product_label = display_params[f]['product_label'] file_tag = display_params[f]['file_tag'] max_format = display_params[f]['format_string'] ax = fig.add_subplot(subplots[0], subplots[1], fi + 1, projection=proj) ax.background_patch.set_facecolor(axes_facecolor) # ax.set_aspect('auto', adjustable=None) ax.coastlines('10m', color=map_color) ax.add_feature(state_boundaries, edgecolor=map_color, linewidth=0.5) ax.add_feature(country_boundaries, edgecolor=map_color, linewidth=1.0) glm_sel = glm_grids[f].sel(time=tidx) if hasattr(glm_sel, 'compute'): # glm is a dask array, and needs to be computed before using some of the # format string operations, below. glm = glm_sel.compute().data.copy() # when doing the np.isnan check below, using dask givs a # weird array is read-only error without the "copy" glm = glm.copy() else: glm = glm_sel.data glm[np.isnan(glm)] = 0 # Use a masked array instead of messing with colormap to get transparency # glm = np.ma.array(glm, mask=(np.isnan(glm))) # glm_alpha = .5 + glm_norm(glm)*0.5 glm_img = ax.imshow( glm, extent=(x.min(), x.max(), y.min(), y.max()), origin='upper', # transform = ccrs.PlateCarree(), cmap=glm_cmap, interpolation='nearest', norm=glm_norm) #, alpha=0.8) # Match the GLM grid limits, in fixed grid space ax.set_xlim(glm_xlim) ax.set_ylim(glm_ylim) # Set a lat/lon box directly # ax.set_extent([-103, -99.5, 32.0, 38.0]) # limits = ax.axis() limits = [0., 1., 0., 1.] glm_field_max = max_format.format(glm.max()) ax.text(limits[0] + .02 * (limits[1] - limits[0]), limits[2] + .02 * (limits[3] - limits[2]), tidx.isoformat().replace('T', ' ') + ' UTC' + label_string.format(glm_field_max, product_label), transform=ax.transAxes, fontsize=10, color=map_color) ax.text(limits[0] + .02 * (limits[1] - limits[0]), limits[3] - .1 * (limits[3] - limits[2]), panel_labels[fi], fontweight='bold', fontsize=10, transform=ax.transAxes, color=map_color) cbars.append((ax, glm_img)) # Make the colorbar position match the height of the Cartopy axes # Have to draw to force layout so that the ax position is correct fig.tight_layout() fig.canvas.draw() cbar_obj = [] for ax, glm_img in cbars: posn = ax.get_position() # internal_left = [posn.x0 + posn.width*.87, posn.y0+posn.height*.05, # 0.05, posn.height*.90] height_scale = .025 * subplots[0] top_edge = [ posn.x0, posn.y0 + posn.height * (1.0 - height_scale), posn.width, posn.height * height_scale ] cbar_ax = fig.add_axes(top_edge) cbar = plt.colorbar( glm_img, orientation='horizontal', cax=cbar_ax, #aspect=50, format=LogFormatter(base=2), ticks=LogLocator(base=2)) cbar.outline.set_edgecolor(axes_facecolor) ax.outline_patch.set_edgecolor(axes_facecolor) cbar.ax.tick_params(direction='in', color=axes_facecolor, which='both', pad=-14, labelsize=10, labelcolor=axes_facecolor) cbar_obj.append(cbar) mapax = set_shared_geoaxes(fig) return mapax, cbar_obj
def create_pngs(target_directory, param='chlor_a'): cwd = os.getcwd() os.chdir(os.path.join(target_directory, 'OC_maps')) all_hdf_files = sorted(glob('*.nc')) try: for i, f in enumerate(all_hdf_files): try: dataset = xr.open_dataset(f) maxvalue = np.nanmax(dataset.chlor_a.data) minvalue = np.nanmin(dataset.chlor_a.data) sst_array = np.ma.masked_where( np.isnan(dataset.chlor_a.data), dataset.chlor_a.data) lon = { 'min': dataset.geospatial_lon_min, 'max': dataset.geospatial_lon_max, 'dims': dataset.dims['lon'], } lat = { 'min': dataset.geospatial_lat_min, 'max': dataset.geospatial_lat_max, 'dims': dataset.dims['lat'], } fig = plt.figure() lonv = np.linspace(lon['min'], lon['max'], lon['dims']) latv = np.linspace(lat['max'], lat['min'], lat['dims']) lons, lats = np.meshgrid(lonv, latv) map = Basemap( llcrnrlon=lon['min'], llcrnrlat=lat['min'], urcrnrlon=lon['max'], urcrnrlat=lat['max'], resolution='i', projection='merc') map.drawcoastlines() map.drawcountries() if param == 'sst': ax = map.pcolormesh( lons, lats, sst_array, vmin=minvalue, vmax=maxvalue, latlon=True, ) map.colorbar( ax, location="right", size="5%", pad='2%', label='SST [°C]') elif param == 'chlor_a': ax = map.pcolormesh( lons, lats, sst_array, norm=LogNorm(), vmin=0.001, vmax=0.63, latlon=True, ) formatter = LogFormatter(10, labelOnlyBase=True) map.colorbar( ax, location="right", size="5%", pad='2%', label='CHL [mg/m3]', ticks=[0.02, 0.1, 0.25, 0.4, 0.63], format=formatter) print(f, dataset.time_coverage_start.split('T')[0], dataset.time_coverage_end.split('T')[0], dataset.time_coverage_start.split('T')[0] == dataset.time_coverage_end.split('T')[0]) plot_date = dataset.time_coverage_end.split('T')[0] plt.title(f"MODIS Aqua - {param.upper()} - {plot_date}") plt.savefig( dataset.product_name.replace('.nc', '.png'), bbox_inches='tight') plt.close(fig) except Exception as e: print(f'Could not plot {f} due to {e}') finally: os.chdir(cwd)
def h2stack1d(self, boxify=False, cmap=mpl.cm.jet, colorbar=True, cumdir=1, **kwargs): histos = [] for i in range(self.nbins[1]): histos.append( d.factory.hist1d(self.bincenters[0], self.binedges[0], weights=self.bincontent[:, i])) if cumdir >= 0: histocum = [ reduce(lambda i, j: i + j, histos[:end]) for end in range(1, len(histos) + 1) ] it = reversed(list(enumerate(zip(histocum, self.bincenters[1])))) else: histocum = [ reduce(lambda i, j: i + j, histos[begin:]) for begin in range(0, len(histos) - 1) ] it = iter(list(enumerate(zip(histocum, self.bincenters[1])))) log = might_be_logspaced(self.binedges[1]) if log: trafo = mpl.colors.LogNorm(vmin=self.bincenters[1][0], vmax=self.bincenters[1][-1]) else: trafo = mpl.colors.Normalize(vmin=self.bincenters[1][0], vmax=self.bincenters[1][-1]) for i, (h, cvalue) in it: if boxify: mask = histocum[-1]._h_bincontent > 0 hscale = h.empty_like() hscale._h_bincontent[mask] = (h._h_bincontent / histocum[-1]._h_bincontent)[mask] hscale.line(filled=1, fc=cmap(trafo(cvalue)), **kwargs) else: h.line(filled=1, fc=cmap(trafo(cvalue)), **kwargs) p.xlim(self.binedges[0][0], self.binedges[0][-1]) if boxify: p.ylim(0, 1.05) cbargs = dict(cmap=cmap, norm=trafo, orientation='vertical') if log: # logspaced colorbars need a little hand-holding from matplotlib.ticker import LogFormatter cbargs['ticks'] = self.binedges[1] cbargs['format'] = LogFormatter() if colorbar: ax = p.gca() cax, cax_kw = mpl.colorbar.make_axes(ax) cax.hold(True) cb1 = mpl.colorbar.ColorbarBase(cax, **cbargs) p.gcf().sca(ax) if self.labels[0] is not None: ax.set_xlabel(self.labels[0]) if self.labels[1] is not None: cb1.set_label(self.labels[1]) else: return cbargs
formatter.scaled[1/(24.*60.)] = '%H:%M:%S' # only show min and sec ax.xaxis.set_major_formatter(formatter) ax.set_xlim([tmin, tmax]) ax.set_ylim([fmin, fmax]) ax.set_yscale('log') num_major_ticks = 10; num_minor_ticks_per_major = 5 majorLocator = MultipleLocator((fmax - fmin)/num_major_ticks) majorFormatter = FormatStrFormatter('%4.2f') minorLocator = MultipleLocator((fmax - fmin)/(num_major_ticks*num_minor_ticks_per_major)) #set linear ticks if we are less than a decade if log10(fmax) - log10(fmin) >= .9: majorLocator = LogLocator(base=10.0, subs=[1.0], numdecs=4, numticks=5) majorFormatter = LogFormatter(base=10, labelOnlyBase=True) minorLocator = LogLocator(base=10.0, subs=[1.0], numdecs=4, numticks=5) ax.yaxis.set_major_locator(majorLocator) ax.yaxis.set_major_formatter(majorFormatter) ax.yaxis.set_minor_locator(minorLocator); ax.get_yaxis().set_tick_params(which='both', direction='out') fig.autofmt_xdate() #set image size and save it as a PNG fig.set_size_inches(18.5,10.5) savefig(output_fname + ".png", format="png", dpi=200, transparent=True, bbox_inches='tight')
# vmin=0,vmax=.6) title=['(a) Total Currents','(b) Ekman Currents','(c) Geostrophic Currents','(d) Stokes Drift'] plt.title(title[typ],fontsize=14,fontweight='bold') return size fig,axes=plt.subplots(nrows=2, ncols=2,figsize=(10*2,8*1.5)) plt.subplot(2,2,1) size=plotDensity(0,lonTot,latTot,uTot[0,:,:],vTot[0,:,:],magTot[0,:,:]) plt.subplot(2,2,2) plotDensity(1,lonEk,latEk,uEk[0,:,:],vEk[0,:,:],magEk[0,:,:]) plt.subplot(2,2,3) plotDensity(2,lonGeo,latGeo,uGeo[0,:,:],vGeo[0,:,:],magGeo[0,:,:]) plt.subplot(2,2,4) plotDensity(3,lonS,latS,uS,vS,magS) fig.subplots_adjust(right=0.9) cbar_ax = fig.add_axes([0.93, 0.14, 0.02, 0.715]) formatter = LogFormatter(10, labelOnlyBase=True) cbar=fig.colorbar(size,cax=cbar_ax) cbar.ax.tick_params(labelsize=12) labels=['<0.01','0.1','1<'] #labelsM=["%.2f" % z for z in labels] actualLabels=[] k=0 for i in range(20): if i%9==0: actualLabels.append(labels[k]) k+=1 else: actualLabels.append('') cbar.ax.set_yticklabels(actualLabels) cbar.set_label("Mean Current Velocity (m s$^{-1}$)", rotation=90,fontsize=13) plt.subplots_adjust(wspace=0.1)
def save_im_falsecolor(im_out_p, im_out_t, pano_path, vmax, savePath, panorama_w, panorama_h, fisheye_im_dimension): # setup the figure fig = plt.figure(figsize=(30, 10)) plt.suptitle("DGP and Falsecolor Analysis") plt.axis("off") DGPs_p = [] DGPs_t = [] DGPs_mean = [] DGPs_max = [] angle_step = 36 if (os.path.exists("results/hdrs_t/") == False): os.mkdir("results/hdrs_t/") if (os.path.exists("results/hdrs_p/") == False): os.mkdir("results/hdrs_p/") for view_angle in range(-180, 180): if view_angle % angle_step == 0: DGP_t, im_t = calc_DGP_im(im_out_t, view_angle, panorama_w, panorama_h, fisheye_im_dimension, "results/hdrs_t/" + pano_path + "_" + str(view_angle)) DGPs_t.append(DGP_t) DGP_p, im_p = calc_DGP_im(im_out_p, view_angle, panorama_w, panorama_h, fisheye_im_dimension, "results/hdrs_p/" + pano_path + "_" + str(view_angle)) DGPs_p.append(DGP_p) dif = abs(im_t - im_p) DGP_dif = abs(DGP_t - DGP_p) im_t = np.clip(im_t, 1, vmax) im_p = np.clip(im_p, 1, vmax) dif = np.clip(dif, 1, vmax) colormap = COLORMAP fig.add_subplot(3, 10, view_angle / angle_step + 180 / angle_step + 1) implot1 = plt.imshow(im_t, cmap=colormap, norm=LogNorm(vmin=1, vmax=vmax)) plt.title("DGP_truth: %.2f" % (DGP_t)) plt.axis("off") fig.add_subplot(3, 10, view_angle / angle_step + 180 / angle_step + 1 + 10) implot2 = plt.imshow(im_p, cmap=colormap, norm=LogNorm(vmin=1, vmax=vmax)) plt.title("DGP_prediction: %.2f" % (DGP_p)) plt.axis("off") fig.add_subplot(3, 10, view_angle / angle_step + 180 / angle_step + 1 + 20) implot3 = plt.imshow(dif, cmap=colormap, norm=LogNorm(vmin=1, vmax=vmax)) if DGP_dif > 0.1 and DGP_dif < 0.2: plt.title("DGP_difference: %.2f" % (DGP_dif), color="red") elif DGP_dif > 0.2: plt.title("DGP_difference: %.2f" % (DGP_dif), color="green") else: plt.title("DGP_difference: %.2f" % (DGP_dif)) plt.axis("off") time_name = pano_path plt.figtext(.5, .93, (time_name, "DGP truth max: %.2f, prediction max: %.2f" % ( np.array(DGPs_t).max(), np.array(DGPs_p).max()), "DGP min: %.2f, prediction min: %.2f" % (np.array(DGPs_t).min(), np.array(DGPs_p).min()), "DGP mean: %.2f, prediction mean: %.2f" % (np.array(DGPs_t).mean(), np.array(DGPs_p).mean()), "DGP MSE %.5f" % (np.mean((np.array(DGPs_t) - np.array(DGPs_p)) ** 2))), fontsize=12, ha='center') # add color index bar cbaxes = fig.add_axes([0.92, 0.1, 0.03, 0.8]) if (vmax == 3000): formatter = LogFormatter(3, labelOnlyBase=False) cb = plt.colorbar(ticks=[1, 4.1, 12.2, 36.5, 109, 325.8, 973.4, 3000], format=formatter, cax=cbaxes) else: formatter = LogFormatter(3.7, labelOnlyBase=False) cb = plt.colorbar(ticks=[1, 3.7, 14, 55, 204, 755, 2794, 10000], format=formatter, cax=cbaxes) cb.set_label('Luminance (cd/m2)', rotation=90) print(os.path.join(savePath, time_name + ".png")) # save the image plt.savefig(os.path.join(savePath, time_name + ".png")) plt.close() plotDGPAnalysis(DGPs_t, DGPs_p, time_name) DGPs_mean.append(np.array(DGPs_t).mean()) DGPs_max.append(np.array(DGPs_t).max()) return DGPs_p, DGPs_t, DGPs_mean, DGPs_max
def plot_daily_trends(ndays=100, lastday=-1, mun_regexp=None, region_list=None, source='r7', subtitle=None): """Plot daily-case trends (pull data from global DFS dict). - lastday: up to this day. - source: 'r7' (7-day rolling average), 'raw' (no smoothing), 'sg' (Savitsky-Golay smoothed). - mun_regexp: regular expression matching municipalities. - region_list: list of municipalities (including e.g. 'HR:Zuid', 'POP:100-200', 'JSON:{...}'. if mun_regexp and mun_list are both specified, then concatenate. If neither are specified, assume 'Nederland'. JSON is a json-encoded dict with: - 'label': short label string - 'color': for plotting, optional. - 'fmt': format for plotting, e.g. 'o--', optional. - 'muns': list of municipality names - subtitle: second title line (optional) """ df_restrictions = DFS['restrictions'] df_mun = DFS['mun'] fig, ax = plt.subplots(figsize=(12, 6)) fig.subplots_adjust(top=0.945 - 0.03 * (subtitle is not None), bottom=0.1, left=0.09, right=0.83) if region_list is None: region_list = [] if mun_regexp: region_list = [m for m in df_mun.index if re.match(mun_regexp, m) ] + region_list if region_list == []: region_list = ['Nederland'] labels = [] # tuples (y, txt)f citystats = [] # tuples (Rt, T2, cp100k, cwk, popk, city_name) for region in region_list: df1, n_inw = get_region_data(region, lastday=lastday) df1 = df1.iloc[-ndays:] fmt = 'o-' if ndays < 70 else '-' psize = 5 if ndays < 30 else 3 dnc_column = dict(r7='Delta7r', raw='Delta', sg='DeltaSG')[source] if region.startswith('JSON:'): reg_dict = json.loads(region[5:]) reg_label = reg_dict['label'] if 'fmt' in reg_dict: fmt = reg_dict['fmt'] color = reg_dict['color'] if 'color' in reg_dict else None else: reg_label = re.sub(r'POP:(.*)-(.*)', r'\1k-\2k inw.', region) reg_label = re.sub(r'^[A-Z]+:', '', reg_label) color = None ax.semilogy(df1[dnc_column] * 1e5, fmt, color=color, label=reg_label, markersize=psize) delta_t = 7 i0 = dict(raw=-1, r7=-3, sg=-3)[source] t_double, Rt = get_t2_Rt(df1[dnc_column], delta_t, i0=i0) citystats.append( (np.around(Rt, 2), np.around(t_double, 2), np.around(df1['Delta'][-1] * 1e5, 2), int(df1['Delta7r'][-4] * n_inw * 7 + 0.5), int(n_inw / 1e3 + .5), reg_label)) if abs(t_double) > 60: texp = f'Stabiel' elif t_double > 0: texp = f'×2: {t_double:.3g} d' elif t_double < 0: texp = f'×½: {-t_double:.2g} d' ax.semilogy(df1.index[[i0 - delta_t, i0]], df1[dnc_column].iloc[[i0 - delta_t, i0]] * 1e5, 'k--', zorder=-10) labels.append((df1[dnc_column][-1] * 1e5, f' {reg_label} ({texp})')) _add_restriction_labels(ax, df1.index[0], df1.index[-1], with_ribbons=False) dfc = pd.DataFrame.from_records( sorted(citystats), columns=['Rt', 'T2', 'C/100k', 'C/wk', 'Pop/k', 'Region']) dfc.set_index('Region', inplace=True) print(dfc) lab_x = df1.index[-1] + pd.Timedelta('1.2 d') add_labels(ax, labels, lab_x) if source == 'r7': ax.axvline(df1.index[-4], color='gray') # ax.text(df1.index[-4], 0.3, '3 dagen geleden - extrapolatie', rotation=90) title = '7-daags voortschrijdend gemiddelde; laatste 3 dagen zijn een schatting' elif source == 'sg': ax.axvline(df1.index[-8], color='gray') # ax.text(df1.index[-4], 0.3, '3 dagen geleden - extrapolatie', rotation=90) title = 'Gefilterde data; laatste 7 dagen zijn minder nauwkeurig' else: title = 'Dagcijfers' ax.set_ylabel('Nieuwe gevallen per 100k per dag') #ax.set_ylim(0.05, None) ax.set_xlim(None, df1.index[-1] + pd.Timedelta('1 d')) from matplotlib.ticker import LogFormatter, FormatStrFormatter ax.yaxis.set_major_formatter(FormatStrFormatter('%g')) # Monkey-patch to prevent '%e' formatting. LogFormatter._num_to_string = lambda _0, x, _1, _2: ('%g' % x) ax.yaxis.set_minor_formatter(LogFormatter(minor_thresholds=(3, 1))) #plt.xticks(pd.to_dateTime(['2020-0{i}-01' for i in range(1, 9)])) ax.legend() # loc='lower left') tools.set_xaxis_dateformat(ax, yminor=True) if subtitle: title += f'\n{subtitle}' win_xtitle = f', {subtitle}' else: win_xtitle = '' ax.set_title(title) fig.canvas.set_window_title(f'Case trends (ndays={ndays}){win_xtitle}') fig.show()
def make_plot(xval, yval, plot_type='d', y_axis_type='normal', \ legend_label=None, name=None, plot_title=None, ylabel=None, \ xlabel=None, xticks=None, yticks=None, figure_size=(16,10), \ freq_cutoff=None, publish=False): """ Takes input from sparam_data and plots calculated permittivity. Can handle multiple data sets. Plots uncertainty countour if plotting single dataset with uncertainty present and plots error bars every 25 points when plotting multiple datasets. Arguments --------- xval : array or list x-axis data. Multiple data sets must be in a list. yval : array or list y-axis data. Multiple data sets must be in a list. plot_type : str Flag for default plot types. Can be set to 'd' for the real part of epsilon, 'lf' for the imaginary part of epsilon, 'lt' for dielectric loss tangent, 'ur' for the real part of mu, 'ui' for the imaginary part of mu, or 'c' for Custom. If 'c' is used, xticks and yticks must be provided. y_axis_type : str, optional Flag for type of axis to use for the y-axis. Can be either 'normal' (default) or 'log' for a log axis. If set to log, y tick postions must be manually provided to yticks. legend_label : list of str, optional Plot legend label. Each dataset much have it's own label. Stings must be in a list. name : str, optional Required when publish=True. Used in file name of saved figure. plot_title : str, optional For use when plot_type='c'. Title of the plot. ylabel : str, optional For use when plot_type='c'. Y-axis label. xlabel : str, optional For use when plot_type='c'. X-axis label. xticks : list, optional Manually set x-axis tick locations. Required when plot_type='c'. yticks : list, optional Manually set y-axis tick locations. Required when plot_type='c' and when y_axis_type='log'. figure_size : tuple or int, optional Set the matplotlib figsize. Default: (16,10). freq_cutoff : float, optional Data points lower than freq_cutoff will not be plotted. publish : bool, optional If True save figure as .eps file. Default: False. """ # Default settings for plotting permittivity data if plot_type == 'd': # Real part plot_title = 'Real Part of the Permittivity' ylabel = r'$\epsilon^{\prime}_{r}$' xlabel = 'Frequency' rnd = 1 # decimals to round to for axes determination elif plot_type == 'lf': # Imaginary part plot_title = 'Imaginary Part of the Permittivity' ylabel = r'$\epsilon^{\prime\prime}_{r}$' xlabel = 'Frequency' rnd = 2 elif plot_type == 'lt': # Loss tan plot_title = 'Loss Tangent' ylabel = r'$tan\delta$' xlabel = 'Frequency' rnd = 2 elif plot_type == 'ur': # Real part of mu plot_title = 'Real Part of the Permeability' ylabel = r'$\mu^{\prime}_{r}$' xlabel = 'Frequency' rnd = 2 elif plot_type == 'ui': # Imaginary part of mu plot_title = 'Imaginary Part of the Permeability' ylabel = r'$\mu^{\prime\prime}_{r}$' xlabel = 'Frequency' rnd = 2 elif plot_type == 'c': # Custom plot pass else: raise Exception('Invalid plot type') # Checks if input data is in a list and determines number of things to plot # NOTE: Multiple data sets must be in a list if isinstance(xval, list): number_to_compare = len(xval) else: number_to_compare = 1 xval = [xval] yval = [yval] # If no label is specified, make one if not legend_label: # If legend_label is None legend_label = [] # Label for first dataset is 'Data 1', next is 'Data 2', etc... for n in range(0, len(xval)): legend_label.append('Data {}'.format(n + 1)) else: # If legend_label is a list, make sure no list items are None for n in range(0, len(xval)): # If a list item is None, make it a label if not legend_label[n]: legend_label[n] = 'Data {}'.format(n + 1) # Remove all points lower than freq_cutoff x = [] y = [] for n in range(0, number_to_compare): if freq_cutoff: x.append(xval[n][xval[n] > freq_cutoff]) y.append(yval[n][xval[n] > freq_cutoff]) else: x.append(xval[n]) y.append(yval[n]) # Determine axes limits if plot_type != 'c': # skip for custom plots x_max = -np.inf x_min = np.inf y_max = -np.inf y_min = np.inf for n in range(0, number_to_compare): x_chk = unp.nominal_values(x[n]) max_tmp = round(max(x_chk[~np.isnan(x_chk)]), rnd) min_tmp = round(min(x_chk[~np.isnan(x_chk)])) if max_tmp > x_max: x_max = max_tmp if min_tmp < x_min: x_min = min_tmp y_chk = unp.nominal_values(y[n]) max_tmp = round(max(y_chk[~np.isnan(y_chk)]), rnd) min_tmp = round(min(y_chk[~np.isnan(y_chk)]), rnd) if max_tmp > y_max: y_max = max_tmp if min_tmp < y_min: y_min = min_tmp # Determine appropriate buffer and spacing depedning on plot type if not yticks: # skip if y ticks are manually provided thickness = y_max - y_min if plot_type in ('d', 'ur', 'ui'): if thickness < 0.1: buffer = 0.1 spacing = 0.02 else: buffer = 0.2 spacing = round((thickness + 2 * buffer) / 9, 1) elif plot_type in ('lf', 'lt', 'c'): if thickness < 0.01: buffer = 0.01 spacing = 0.002 else: buffer = 0.02 spacing = round((thickness + 2 * buffer) / 9, 2) # Makes sure the lowest point is 0 if y_min is 0 if y_min == 0: y_min += buffer elif y_min - buffer < 0: # Make sure buffer does not make ymin negative y_min = buffer # Plot f = plt.figure(figsize=figure_size) ax = f.add_subplot(111) # Plot labels ax.set_title(plot_title, fontsize=40) ax.set_ylabel(ylabel, fontsize=40) ax.set_xlabel(xlabel, fontsize=40) # Colours if number_to_compare > 6: # cycle through cubehelix palette if plotting more than 6 things ax.set_prop_cycle(cycler('color',\ sns.cubehelix_palette(n_colors=number_to_compare,dark=0.05,light=0.75))) else: # otherise use Dark2 palette (should be colour blind safe) ax.set_prop_cycle(cycler('color',\ sns.color_palette("Dark2",number_to_compare))) # Plot axes ax.set_xscale('log') # Set y ticks if y_axis_type == 'log': # check if y axis should be log ax.set_yscale('log') ax.set_ylim(min(yticks), max(yticks)) majorLocator_y = FixedLocator(yticks) majorFormatter_y = LogFormatter() minorLocator_y = LogLocator( subs='all' ) # Use all interger multiples of the log base for minor ticks minorFormatter_y = NullFormatter() # No minor tick labels # Apply y ticks ax.get_yaxis().set_major_locator(majorLocator_y) ax.get_yaxis().set_major_formatter(majorFormatter_y) ax.get_yaxis().set_minor_locator(minorLocator_y) ax.get_yaxis().set_minor_formatter(minorFormatter_y) elif yticks and y_axis_type == 'normal': ax.set_ylim(min(yticks), max(yticks)) ax.set_yticks(yticks) elif not yticks: # auto set ax.set_ylim(y_min - buffer, y_max + buffer) ax.set_yticks(np.arange(y_min - buffer, y_max + buffer, spacing)) # Set x ticks if xticks: x_ticklocs = xticks elif not xticks: # auto set if x_min == 0: # log of min and max x values x_logmin = 0 # don't take log of 0 else: x_logmin = np.log10(x_min) if x_max == 0: x_logmax = 0 else: x_logmax = np.log10(x_max) x_logticks = np.logspace(x_logmin, x_logmax, num=4) # 4 equaly spaced points in log space x_ticklocs = [] for n in range( len(x_logticks)): # round scientific values and make a list x_ticklocs.append(np.float(np.format_float_scientific(x_logticks[n],\ precision=0))) if len(set( x_ticklocs)) < 4: # check that this produced 4 unique values x_ticklocs = [] # if not do it again with precision = 1 for n in range(len(x_logticks)): x_ticklocs.append(np.float(np.format_float_scientific(x_logticks[n]\ ,precision=1))) majorLocator_x = FixedLocator(x_ticklocs) majorFormatter_x = EngFormatter(unit='Hz') # Format major ticks with units minorLocator_x = LogLocator( subs='all' ) # Use all interger multiples of the log base for minor ticks minorFormatter_x = NullFormatter() # No minor tick labels # Apply x ticks ax.get_xaxis().set_major_locator(majorLocator_x) ax.get_xaxis().set_major_formatter(majorFormatter_x) ax.get_xaxis().set_minor_locator(minorLocator_x) ax.get_xaxis().set_minor_formatter(minorFormatter_x) # Format the actual tick marks ax.tick_params(which='both', width=1, labelsize=30) ax.tick_params(which='major', length=7) ax.tick_params(which='minor', length=4) # Use smaller line width for minor tick grid lines ax.grid(b=True, which='major', color='w', linewidth=1.0) ax.grid(b=True, which='minor', color='w', linewidth=0.5) # Do the actual plotting if number_to_compare == 1: # plot uncertainty only if plotting one dataset ax.plot(unp.nominal_values(x[0]), unp.nominal_values(y[0]), lw=2, \ label=legend_label[0]) ax.fill_between(unp.nominal_values(x[0]), unp.nominal_values(y[0]) - \ unp.std_devs(y[0]), unp.nominal_values(y[0]) + \ unp.std_devs(y[0]), color="#3F5D7D",alpha=0.3,label='Uncertainty') else: for n in range(0, number_to_compare): ax.errorbar(unp.nominal_values(x[n]), unp.nominal_values(y[n]), \ yerr=unp.std_devs(y[n]), errorevery=25, elinewidth=1, \ capthick=1, capsize=2,lw=2,label=legend_label[n]) ax.legend(fontsize=30, loc='best') if publish: # Make file name #If save_path_for_plots already exits, use it, otherwise promt for path if 'save_path_for_plots' in globals() and not None: datapath = save_path_for_plots else: datapath = _dirprompt() # prompt for save dir savename = name.replace(' ','-') + '_' + plot_title.replace(' ','-') \ + '_' + DATE + '.pdf' filepath = os.path.join(datapath, savename) # Save figure to .pdf file plt.savefig(filepath, dpi=300, format='pdf', pad_inches=0) plt.show()
def __call__(self, v, pos=None): vv = self._base**v return LogFormatter.__call__(self, vv, pos)
def plotHeatmap_region(ma, chrBinBoundaries, fig, position, args, cmap, xlabel=None, ylabel=None, start_pos=None, start_pos2=None): axHeat2 = fig.add_axes(position) if args.title: axHeat2.set_title(args.title) norm = None if args.log1p: ma += 1 norm = LogNorm() if start_pos is None: start_pos = np.arange(ma.shape[0]) if start_pos2 is None: start_pos2 = start_pos xmesh, ymesh = np.meshgrid(start_pos, start_pos2) img3 = axHeat2.pcolormesh(xmesh.T, ymesh.T, ma, vmin=args.vMin, vmax=args.vMax, cmap=cmap, norm=norm) img3.set_rasterized(True) if args.region: # relabel xticks def relabel_ticks(ticks): if ticks[-1] - ticks[0] > 100000: labels = ["{:.2f} ".format((x / 1e6)) for x in ticks] labels[-1] += "Mbp" else: labels = ["{:,} ".format((x)) for x in ticks] labels[-1] += "bp" return labels xtick_lables = relabel_ticks(axHeat2.get_xticks()) axHeat2.get_xaxis().set_tick_params(which='both', bottom='on', direction='out') axHeat2.set_xticklabels(xtick_lables, size='small', rotation=45) ytick_lables = relabel_ticks(axHeat2.get_yticks()) axHeat2.get_yaxis().set_tick_params(which='both', bottom='on', direction='out') axHeat2.set_yticklabels(ytick_lables, size='small') """ axHeat2.set_xticks([0, ma.shape[0]]) axHeat2.set_xticklabels([args.region[1], args.region[2]], size=4, rotation=90) axHeat2.set_axis_off() """ else: ticks = [ int(pos[0] + (pos[1] - pos[0]) / 2) for pos in chrBinBoundaries.values() ] labels = chrBinBoundaries.keys() axHeat2.set_xticks(ticks) if len(labels) > 20: axHeat2.set_xticklabels(labels, size=4, rotation=90) else: axHeat2.set_xticklabels(labels, size=8) from mpl_toolkits.axes_grid1 import make_axes_locatable divider = make_axes_locatable(axHeat2) cax = divider.append_axes("right", size="2.5%", pad=0.09) if args.log1p: from matplotlib.ticker import LogFormatter formatter = LogFormatter(10, labelOnlyBase=False) # get a useful log scale # that looks like [1, 2, 5, 10, 20, 50, 100, ... etc] aa = np.array([1, 2, 5]) tick_values = np.concatenate([aa * 10**x for x in range(10)]) cbar = fig.colorbar(img3, ticks=tick_values, format=formatter, cax=cax) else: cbar = fig.colorbar(img3, cax=cax) cbar.solids.set_edgecolor( "face") # to avoid white lines in the color bar in pdf plots if args.scoreName: cbar.ax.set_ylabel(args.scoreName, rotation=270, size=8) axHeat2.set_ylim(start_pos2[0], start_pos2[-1]) axHeat2.set_xlim(start_pos[0], start_pos[-1]) if ylabel is not None: axHeat2.set_ylabel(ylabel) if xlabel is not None: axHeat2.set_xlabel(xlabel)
def get_formatter(self): return LogFormatter(10, labelOnlyBase=False)
def plot_projected_density_image(planets, debris, disk, shell): from prepare_figure import single_frame, figure_frame, set_tickmarks disk.temperature = mu() / constants.kB * disk.u x = disk.x.value_in(units.AU) y = disk.y.value_in(units.AU) z = disk.z.value_in(units.AU) T = disk.temperature.value_in(units.K) SLICE_DATA = False if SLICE_DATA: selection = z < 10 x = x[selection] y = y[selection] z = z[selection] T = T[selection] selection = z > -10 x = x[selection] y = y[selection] z = z[selection] T = T[selection] print("After slicing the data: N=", len(x)) rho = numpy.log10(disk.rho.value_in(units.g / units.cm**3)) print("Density=", rho.min(), rho.max(), "in log(g/cm^3)") #-12.9251289255 -9.9657991654 in log(g/cm^3) import numpy as np import matplotlib.pyplot as plt from matplotlib.ticker import NullFormatter nullfmt = NullFormatter() # no labels left, width = 0.2, 0.45 bottom, height = 0.1, 0.65 bottom_h = left_h = left + width + 0.02 rect_xy = [left, bottom, width, height] rect_xz = [left, bottom_h + 0.11, width + 0.114, 0.2] rect_zy = [left_h, bottom, 0.15, height] # start with a rectangular Figure fig = plt.figure(1, figsize=(12, 8)) ax_xy = plt.axes(rect_xy) ax_xz = plt.axes(rect_xz) ax_zy = plt.axes(rect_zy) # no labels ax_xz.xaxis.set_major_formatter(nullfmt) ax_zy.yaxis.set_major_formatter(nullfmt) # levels = [-16, -14, -13, -11] levels = 20 plot_density_view(ax_xy, x, y, rho, 100, 100, levels) plot_density_view(ax_xz, x, z, rho, 100, 30, levels) img = plot_density_view(ax_zy, z, y, rho, 30, 100, levels) # import matplotlib.ticker as ticker # cbar = pyplot.colorbar(img, ax=ax_xz, orientation='vertical', format=ticker.FuncFormatter(fmt)) # cbar = pyplot.colorbar(img, ax=ax_xz, orientation='vertical', format='%.0e') from matplotlib.ticker import LogFormatter formatter = LogFormatter(10, labelOnlyBase=False) bounds = [-4, -3, -2, -1] import matplotlib as mpl cmap = mpl.cm.jet ##cool norm = mpl.colors.BoundaryNorm(bounds, cmap.N) cbar = pyplot.colorbar(img, ax=ax_xz, orientation='vertical', format=formatter, norm=norm, ticks=bounds) # cbar = pyplot.colorbar(img, ax=ax_xz, orientation='vertical', format=formatter, # ticks=[-4, -3, -2]) # cb = pyplot.colorbar(ticks=[1,5,10,20,50], format=formatter) ax_bar = cbar.ax text = ax_bar.yaxis.label font = matplotlib.font_manager.FontProperties(family='times new roman', size=16) text.set_font_properties(font) cbar.set_label(r'$log(\rho) [g/cm^3]$', rotation=270, labelpad=+20) ax_xz.set_xlim(ax_xy.get_xlim()) ax_zy.set_ylim(ax_xy.get_ylim()) ax_xy.set_xlabel("X [au]") ax_xy.set_ylabel("Y [au]") ax_xz.set_ylabel("Z [au]") ax_zy.set_xlabel("Z [au]") pyplot.savefig("fig_disk_projected_density.pdf") plt.show()
def simulate_and_plot(Rs, f0, title_prefix='', date_ra=('2020-12-18', '2021-02-15'), n0=1e4, clip_nRo=('2099', '2099', '2099'), R_changes=None, use_r7=True, df_lohi=None, country_select=None): """Simulate and plot, given R and initial prevelance. - clip_nRo: optional max dates for (n_NL, R_nL, f_other) - R_changes: list of R-changes as tuples (date, R_scaling, label). For example: R_changes=[('2021-01-23', 0.8, 'Avondklok')] - df_lohi: optional DataFrame with columns nlo, nhi, Rlo, Rhi to use as confidence intervals. - country_select: selection preset (str) for which countries to show. See get_data_countries() for details. """ df = simulate_cases(f0=f0, Rs=Rs, date_ra=date_ra, n0=n0, use_r7=use_r7, R_changes=R_changes) # simulation for 'no interventions' df_nointv = simulate_cases(f0=f0, Rs=Rs, date_ra=date_ra, n0=n0, use_r7=use_r7, R_changes=None) df_R, dfc = get_Rt_cases() df_R = df_R.loc[df_R.index >= '2020-12-01'] dfc = dfc.loc[dfc.index >= '2020-12-25'] # cases colors = plt.rcParams['axes.prop_cycle']() colors = [next(colors)['color'] for _ in range(10)] fig, axs = plt.subplots(3, 1, tight_layout=True, sharex=True, figsize=(9, 10)) ## top panel: number of cases ax = axs[0] ax.set_ylabel('Aantal per dag') ax.semilogy(df['ni_old'], label='Infecties oude variant (simulatie)', color=colors[0], linestyle='-.') ax.semilogy(df['ni_b117'], label='Infecties B117 variant (simulatie)', color=colors[0], linestyle='--') ax.semilogy(df['ni_old'] + df['ni_b117'], label='Infecties totaal (simulatie)', color=colors[0], linestyle='-', linewidth=2) ax.semilogy(df['npos'], label='Positieve tests (simulatie)', color=colors[1], linestyle='-.') if df_lohi is not None: _fill_between_df(ax, df_lohi, 'nlo', 'nhi', color=colors[1], alpha=0.15, zorder=-10) if R_changes: ax.semilogy(df_nointv['npos'], label='P.T. (sim., geen maatregelen)', color=colors[1], linestyle=':') select = dfc.index <= clip_nRo[0] ax.semilogy(dfc.loc[select, 'Delta7r'] * 17.4e6, label='Positieve tests (NL)', color=colors[2], linestyle='--', linewidth=3) # first valid point of positive tests firstpos = df.loc[~df['npos'].isna()].iloc[0] ax.scatter(firstpos.name, firstpos['npos'], color=colors[1], zorder=10) ax.set_ylim(df['npos'].min() / 3, 20000) ax.yaxis.set_minor_formatter(LogFormatter(minor_thresholds=(2, 1))) date_labels = [('2020-12-15', 0, 'Lockdown')] + (R_changes or []) for date, _, label in date_labels: ax.text(pd.to_datetime(date), df['npos'].min() / 2.6, label, rotation=90, horizontalalignment='center') ax.grid() ax.grid(which='minor', axis='y') ax.legend(loc='lower left') ## R plot ax = axs[1] ax.set_ylabel('R') ax.plot(df['Rt'], label='$R_t$ (simulatie)', color=colors[1]) if df_lohi is not None: _fill_between_df(ax, df_lohi, 'Rlo', 'Rhi', color=colors[1], alpha=0.15, zorder=-10) if R_changes: ax.plot(df_nointv['Rt'], label='$R_t$ (sim., geen maatregelen)', color=colors[1], linestyle=':') ax.scatter(df.index[0], df['Rt'][0], color=colors[1], zorder=10) dfR1 = df_R.loc[df_R.index <= clip_nRo[1]] ax.fill_between(dfR1.index, dfR1['Rlo'], dfR1['Rhi'], color='#0000ff', alpha=0.15, label='$R_t$ (observatie NL)') date_labels = [('2020-12-15', 0, 'Lockdown')] + (R_changes or []) for date, _, label in date_labels: ax.text(pd.to_datetime(date), 0.82, label, rotation=90, horizontalalignment='center') ax.grid(zorder=0) ax.legend() ## odds plot ax = axs[2] ax.set_ylabel('Verhouding B117:overig (pos. tests)') ax.semilogy(f2odds(df['f_b117']), label='Nederland (simulatie)', color=colors[1]) if df_lohi is not None: df_lohi['odds_lo'] = f2odds(df_lohi['flo']) df_lohi['odds_hi'] = f2odds(df_lohi['fhi']) _fill_between_df(ax, df_lohi, 'odds_lo', 'odds_hi', color=colors[1], alpha=0.15, zorder=-10) markers = iter('o^vs*Do^vs*D' * 2) cdict, _meta_df = get_data_countries(select=country_select) for country_name, df in cdict.items(): df = df.loc[df.index <= clip_nRo[2]] marker = next(markers) label = country_name if len( country_name) < 25 else country_name[:23] + '...' ax.plot(df.index, f2odds(df['f_b117']), f'{marker}:', linewidth=2, label=label, zorder=100) ax.grid(which='both', axis='y') ax.legend(fontsize=10, framealpha=0.9) ax.yaxis.set_major_formatter(FormatStrFormatter('%g')) # Monkey-patch to prevent '%e' formatting. LogFormatter._num_to_string = lambda _0, x, _1, _2: ('%g' % x) ax.yaxis.set_minor_formatter(LogFormatter(minor_thresholds=(2, 1))) title = f'{title_prefix}R_oud={Rs[0]:.2f}; R_B117={Rs[1]:.2f}' if R_changes: title += f'\n(R wijzigt vanaf {R_changes[0][0]})' axs[0].set_title(title) for i in range(3): tools.set_xaxis_dateformat(axs[i], maxticks=15, ticklabels=(i == 2)) add_percentage_y2_axis(ax, label='Aandeel B.1.1.7 (%)') # repeat to get the weekly ticks tools.set_xaxis_dateformat(axs[2], maxticks=15) fig.show() plt.pause(0.75)
def plot_variable(u, name, direc, coords=None, cells=None, figsize=(8, 7), cmap='gist_yarg', scale='lin', numLvls=10, levels=None, levels_2=None, umin=None, umax=None, normalize_vec=False, plot_tp=False, tp_kwargs={ 'linestyle': '-', 'lw': 1.0, 'color': 'k', 'alpha': 0.5 }, show=True, hide_x_tick_labels=False, hide_y_tick_labels=False, vertical_y_labels=False, vertical_y_label=False, xlabel=r'$x$', ylabel=r'$y$', equal_axes=True, title='', hide_axis=False, colorbar_loc='right', contour_type='filled', extend='neither', ext='.pdf', plot_quiver=True, quiver_skip=0, quiver_kwargs={ 'pivot': 'middle', 'color': 'k', 'alpha': 0.8, 'width': 0.004, 'headwidth': 4.0, 'headlength': 4.0, 'headaxislength': 4.0 }, res=150, cb=True, cb_format='%.1e'): """ """ vec = False # assume initially that 'u' is not a vector # if 'u' is a NumPy array and the cell arrays and coordinates are supplied : if (type(u) == np.ndarray or type(u) == list or type(u) == tuple) \ and len(coords) == 2: x = coords[0] y = coords[1] v = u if type(cells) == np.ndarray: t = cells else: t = [] # if there are multiple components to 'u', it is a vector : if len(np.shape(u)) > len(np.shape(x)): vec = True # used for plotting below v0 = u[0] v1 = u[1] # compute norm : v = 0 for k in u: v += k**2 v = np.sqrt(v + 1e-16) # if 'u' is a NumPy array and cell/coordinate arrays are not supplied : if (type(u) == np.ndarray or type(u) == list or type(u) == tuple) \ and coords is None: print_text(">>> numpy arrays require `coords' <<<", 'red', 1) sys.exit(1) # if 'u' is a FEniCS Function : elif type(u) == indexed.Indexed \ or type(u) == fe.dolfin.function.Function \ or type(u) == fe.dolfin.functions.function.Function \ or type(u) == da.function.Function: # if this is a scalar : if len(u.ufl_shape) == 0: mesh = u.function_space().mesh() v = u.compute_vertex_values(mesh) # otherwise it is a vector, so calculate the L^2 norm : else: vec = True # used for plotting below # if the function is defined on a mixed space, deepcopy : # TODO: there is a way to do this without deepcopy if type(u[0]) == indexed.Indexed: out = u.split(True) else: out = u # extract the mesh : mesh = out[0].function_space().mesh() # compute norm : v = 0 for k in out: kv = k.compute_vertex_values(mesh) v += kv**2 v = np.sqrt(v + 1e-16) v0 = out[0].compute_vertex_values(mesh) v1 = out[1].compute_vertex_values(mesh) t = mesh.cells() x = mesh.coordinates()[:, 0] y = mesh.coordinates()[:, 1] # if normalized vectors are desired : if vec and normalize_vec: v0 = v0 / v v1 = v1 / v if vec: print_text("::: plotting vector variable :::", 'red') else: print_text("::: plotting scalar variable :::", 'red') #============================================================================= # plotting : if umin != None and levels is None: vmin = umin elif levels is not None: vmin = levels.min() else: vmin = v.min() if umax != None and levels is None: vmax = umax elif levels is not None: vmax = levels.max() else: vmax = v.max() # set the extended colormap : cmap = plt.get_cmap(cmap) # countour levels : if scale == 'log': if levels is None: levels = np.logspace(np.log10(vmin), np.log10(vmax), numLvls) v[v < vmin] = vmin + 2e-16 v[v > vmax] = vmax - 2e-16 formatter = LogFormatter(10, labelOnlyBase=False) norm = colors.LogNorm() # countour levels : elif scale == 'sym_log': if levels is None: levels = np.linspace(vmin, vmax, numLvls) v[v < vmin] = vmin + 2e-16 v[v > vmax] = vmax - 2e-16 formatter = LogFormatter(e, labelOnlyBase=False) norm = colors.SymLogNorm(vmin=vmin, vmax=vmax, linscale=0.001, linthresh=0.001) elif scale == 'lin': if levels is None: levels = np.linspace(vmin, vmax, numLvls) norm = colors.BoundaryNorm(levels, cmap.N) elif scale == 'bool': v[v < 0.0] = 0.0 levels = [0, 1, 2] norm = colors.BoundaryNorm(levels, cmap.N) fig = plt.figure(figsize=figsize) ax = fig.add_subplot(111) ax.set_xlabel(xlabel) yl = ax.set_ylabel(ylabel) if vertical_y_label: yl.set_rotation(0) if hide_x_tick_labels: ax.set_xticklabels([]) ax.xaxis.set_ticks_position('none') if hide_y_tick_labels: ax.yaxis.set_ticks_position('none') ax.set_yticklabels([]) if vertical_y_labels: plt.setp(ax.yaxis.get_majorticklabels(), rotation=-90, verticalalignment='center') #for tick in ax.get_yticklabels(): # tick.set_rotation(-90) if hide_axis: ax.axis('off') if equal_axes: ax.axis('equal') # filled contours : if contour_type == 'filled': # if the number of degrees equal the number of cells, a DG space is used : if len(v) == len(t): cs = ax.tripcolor(mesh2triang(mesh), v, shading='flat', cmap=cmap, norm=norm) # otherwise, a CG space is used : else: if len(np.shape(x)) > 1: cs_ftn = ax.contourf else: cs_ftn = ax.tricontourf if len(v) != len(t) and scale != 'log': cs = cs_ftn(x, y, v, triangles=t, levels=levels, cmap=cmap, norm=norm, extend=extend) elif len(v) != len(t) and scale == 'log': cs = cs_ftn(x, y, v, triangles=t, levels=levels, cmap=cmap, norm=norm) # non-filled contours : elif contour_type == 'lines': if len(np.shape(x)) > 1: cs_ftn = ax.contour else: cs_ftn = ax.tricontour cs = cs_ftn(x, y, v, triangles=t, linewidths=1.0, levels=levels, colors='k') for line in cs.collections: if line.get_linestyle() != [(None, None)]: #line.set_linestyle([(None, None)]) #line.set_color('red') # reduce the line size only if 'main' contours are needed : if levels_2 is not None: line.set_linewidth(1.5) if levels_2 is not None: cs2 = ax.tricontour(x, y, v, triangles=t, levels=levels_2, colors='0.30') for line in cs2.collections: if line.get_linestyle() != [(None, None)]: line.set_linestyle([(None, None)]) line.set_color('#c1000e') line.set_linewidth(0.5) ax.clabel(cs, inline=1, fmt=cb_format) # plot vectors, if desired : if vec and plot_quiver: # reduce the size of the dataset : if quiver_skip > 0: sav = range(0, len(x), quiver_skip) v0_quiv = v0[sav] v1_quiv = v1[sav] x_quiv = x[sav] y_quiv = y[sav] else: v0_quiv = v0 v1_quiv = v1 x_quiv = x y_quiv = y q = ax.quiver(x_quiv, y_quiv, v0_quiv, v1_quiv, **quiver_kwargs) # plot triangles, if desired : if plot_tp == True: tp = ax.triplot(x, y, t, **tp_kwargs) # this enforces equal axes no matter what (yeah, a hack) : divider = make_axes_locatable(ax) # include colorbar : if cb and scale != 'bool' and contour_type != 'lines': cax = divider.append_axes("right", "5%", pad="3%") cbar = fig.colorbar(cs, cax=cax, ticks=levels, format=cb_format) ax.set_xlim([x.min(), x.max()]) ax.set_ylim([y.min(), y.max()]) if title is None and cb is False: plt.tight_layout() else: plt.tight_layout(rect=[0, 0, 1, 0.95]) #mpl.rcParams['axes.titlesize'] = 'small' #tit = plt.title(title) # title : if title is not None: tit = plt.title(title) #tit.set_fontsize(40) # create the output directory : d = os.path.dirname(direc) if not os.path.exists(d): os.makedirs(d) # always save the figure to a file : plt.savefig(direc + name + ext, res=res) # show the figure too, if desired : if show: plt.show() else: plt.close(fig)
def plot_countries_odds_ratios(country_select='all_recent', subtract_eng_bg=True, wiki=False): """Generate graph with odds ratios over time on semi log scale. UK data is based on population sampling, mostly SGTF (background subtracted). UK SGTF data shifted by 14 days to estimate symptom onset. Other data is from genomic sequencing ('seq'). """ cdict, meta_df = get_data_countries(country_select, subtract_eng_bg=subtract_eng_bg) fig, ax = _setup_country_fig_ax(cdict) markers = iter('o^vs*Do^vs*D' * 4) colors = plt.rcParams['axes.prop_cycle']() colors = iter([next(colors)['color'] for _ in range(40)]) tm0 = pd.to_datetime('2020-12-01') one_day = pd.Timedelta(1, 'd') oddsfit_records = [] tm_now = pd.to_datetime('now') tm_now += pd.Timedelta(12 - tm_now.hour, 'h') # 12:00 noon for desc, df in cdict.items(): meta = meta_df.loc[desc] # metadata if meta['ccode'] and meta['is_seq'] or ('DK' not in meta_df['ccode']): # highlight countries with sequence data # (only if country data like DK is in the selection) plotargs = dict(zorder=0, alpha=0.9, linewidth=2, markersize=6) else: # SGTF data and sub-national regions plotargs = dict(zorder=-10, alpha=0.4, markersize=4) odds = f2odds(df['f_b117']).values tms = df.index xs = np.array((tms - tm0) / one_day) # Fitting on at most 6 weeks. ifirst = np.argmax(tms > tms[-1] - pd.Timedelta(42, 'd')) ifirst = max(1, ifirst) oslope, odds0 = fit_log_odds(xs[ifirst:], odds[ifirst:], last_weight=0.33) # show fit result odds_latest = np.exp(odds0 + oslope * xs[-1]) tm_latest = tms[-1].strftime("%Y-%m-%d") oddsfit_records.append( dict(region=desc, date=tm_latest, odds=float('%.4g' % odds_latest), log_slope=float('%.4g' % oslope))) xse = np.array([xs[ifirst], xs[-1]]) # expanded x range tms_fit = [tms[ifirst], tms[-1]] odds_fit = np.exp(oslope * xse + odds0) # extrapolate fit to present day. (Clip at odds=20, 21 days after most recent point) xs_ext = np.arange((tms[-1] - tm0) / one_day, (tm_now - tm0) / one_day) odds_ext = np.exp(oslope * xs_ext + odds0) tms_ext = np.array([tm0 + dt for dt in xs_ext * one_day]) mask_ext = (odds_ext < 15) & (tms_ext < tms[-1] + pd.Timedelta(21, 'd')) # draw the data p = next(markers) col = next(colors) label = f'{desc} [{oslope:.3f}]' ax.semilogy(tms, odds, f'{p}', color=col, label=label, **plotargs) ax.semilogy(tms_fit, odds_fit, '-', color=col, **plotargs) ax.semilogy(tms_ext[mask_ext], odds_ext[mask_ext], '--', color=col, **plotargs) if ifirst > 0: ax.semilogy(tms[:ifirst + 1], odds[:ifirst + 1], ':', color=col, **plotargs) odds_fit_df = pd.DataFrame.from_records(oddsfit_records).set_index( 'region') print(f'Slope fit results:\n{odds_fit_df}') if not wiki: ymin = ax.get_ylim()[0] ax.axvline(tm_now, color='#888888') ax.text(tm_now, ymin, tm_now.strftime(' %d %b'), horizontalalignment='right', verticalalignment='bottom', rotation=90) ax.set_ylabel('Odds ratio B.1.1.7/other variants') ax.yaxis.set_major_formatter(FormatStrFormatter('%g')) # Monkey-patch to prevent '%e' formatting. LogFormatter._num_to_string = lambda _0, x, _1, _2: ('%g' % x) ax.yaxis.set_minor_formatter(LogFormatter(minor_thresholds=(2, 1))) tools.set_xaxis_dateformat(ax) # must be before adding a second y axis. ax.legend(loc='upper left', bbox_to_anchor=(1.15, 1), fontsize=9) ax.set_title('B.1.1.7 presence in positive cases, with $\\log_e$ slopes') fig.canvas.set_window_title('B117 in countries/regions') if not wiki: # if subtract_eng_bg: # sgtf_subtracted = ' (backgroud positive rate subtracted for England regions)' # else: # sgtf_subtracted = '' # ax.text(1.10, -0.05 + 0.1 * (len(cdict) < 16), fig.text(0.99, 0.01, '@hk_nien', fontsize=8, horizontalalignment='right', verticalalignment='bottom') add_percentage_y2_axis(ax) tools.set_xaxis_dateformat( ax) # repeat, otherwise the y2 axis will delete the minor ticks. fig.show() plt.pause(0.5) if wiki: fname = f'output/uk_strain_status-{country_select}.png' fig.savefig(fname) print(f'Wrote {fname}.')
def __call__(self, v, pos=None): vv = self._base ** v return LogFormatter.__call__(self, vv, pos)
def plot_rad_map(header=None, contour_image=None, rad_field=None, cores=None, cores_to_keep=None, props=None, cloud_dict=None, regions=None, title=None, limits=None, contours=None, boxes=False, savedir='./', filename=None, show=False, hi_vlimits=None, vlimits=None, vscale='linear'): # Import external modules import matplotlib.pyplot as plt import matplotlib import numpy as np from mpl_toolkits.axes_grid1 import AxesGrid import pyfits as pf import matplotlib.pyplot as plt import pywcsgrid2 as wcs import pywcs from pylab import cm # colormaps from matplotlib.patches import Polygon import matplotlib.patheffects as PathEffects import myplotting as myplt # Set up plot aesthetics # ---------------------- #plt.close();plt.clf() # Color map cmap = plt.cm.copper # Color cycle, grabs colors from cmap # Create figure instance fig = plt.figure(figsize=(7.5, 5)) if rad_field is not None: nrows_ncols = (1, 1) ngrids = 1 imagegrid = AxesGrid(fig, (1, 1, 1), nrows_ncols=nrows_ncols, ngrids=ngrids, cbar_mode="single", cbar_location='right', cbar_pad="2%", cbar_size='3%', axes_pad=0.1, axes_class=(wcs.Axes, dict(header=header)), aspect=True, label_mode='L', share_all=True) # ------------------ # Av image # ------------------ # create axes ax = imagegrid[0] #ax = wcs.subplot(111, header=header) if vscale == 'log': norm = matplotlib.colors.LogNorm() else: norm = None # show the image im = ax.imshow( rad_field, interpolation='nearest', origin='lower', cmap=cmap, norm=norm, vmin=vlimits[0], vmax=vlimits[1], ) # Asthetics #ax.set_display_coord_system("fk5") #ax.set_ticklabel_type("hms", "dms") #ax.set_xlabel('Right Ascension [J2000]',) #ax.set_ylabel('Declination [J2000]',) ax.set_display_coord_system("gal") #ax.set_ticklabel_type("hms", "dms") ax.set_ticklabel_type("dms", "dms") #ax.set_xlabel('Right Ascension [J2000]',) #ax.set_ylabel('Declination [J2000]',) ax.set_xlabel('Galactic Longitude [deg]', ) ax.set_ylabel('Galactic Latitude [deg]', ) ax.axis["top", ].toggle(ticklabels=True) ax.grid(color='w', linewidth=0.5) ax.axis[:].major_ticks.set_color("w") cmap.set_bad(color='w') #ax.locator_params(nbins=6) ax.locator_params(nbins=6) # colorbar from matplotlib.ticker import LogFormatter formatter = LogFormatter(10, labelOnlyBase=False) ticks = np.logspace( np.log10(vlimits[0]), np.log10(vlimits[1]), #np.log10(vlimits[1]) - np.log10(vlimits[0]), 5, ) from matplotlib.ticker import LogFormatterMathtext if 1: cb = ax.cax.colorbar(im, #ticks=ticks, #format=LogFormatterMathtext(), ) #cb.set_label_text(r'log$_{10}$[$U_{0,M83}$]',) cb.set_label_text(r'$U\,[U_{\rm D78}$]', ) else: from mpl_toolkits.axes_grid1 import make_axes_locatable divider = make_axes_locatable(ax) #cax = divider.append_axes("right", size="5%", pad=0.05) #plt.colorbar(im, cax=cax) #cb = fig.colorbar(im, ax=ax) #cb.ax.set_ylabel(r'$U_{0,M83}$',) # plot limits if limits is not None: limits_pix = myplt.convert_wcs_limits(limits, header, frame='fk5') ax.set_xlim(limits_pix[0], limits_pix[1]) ax.set_ylim(limits_pix[2], limits_pix[3]) # Plot Av contours if contour_image is not None: ax.contour(contour_image, levels=contours, colors='r') if props is not None: if 0: for region in props['region_name_pos']: if region == 'taurus1': region = 'taurus 1' if region == 'taurus2': region = 'taurus 2' ax.annotate(region.capitalize(), xy=props['region_name_pos'][region]['pixel'], xytext=(0, 0), textcoords='offset points', color='w', fontsize=7, zorder=10) if regions is not None: for region in props['region_name_pos']: vertices = np.copy(regions[region]['poly_verts']['pixel']) rect = ax.add_patch( Polygon(vertices[:, ::-1], facecolor='none', edgecolor='w')) # ax.legend(rects, core_labels, bbox_to_anchor=(1.05, 1), loc=2, # borderaxespad=0.) #ax.legend(rects, core_labels, bbox_to_anchor=(0., 1.02, 1., .102), loc=3, # ncol=5, mode="expand", borderaxespad=0.) if filename is not None: plt.savefig(savedir + filename, bbox_inches='tight') if show: fig.show()