def plot_colorbar(cax, cmap, hue_norm, cnorm=None, label=None, orientation='vertical', labelsize=4, linewidth=0.5): if isinstance(cmap, str): cmap = copy.copy(get_cmap(cmap)) if cnorm is None: cnorm = Normalize(vmin=hue_norm[0], vmax=hue_norm[1]) from .utilities import smart_number_format colorbar = ColorbarBase(cax, cmap=cmap, norm=cnorm, format=ticker.FuncFormatter(smart_number_format), orientation=orientation, extend='both') colorbar.locator = ticker.MaxNLocator(nbins=3) colorbar.update_ticks() colorbar.set_label(label, fontsize=labelsize) colorbar.outline.set_linewidth(linewidth) colorbar.ax.tick_params(size=labelsize, labelsize=labelsize, width=linewidth) return cax
def display_displacements(simulation_folder, lower_percentile=70, upper_percentile=99.7, save_plot = True, arrow_factor=4): """ show the total displacement field or the masked range of displacements by size simulation_folder: path to simulation folder lower_percentile and upper_percentile give range of deformationsize which is used as a mask save_plot: option to save the plot to the simulation folder arrow_factor: scales the arrow length in the plot To increase plotting speed you might increase the lower percentile (e.g 30 instead 0) """ # load in deformations and coordinates r = np.genfromtxt(os.path.join(simulation_folder, "R.dat")) # positions u = np.genfromtxt(os.path.join(simulation_folder, "Ufound.dat")) #deformations uabs = np.sqrt(np.sum(u ** 2., axis=1)) # absolute values for filtering # filter displacements by absolute size mask = (uabs > np.percentile(uabs, lower_percentile)) & (uabs < np.percentile(uabs, upper_percentile)) r2 = r[mask] u2 = u[mask] uabs2 = uabs[mask] # plot the masked deformation fig = plt.figure() ax1 = fig.gca(projection='3d', label='fitted-displ', rasterized=True) color_bounds1 = np.array([np.percentile(uabs2, 0), np.percentile(uabs2, 99.8)]) * 10 ** 6 np.random.seed(1234) for r2i, u2i, uabs2i in tqdm(zip(r2 * 10 ** 6, u2 * 10 ** 6, uabs2 * (10 ** 6))): # if np.random.uniform(0, 1) < 0.2: # uabs2i/u_upper: color = plt.cm.jet(((uabs2i - color_bounds1[0]) / (color_bounds1[1] - color_bounds1[0]))) alpha = 1. - (r2i[0] - r2i[1]) / (270. * 0.5) if alpha > 1: alpha = 1. if alpha < 0: alpha = 0. plt.quiver(r2i[0], r2i[1], r2i[2], u2i[0], u2i[1], u2i[2], length=uabs2i * arrow_factor , color=color, arrow_length_ratio=0, alpha=alpha, pivot='tip', linewidth=0.5) # plot colorbar --------------------------------------------------------- cbaxes = fig.add_axes([0.15, 0.1, 0.125, 0.010]) cmap = plt.cm.jet norm = plt.Normalize(vmin=color_bounds1[0], vmax=color_bounds1[1]) cb1 = ColorbarBase(cbaxes, cmap=cmap, norm=norm, orientation='horizontal') cb1.set_label('Displacements [µm]') tick_locator = ticker.MaxNLocator(nbins=3) cb1.locator = tick_locator cb1.update_ticks() ax1.w_xaxis.set_pane_color((0.2, 0.2, 0.2, 1.0)) ax1.w_yaxis.set_pane_color((0.2, 0.2, 0.2, 1.0)) ax1.w_zaxis.set_pane_color((0.2, 0.2, 0.2, 1.0)) if save_plot: plt.savefig( os.path.join(simulation_folder,'deformations_plot_lower_{}_upper_{}.png'.format(lower_percentile, upper_percentile)), dpi=500, bbox_inches="tight", pad_inches=0) return
def create_colorbar(cax, cmap_range=(0, 1), cmap_name="jet", colorbar_title="", colorbar_loc="top", ticksize=8, num_ticks=None): """ create colorbar see https://matplotlib.org/devdocs/tutorials/colors/colorbar_only.html Parameters ---------- cax: matplotlib.axes.Axes cmap_name: str cmap_range: tuple colorbar_title: str colorbar_loc: str 'top' or 'bottom' ticksize: int """ assert colorbar_loc == "top" or colorbar_loc == "bottom" norm = Normalize(vmin=cmap_range[0], vmax=cmap_range[1]) cmap = get_cmap(cmap_name) cbar = ColorbarBase(cax, cmap=cmap, norm=norm, orientation="horizontal", label=colorbar_title, ticklocation=colorbar_loc, extend="both") #tick_locator = ticker.MaxNLocator(nbins=5) if num_ticks is not None: tick_locator = ticker.LinearLocator(numticks=num_ticks) cbar.locator = tick_locator cbar.update_ticks() cax.tick_params(labelsize=ticksize) return cbar
def display_forces(simulation_folder, lower_percentile=97.5, upper_percentile=99, save_plot = True, arrow_factor=5): """ show the force density field of the masked force components simulation_folder: path to simulation folder lower_percentile and upper_percentile give range of forces which is used as a mask save_plot: option to save the plot to the simulation folder arrow_factor: scales the arrow length in the plot """ # load in forces and coordinates r = np.genfromtxt(os.path.join(simulation_folder, "R.dat")) # positions f = np.genfromtxt(os.path.join(simulation_folder, "Fden.dat")) # force densities fabs = np.sqrt(np.sum(f ** 2., axis=1)) # absolute values for filtering # filter mask = (fabs > np.percentile(fabs, lower_percentile)) & (fabs < np.percentile(fabs, upper_percentile)) r2 = r[mask] f2 = f[mask] fabs2 = fabs[mask] # Force fig2 = plt.figure() ax2 = fig2.gca(projection='3d', label='fitted-forces', rasterized=True) color_bounds2 = np.array([np.percentile(fabs2, 0.1), np.percentile(fabs2, 99.9)]) * 10 ** -6 for r2i, f2i, fabs2i in tqdm(zip(r2 * 10 ** 6, f2 * 10 ** -6, fabs2 * (10 ** -6))): #*f_scale color = plt.cm.hot(((fabs2i - color_bounds2[0]) / (color_bounds2[1] - color_bounds2[0]))) # alpha = 1. - (r2i[0] - r2i[1]) / (270. * 1.25) # if alpha > 1: # alpha = 1. alpha = 1. ax2.quiver(r2i[0], r2i[1], r2i[2], f2i[0], f2i[1], f2i[2], length=fabs2i * arrow_factor , color=color, arrow_length_ratio=0, alpha=alpha, pivot='tip', linewidth=0.5) # plot colorbar --------------------------------------------------------- cbaxes = fig2.add_axes([0.15, 0.1, 0.125, 0.010]) cmap = plt.cm.hot norm = plt.Normalize(vmin=color_bounds2[0], vmax=color_bounds2[1]) cb1 = ColorbarBase(cbaxes, cmap=cmap, norm=norm, orientation='horizontal') cb1.set_label('Force Den. [pN/µm³]') tick_locator = ticker.MaxNLocator(nbins=3) cb1.locator = tick_locator cb1.update_ticks() # ------------------------------------------------------------------------- #ax2.set_xlim([-150, 150]) can be used to make an individual plot #ax2.set_ylim([-150, 150]) #ax2.set_zlim([-150, 150]) #ax2.set_xticks([-100, -50, 0, 50, 100]) #ax2.set_yticks([-100, -50, 0, 50, 100]) #ax2.set_zticks([-100, -50, 0, 50, 100]) # ax2.set_xticklabels(['']*5) # ax2.set_yticklabels(['']*5) # ax2.set_zticklabels(['']*5) ax2.w_xaxis.set_pane_color((0.2, 0.2, 0.2, 1.0)) ax2.w_yaxis.set_pane_color((0.2, 0.2, 0.2, 1.0)) ax2.w_zaxis.set_pane_color((0.2, 0.2, 0.2, 1.0)) if save_plot: plt.savefig( os.path.join(simulation_folder,'force_density_plot_lower_{}_upper_{}.png'.format(lower_percentile, upper_percentile)), dpi=500 , bbox_inches="tight", pad_inches=0) return