def draw(self, renderer): """Draw the line and arrowhead using the passed renderer. """ # if self._invalid: # self.recache() renderer.open_group('arrowline2d') if not self._visible: return Line2D.draw(self, renderer) if self._arrow is not None: gc = renderer.new_gc() self._set_gc_clip(gc) gc.set_foreground(self._arrowedgecolor) gc.set_linewidth(self._arrowedgewidth) gc.set_alpha(self._alpha) funcname = self.arrows.get(self._arrow, '_draw_nothing') if funcname != '_draw_nothing': tpath, affine = self._transformed_path\ .get_transformed_points_and_affine() arrow_func = getattr(self, funcname) arrow_func(renderer, gc, tpath, affine.frozen()) renderer.close_group('arrowline2d')
def __init__(self, ticksize=5., tick_out=False, **kwargs): self.set_ticksize(ticksize) self.set_tick_out(tick_out) self.clear() Line2D.__init__(self, [0.], [0.], **kwargs) self.set_color('black') self.set_visible_axes('all')
def draw(self, renderer): x, y = self.get_data() if len(x) == 2 or len(y) == 2: xlim = self.axes.get_xlim() ylim = self.axes.get_ylim() x0, y0 = x[0], y[0] x1, y1 = x[1], y[1] if x0 == x1: # vertical x, y = (x0, x0), ylim elif y0 == y1: # horizontal x, y = xlim, (y0, y0) else: # coeff != 0 coeff = float(y1 - y0) / (x1 - x0) minx = (ylim[0] - y0) / coeff + x0 maxx = (ylim[1] - y0) / coeff + x0 miny = coeff * (xlim[0] - x0) + y0 maxy = coeff * (xlim[1] - x0) + y0 if coeff > 0: x = max(minx, xlim[0]), min(maxx, xlim[1]) y = max(miny, ylim[0]), min(maxy, ylim[1]) else: x = max(maxx, xlim[0]), min(minx, xlim[1]) y = min(miny, ylim[1]), max(maxy, ylim[0]) self.set_data(x, y) Line2D.draw(self, renderer)
def __init__(self, ticksize=None, tick_out=False, **kwargs): if ticksize is None: ticksize = rcParams["xtick.major.size"] self.set_ticksize(ticksize) self.set_tick_out(tick_out) # FIXME: tick_out is incompatible with Matplotlib tickdir option self.clear() line2d_kwargs = {"color": rcParams["xtick.color"], "linewidth": rcParams["xtick.major.width"]} line2d_kwargs.update(kwargs) Line2D.__init__(self, [0.0], [0.0], **line2d_kwargs) self.set_visible_axes("all") self._display_minor_ticks = False
def __init__(self, *args, **kwargs): """Initialize the line and arrow. See the top-level class documentation. """ self._arrow = kwargs.pop('arrow', '-') self._arrowsize = kwargs.pop('arrowsize', 2 * 4) self._arrowedgecolor = kwargs.pop('arrowedgecolor', 'b') self._arrowfacecolor = kwargs.pop('arrowfacecolor', 'b') self._arrowedgewidth = kwargs.pop('arrowedgewidth', 4) self._arrowheadwidth = kwargs.pop('arrowheadwidth', self._arrowsize) self._arrowheadlength = kwargs.pop('arrowheadlength', self._arrowsize) Line2D.__init__(self, *args, **kwargs)
def __init__(self, ticksize=None, tick_out=None, **kwargs): if ticksize is None: ticksize = rcParams['xtick.major.size'] self.set_ticksize(ticksize) self.set_minor_ticksize(rcParams['xtick.minor.size']) self.set_tick_out(rcParams['xtick.direction'] == 'out') self.clear() line2d_kwargs = {'color': rcParams['xtick.color'], 'linewidth': rcParams['xtick.major.width']} line2d_kwargs.update(kwargs) Line2D.__init__(self, [0.], [0.], **line2d_kwargs) self.set_visible_axes('all') self._display_minor_ticks = False
def draw(self, renderer): xlim = self.ax.get_xlim() ind0, ind1 = np.searchsorted(self.xorig, xlim) self._x = self.xorig[ind0:ind1] self._y = self.yorig[ind0:ind1] N = len(self._x) if N<1000: self._marker = 's' self._linestyle = '-' else: self._marker = None self._linestyle = '-' Line2D.draw(self, renderer)
def __init__(self, ticksize, tick_out=False, **kwargs): self._ticksize = ticksize self.locs_angles_labels = [] self.set_tick_out(tick_out) self._axis = kwargs.pop("axis", None) if self._axis is not None: if "color" not in kwargs: kwargs["color"] = "auto" if ("mew" not in kwargs) and ("markeredgewidth" not in kwargs): kwargs["markeredgewidth"] = "auto" Line2D.__init__(self, [0.], [0.], **kwargs) AttributeCopier.__init__(self, self._axis, klass=Line2D) self.set_snap(True)
def __init__(self, ticksize=None, tick_out=False, **kwargs): if ticksize is None: ticksize = rcParams['xtick.major.size'] self.set_ticksize(ticksize) self.set_tick_out(tick_out) # FIXME: tick_out is incompatible with Matplotlib tickdir option self.clear() line2d_kwargs = { 'color': rcParams['xtick.color'], 'linewidth': rcParams['xtick.major.width'] } line2d_kwargs.update(kwargs) Line2D.__init__(self, [0.], [0.], **line2d_kwargs) self.set_visible_axes('all')
def __init__(self, ticksize=None, tick_out=None, **kwargs): if ticksize is None: ticksize = rcParams['xtick.major.size'] self.set_ticksize(ticksize) self.set_tick_out(rcParams.get('xtick.direction', 'in') == 'out') self.clear() line2d_kwargs = {'color': rcParams['xtick.color'], # For the linewidth we need to set a default since old versions of # matplotlib don't have this. 'linewidth': rcParams.get('xtick.major.width', 1)} line2d_kwargs.update(kwargs) Line2D.__init__(self, [0.], [0.], **line2d_kwargs) self.set_visible_axes('all') self._display_minor_ticks = False
def __init__(self, ticksize=None, tick_out=None, **kwargs): if ticksize is None: ticksize = rcParams["xtick.major.size"] self.set_ticksize(ticksize) self.set_tick_out(rcParams.get("xtick.direction", "in") == "out") self.clear() line2d_kwargs = { "color": rcParams["xtick.color"], # For the linewidth we need to set a default since old versions of # matplotlib don't have this. "linewidth": rcParams.get("xtick.major.width", 1), } line2d_kwargs.update(kwargs) Line2D.__init__(self, [0.0], [0.0], **line2d_kwargs) self.set_visible_axes("all") self._display_minor_ticks = False
def __init__(self, xfield = None, yfield = None, canvas = None, *args, **kwargs): self.canvas = canvas self.original_xfield = xfield self.original_yfield = yfield self.current_xfield = xfield self.current_yfield = yfield self.press = None self.draggable = False self.color = "b" self.marker = "None" self.xscale = 1.0 self.yscale = 1.0 self.xoffset = 0 self.yoffset = 0 Line2D.__init__(self, self.current_xfield.to_list(), self.current_yfield.to_list(), *args,**kwargs)
def set_marker(self, marker): self.marker = marker ret = Line2D.set_marker(self, marker) if self.canvas: self.canvas.draw() return ret
for mysamp_idx, mysamp in enumerate(samp_order): plt.bar(bin_ls_array + offset_ls[mysamp_idx], percentDict[samp_dict[mysamp]], color=colors[mysamp], width=0.125) plt.xticks(bin_ls_array) ax.set_xticks(bin_ls_array - (.5 * 0.125)) # len(bin_ls_array) is 17 myxtick_names = [ 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9 ] ax.set_xticklabels(myxtick_names) #plt.setp(ax.get_xticklabels()[::2], visible=False) # legend plotting point1 = Line2D([0], [0], color="#000000", lw=4, label='Northern Spotted Owl (Marin County)') point2 = Line2D([0], [0], color="#d55e00", lw=4, label='Northern Spotted Owl (Humboldt County)') point3 = Line2D([0], [0], color="#cc79a7", lw=4, label='California Spotted Owl (Nevada County)') point4 = Line2D([0], [0], color="#2b9f78", lw=4, label='California Spotted Owl (San Diego County)') plt.legend(handles=[point1, point2, point3, point4], framealpha=1) title_string = "Histogram of runs of homozygosity (ROH)"
def visualize(self, file_name=None, fig_size: (float, float) = (6.5, 6.5), size_auv_path: float = 0.8, size_max_radius: float = 0.3, size_min_radius: float = 0.1, tick_size: float = 14, grid_width: float = 0.25, size_arrow_h_width: float = 0.4, size_arrow_h_length: float = 0.3, size_arrow_width: float = 0.4, color_obstacle: str = 'firebrick', color_target: str = 'deepskyblue', color_auv: str = 'darkorange', color_auv_path: str = 'peachpuff', visited_reward_opacity: float = 0.15) -> Figure: if (fig_size[0] <= 0 or fig_size[1] <= 0 or size_auv_path <= 0 or size_max_radius <= 0 or size_arrow_h_width <= 0 or size_arrow_h_length <= 0 or size_arrow_width <= 0 or tick_size <= 0 or grid_width <= 0): raise ValueError("Size must be positive") max_reward = self._environment.max_reward title_font = { 'fontname': 'Sans Serif', 'size': '16', 'color': 'black', 'weight': 'bold' } z = {'auv_path': 1, 'target': 2, 'obstacle': 3, 'auv': 5} # Initialize the figure fig = plt.figure(figsize=fig_size) ax = fig.add_subplot(111) plt.hlines(y=range(self._environment.y_min, self._environment.y_max + 1), xmin=self._environment.x_min, xmax=self._environment.x_max, color='k', linewidth=grid_width, zorder=0) plt.vlines(x=range(self._environment.x_min, self._environment.x_max + 1), ymin=self._environment.y_min, ymax=self._environment.y_max, color='k', linewidth=grid_width, zorder=0) # Plot obstacles for i, j in self._environment.obstacles: ax.add_patch( Rectangle(xy=(i, j), width=1, height=1, color=color_obstacle, zorder=z['obstacle'])) # Plot rewards for position, reward in self._environment.rewards.items(): target_radius = ((reward / max_reward) * (size_max_radius - size_min_radius) + size_min_radius) centroid = (position[0] + 0.5, position[1] + 0.5) ax.add_patch( Circle(xy=centroid, radius=target_radius, color=color_target, zorder=z['target'], alpha=(visited_reward_opacity if position in self.visited else 1.0))) # Plot agents for path in self._paths: x, y = path[-1] dx, dy = 0, 1 if len(path) >= 2: x_p, y_p = path[-2] if x == x_p + 1 and y == y_p: dx, dy = 1, 0 elif x == x_p - 1 and y == y_p: dx, dy = -1, 0 elif x == x_p and y == y_p - 1: dx, dy = 0, -1 x += 0.5 * float(1 - dx) y += 0.5 * float(1 - dy) ax.add_patch( FancyArrow(x=x, y=y, dx=dx, dy=dy, fc=color_auv, width=size_arrow_width, head_width=size_arrow_h_width, head_length=size_arrow_h_length, zorder=z['auv'], length_includes_head=True)) # plot trajectories for i in range(1, len(path)): x, y = path[i] x_p, y_p = path[i - 1] ax.add_line( Line2D(xdata=(x + 0.5, x_p + 0.5), ydata=(y + 0.5, y_p + 0.5), linewidth=size_auv_path * 10, color=color_auv_path, zorder=z['auv_path'])) # Plotting plt.title('AUV Trajectory \n Accumulated Reward: ' + str(self.reward), title_font) plt.xlabel('x', title_font) plt.ylabel('y', title_font) x_ticks = np.arange(self._environment.x_min, self._environment.x_max + 1, 1) y_ticks = np.arange(self._environment.y_min, self._environment.y_max + 1, 1) plt.xticks(x_ticks + 0.5, x_ticks.astype(int)) plt.yticks(y_ticks + 0.5, y_ticks.astype(int)) ax.tick_params(labelsize=tick_size) ax.grid(False) ax.axis('equal') ax.set_xlim(self._environment.x_min - 0.5, self._environment.x_max + 1.5) ax.set_ylim(self._environment.y_min - 0.5, self._environment.y_max + 1.5) # Save and display plt.show() if file_name is not None: plt.savefig(file_name) return fig
#%% fig, axs = plt.subplots(1, 1, sharex=True, sharey=True, figsize=(8, 6)) tu = pde.exactu(fem.p) for k in range(n): vert = mesh.p[mesh.t[k, :]] x = np.linspace(vert[0], vert[1], 11) uhK = uh[fem.t[k, :]] u = evalFEfun1D(x, uhK, vert, pd, 0) axs.plot(x, pde.exactu(x), 'k', lw=2) axs.plot(x, u, 'r--', lw=2) axs.grid() custom_lines = [ Line2D([0], [0], color='k', lw=2), Line2D([0], [0], color='r', ls='--', lw=2) ] axs.legend(custom_lines, ['$u$', '$Iu$']) axs.set_xlim(domain) axs.set_xlabel('$x$') plt.show() #%% errL2, errKL2 = getErr1D(uh, pde.exactu, mesh, fem, ng, 0) errH1, errKH2 = getErr1D(uh, pde.Du, mesh, fem, ng, 1) print('#----------------------------------------#') print('n = %d' % (n)) print('L2 error: %5.3e' % errL2) print('H1 error: %5.3e' % errH1)
def well_plot(data_dict, outlier_dict=None, fig=None, ax=None, title=None, outfile=None, sort=False, print_medians=False, figsize=(7.1, 4), colorbar_range=None, cmap=None, radius=0.45, wedge_width=0.2, infected_marker_width=0.05, angular_gap=0.0, min_samples_per_well=None): """ Shows result in the structure of a 96 well plate. Parameters ---------- data_dict : dict Dictionary mapping filenames to scores. outlier_dict : dict, optional Dictionary whose keys are filenames or well-names (something that get_well() works on, like 'WellG04') of wells corresponding to outliers. They will be marked with a red circle. The values should be the outlier types. fig : matplotlib.figure.Figure, optional ax : matplotlib.axes.Axes, optional title : str, optional outfile : str, optional Path to save the plot at. sort : bool If True, the observations for each well are sorted. print_medians : bool If True, the median values for each well are overlaid as text. figsize : tuple colorbar_range : tuple, optional Min and max of the colorbar. Useful if multiple well plots will be compared by eye. radius : float Radius of the circles for each well wedge_width : float Width of the wedges showing the individual observations per well. Set to 0 to only show the median. infected_marker_width : float Width of the red ring around wells of infected patients. angular_gap : float Gap (in degrees) between neighboring wedges. Useful to clearly see number of observations per well. min_samples_per_well : int, optional Wells with less samples will not be displayed. """ if isinstance(outlier_dict, list): # for backwards compatibility outlier_dict = {well: '' for well in outlier_dict} per_well_dict = make_per_well_dict(data_dict, min_samples_per_well) patches = [] patch_values = [] nan_patches = [] for well_position, values in per_well_dict.items(): n_samples = len(values) center = well_position[1], 7 - well_position[0] if sort: values = sorted(values) # central circle is showing the median central_circle = Circle(center, radius - wedge_width) median = np.median(values) if not math.isnan(median): patches.append(central_circle) patch_values.append(median) else: nan_patches.append(central_circle) # outer wedges show values for individual images if wedge_width == 0: continue for i, value in enumerate(values): wedge = Wedge(center, radius, (360 / n_samples * (i + angular_gap)), 360 / n_samples * (i + 1 - angular_gap), width=wedge_width) if not math.isnan(value): patches.append(wedge) patch_values.append(value) else: nan_patches.append(wedge) if fig is None or ax is None: assert fig is None and ax is None, f'Please specify either neither or both fig and ax' fig, ax = plt.subplots(figsize=figsize) coll = PatchCollection(patches) coll.set_array(np.array(patch_values)) if colorbar_range is not None: coll.set_clim(*colorbar_range) if cmap is not None: coll.set_cmap(cmap) ax.add_collection(coll) plt.gca().set_aspect('equal', adjustable='box') plt.xticks(np.arange(12), np.arange(1, 13)) plt.xlim(-0.7, 11.7) plt.ylim(-0.7, 7.7) plt.yticks(np.arange(len(row_letters)), reversed(row_letters)) ax.add_collection(PatchCollection(nan_patches, facecolors='r')) if outlier_dict is not None: outlier_dict = { get_well(well): str(description) for well, description in outlier_dict.items() } # add red circles around infected patients infected_marker_patches = [] for well_position in outlier_dict: center = well_position[1], 7 - well_position[0] infected_marker_patches.append( Wedge(center, radius, 0, 360, width=infected_marker_width)) infected_marker_patches = PatchCollection(infected_marker_patches, facecolors='r') ax.add_collection(infected_marker_patches) def add_superscript(string, supscript): if supscript is None or supscript == '': return string return string + '$^{' + supscript + '}$' if print_medians: description_to_label = OUTLIER_TYPE_DICT.copy() for well_position, values in per_well_dict.items(): center = well_position[1], 7 - well_position[0] if outlier_dict is not None and well_position in outlier_dict: superscript = [] for description in outlier_dict[well_position].split(';'): if description not in description_to_label: description_to_label[description] = str( len(description_to_label) + 1) superscript.append(description_to_label[description]) superscript = ','.join(superscript) else: superscript = None median = np.median(values) t = plt.annotate(add_superscript(f'{median:4.2f}', superscript), center, ha='center', va='center') t.set_bbox(dict(edgecolor='white', facecolor='white', alpha=0.3)) legend_elements = [ Line2D([0], [0], marker='o', color='w', label='outlier wells', markerfacecolor=None, markeredgecolor='r', markersize=10, markeredgewidth=2), ] legend_elements.extend([ Line2D([0], [0], marker=f'${label}$', color='w', label=description, markerfacecolor='k', markeredgecolor='None', markersize=None) for description, label in description_to_label.items() ]) plt.gca().legend(handles=legend_elements, loc='upper center', bbox_to_anchor=(0.5, -0.05), ncol=2) if title is not None: plt.title(title) divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="3%", pad=0.1) fig.colorbar(coll, cax=cax) plt.tight_layout() if outfile is not None: plt.savefig(outfile, dpi=300, quality=75, optimize=True, bbox_inches='tight') plt.close()
sns.despine(ax=axis) pal = sns.color_palette() fig, axes = plt.subplots(1, len(devices), sharey=True, figsize=(17.0 * plot_settings.cm_to_inches, 5.0 * plot_settings.cm_to_inches)) # Plot data for each device for i, a in enumerate(axes): plot_line(a, data, i, algorithms, pal, i == 0) # Add axis labels for i, (a, d) in enumerate(zip(axes, devices)): a.set_title(chr(ord("A") + i) + ": " + d, loc="left") # Show figure legend with devices beneath figure legend_actors = [Line2D([], [], color=p) for _, p in zip(algorithms, pal)] fig.legend(legend_actors, algorithms, ncol=len(algorithms), frameon=False, loc="lower center") plt.tight_layout(pad=0, w_pad=1.0, rect=[0.0, 0.125, 1.0, 1.0]) if not plot_settings.presentation: fig.savefig("../figures/performance_scaling.pdf") plt.show()
def create_artists(self, legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans): plotlines, caplines, barlinecols = orig_handle xdata, xdata_marker = self.get_xdata(legend, xdescent, ydescent, width, height, fontsize) ydata = ((height - ydescent) / 2.) * np.ones(xdata.shape, float) legline = Line2D(xdata, ydata) xdata_marker = np.asarray(xdata_marker) ydata_marker = np.asarray(ydata[:len(xdata_marker)]) xerr_size, yerr_size = self.get_err_size(legend, xdescent, ydescent, width, height, fontsize) legline_marker = Line2D(xdata_marker, ydata_marker) # when plotlines are None (only errorbars are drawn), we just # make legline invisible. if plotlines is None: legline.set_visible(False) legline_marker.set_visible(False) else: self.update_prop(legline, plotlines, legend) legline.set_drawstyle('default') legline.set_marker('None') self.update_prop(legline_marker, plotlines, legend) legline_marker.set_linestyle('None') if legend.markerscale != 1: newsz = legline_marker.get_markersize() * legend.markerscale legline_marker.set_markersize(newsz) handle_barlinecols = [] handle_caplines = [] if orig_handle.has_xerr: verts = [((x - xerr_size, y), (x + xerr_size, y)) for x, y in zip(xdata_marker, ydata_marker)] coll = mcoll.LineCollection(verts) self.update_prop(coll, barlinecols[0], legend) handle_barlinecols.append(coll) if caplines: capline_left = Line2D(xdata_marker - xerr_size, ydata_marker) capline_right = Line2D(xdata_marker + xerr_size, ydata_marker) self.update_prop(capline_left, caplines[0], legend) self.update_prop(capline_right, caplines[0], legend) capline_left.set_marker("|") capline_right.set_marker("|") handle_caplines.append(capline_left) handle_caplines.append(capline_right) if orig_handle.has_yerr: verts = [((x, y - yerr_size), (x, y + yerr_size)) for x, y in zip(xdata_marker, ydata_marker)] coll = mcoll.LineCollection(verts) self.update_prop(coll, barlinecols[0], legend) handle_barlinecols.append(coll) if caplines: capline_left = Line2D(xdata_marker, ydata_marker - yerr_size) capline_right = Line2D(xdata_marker, ydata_marker + yerr_size) self.update_prop(capline_left, caplines[0], legend) self.update_prop(capline_right, caplines[0], legend) capline_left.set_marker("_") capline_right.set_marker("_") handle_caplines.append(capline_left) handle_caplines.append(capline_right) artists = [] artists.extend(handle_barlinecols) artists.extend(handle_caplines) artists.append(legline) artists.append(legline_marker) for artist in artists: artist.set_transform(trans) return artists
if validate: r_wsd_vl_acc_s = (df_R_vl['r_acc'] - 1) * 100 r_wsd_vl_acc_s.plot(ax=ax, secondary_y=True, linewidth=1, color='C1') s_bh_vl.plot(ax=ax, secondary_y=True, linewidth=1, color='C2', style='-') ax.right_ax.set_ylabel("Acc. Return (%)") ax.right_ax.yaxis.set_major_formatter(FormatStrFormatter('%2.0f')) ax.legend(handles=[tr_pt, te_pt, vl_pt], loc='upper left') colors = ['C0', 'C1', 'C2'] lines = [Line2D([0], [0], color=c) for c in colors] labels = ['Close', 'WSD return', 'B&H return'] plt.legend(lines, labels, loc='upper left', bbox_to_anchor=(0, 0.75)) plt.figure() plt.subplot(2, 1, 1) plt.step(range(len(Y_vl_int)), Y_vl_int) plt.title('Real (y)') plt.yticks([0, 1]) plt.xticks([]) plt.xlabel('') plt.subplot(2, 1, 2) plt.step(range(len(Y_vl_int)), G_vl_int) plt.title('Prediction (G)') plt.yticks([0, 1])
def cmp_netsz_iternumbers(unique_id, dp_eps_): fig = plt.figure() plt.rc('text', usetex=True) plt.rc('font', family='Times New Roman', weight='normal', size=14) plt.rcParams['mathtext.fontset'] = 'stix' # set_xscale only supported in add_subplot ax = fig.add_subplot(111) mean_cord = [] for n in size_list: # for n in [5]: q = [q_ for _ in range(n)] s = [s_ for _ in range(n)] c = [c_ for _ in range(n)] state = random_initialize(n, 50, 100) G, fig_id = gen_draw_geograph(n, set_dis(n), unique_id) y_cord = [] for _ in range(20): records = dp_avg_consensus(G, fig_id, c, q, s, state=np.matrix(state).transpose()) y_cord.append(records.shape[1]) ax.scatter([n for _ in range(20)], y_cord, marker='D', s=160, facecolors='none', edgecolors='black') mean_cord.append(np.mean(y_cord)) ax.scatter(size_list, mean_cord, marker='o', s=80, color='none', edgecolors='g', linewidth=3) ax.plot(size_list, mean_cord, c='b', lw=1) # custom legend legend_elements = [ Line2D([0], [0], c='none', marker='D', markerfacecolor='none', markeredgecolor='black', label='Iterative Times of Each Run (PE-IDA)', markersize=9), Line2D([0], [0], c='none', marker='o', markerfacecolor='none', markeredgewidth=3, markeredgecolor='g', label='Iterative Times of IDA algorithm', markersize=9) ] ax.legend(handles=legend_elements, prop={'size': 15}) plt.xticks([50, 100, 150, 200, 225, 250, 275, 300], fontsize=20, weight='bold') plt.yticks(fontsize=20, weight='bold') plt.xlabel(r"$|\mathcal{V}|$", fontsize=20) plt.ylabel(r"Iterative Times", fontsize=25, weight='bold') ax.grid(ls=':') ax.set_aspect(1.0 / ax.get_data_ratio() * 0.6) # plt.show() # plt.show() fig_id = unique_id + "_eps_" + str(dp_eps_).replace('.', '') + "_szitt" plt.savefig("../final_exp_publication/netsz_acc_itertnum/" + fig_id + '.pdf', bbox_inches="tight", pad_inches=0.01)
def cmp_netsz_accuracy(unique_id, dp_eps_): fig = plt.figure() plt.rc('text', usetex=True) plt.rc('font', family='Times New Roman', weight='normal', size=14) plt.rcParams['mathtext.fontset'] = 'stix' # set_xscale only supported in add_subplot ax = fig.add_subplot(111) mean_cord = [] for n in size_list: # for n in [5]: state = random_initialize(n, 50, 100) G, _ = gen_draw_geograph(n, set_dis(n), unique_id) y_cord = [] for _ in range(20): y_cord.append(direct_cal_diff(n, s_, c_)) ax.scatter( [n for _ in range(20)], y_cord, marker='P', s=160, # color="#007dff", # linewidth=2, color='none', edgecolors='black') mean_cord.append(np.mean(y_cord)) ax.scatter(size_list, mean_cord, marker='o', s=80, color='none', edgecolors='b', linewidth=3) ax.plot(size_list, mean_cord, c='b', lw=1) # custom legend legend_elements = [ Line2D([0], [0], c='none', marker='P', markerfacecolor='none', markeredgecolor='black', label='Error of Each Run', markersize=9), Line2D([0], [0], c='none', marker='o', markerfacecolor='none', markeredgewidth=3, markeredgecolor='b', label='Empirical Mean', markersize=9) ] ax.legend(handles=legend_elements, prop={'size': 15}) ax.grid(ls=':') plt.xticks([50, 100, 150, 200, 225, 250, 275, 300], fontsize=20, weight='bold') plt.yticks(fontsize=20, weight='bold') # plt.yticks([1, 2]) plt.xlabel(r"$|\mathcal{V}|$", fontsize=20) plt.ylabel(r"$|x^{*} - \bar{\mathbf{x}}|$", fontsize=25) ax.set_aspect(1.0 / ax.get_data_ratio() * 0.6) # plt.show() fig_id = unique_id + "_eps_" + str(dp_eps_).replace('.', '') + "_szacc" plt.savefig("../final_exp_publication/netsz_acc_itertnum/" + fig_id + '.pdf', bbox_inches="tight", pad_inches=0.01)
def main(): if len(sys.argv) < 2: print("Need file to plot as argument") return plots = [] for arg in sys.argv[1:]: if os.path.isdir(arg): l = os.listdir(arg) for plot in l: if plot.endswith(".eeg"): plots += [arg + "/" + plot] else: plots += [arg] y = 2 x = ceil(len(plots) / y) #fig, ax = plt.subplots(x, y, sharex=True) xbuf = 0 ybuf = 0 plot_count = 0 all_lines = [] longest_len = 0 longest_time_array = [] max_value = 0 min_value = 0 for plot in plots: f = open(plot, "r") lines = f.read().splitlines() f.close() time_array = [] data_array = [] offset = -1 count = 0 line_color = 'green' for line in lines: split_line = line.split(',') if (split_line[0] == "metadata"): if (split_line[1] == 'square'): line_color = 'red' elif (split_line[1] == 'circle'): line_color = 'blue' continue if offset < 0: offset = float(split_line[0]) if line_color == 'blue': time_array.append(float(split_line[0]) - offset) data_array.append(float(split_line[1])) if float(split_line[1]) > 3800 or float(split_line[1]) < 200: pass #count += 1 all_lines.append(data_array) if (len(data_array)): max_val = max(data_array) if max_val > max_value: max_value = max_val min_val = min(data_array) if min_val < min_value: min_value = min_val if len(data_array) > longest_len: longest_len = len(data_array) longest_time_array = time_array if count < len(lines) / 3: #ax[xbuf, ybuf].plot(time_array, data_array, color=line_color) plt.plot(time_array, data_array, color=line_color) plot_count += 1 xbuf += 1 if xbuf >= x: xbuf = 0 ybuf += 1 print(max_value, min_value) points_list = [] totals = [] index = 0 buffer = [] for num in range(longest_len): buffer = [] for line in all_lines: if len(line) > num: buffer.append(line[num]) points_list.append(buffer) averages = [] medians = [] q3 = [] for points in points_list: averages.append(sum(points) / len(points)) for points in points_list: points.sort() length = len(points) left = points[floor(length / 2)] right = points[ceil(length / 2)] medians.append((left + right) / 2) lower = length * 3 / 4 left = points[floor(lower)] if ceil(lower) >= length: right = points[length - 1] else: right = points[ceil(lower)] q3.append((left + right) / 2) plt.plot(longest_time_array, averages, color="red", linewidth=3) plt.plot(longest_time_array, medians, color="orange", linewidth=3) plt.plot(longest_time_array, q3, color="#18E800", linewidth=3) legend_elements = [ Line2D([0], [0], color="red", lw=3, label="gemiddelde"), Line2D([0], [0], color="orange", lw=3, label="mediaan"), Line2D([0], [0], color="#18E800", lw=3, label="q3") ] plt.legend(handles=legend_elements, loc='lower right') plt.ylim(min_value, max_value) plt.xlabel("Time") plt.ylabel("Amplitude") plt.show()
mask = np.zeros(img.shape[:2], dtype="uint8") mask[segments == segVal] = 255 # show the masked region #cv2.imshow("Mask", mask) #cv2.imshow("Applied", cv2.bitwise_and(img, img, mask = mask)) #cv2.waitKey(0) # centers centers = np.array( [np.mean(np.nonzero(segments == i), axis=1) for i in segments_ids]) vs_right = np.vstack([segments[:, :-1].ravel(), segments[:, 1:].ravel()]) vs_below = np.vstack([segments[:-1, :].ravel(), segments[1:, :].ravel()]) bneighbors = np.unique(np.hstack([vs_right, vs_below]), axis=1) fig = plt.figure(figsize=(10, 10)) ax = fig.add_subplot(111) plt.imshow(mark_boundaries(img, segments)) plt.scatter(centers[:, 1], centers[:, 0], c='y') for i in range(bneighbors.shape[1]): y0, x0 = centers[bneighbors[0, i]] y1, x1 = centers[bneighbors[1, i]] l = Line2D([x0, x1], [y0, y1], alpha=0.5) ax.add_line(l) plt.show()
def set_data(self, *args, **kwargs): Line2D.set_data(self, *args, **kwargs) self.xorig = npy.array(self._x) self.yorig = npy.array(self._y)
plt.xlabel("Hundreds of Thousands of Frames") plt.ylabel("Average Return") if ybotlim is not None: plt.ylim(bottom=ybotlim) if ytoplim is not None: plt.ylim(top=ytoplim) plt.subplots_adjust(bottom=0.17, left=0.2) plt.savefig(write_dir + "UNLABELED_{}_all_kl.png".format(env)) # plt.legend() legend_elements = [ Line2D([0], [0], marker=markers["forward"], color='black', label='Forward KL', markerfacecolor='black', markersize=markersizes["forward"], mew=mews["forward"]), Line2D([0], [0], marker=markers["reverse"], color='black', label='Reverse KL', markerfacecolor='black', markersize=markersizes["reverse"], mew=mews["reverse"]) ] plt.legend(handles=legend_elements, frameon=False) plt.savefig(write_dir + "{}_all_kl.png".format(env)) # combined sensitivity
def set_data(self, *args, **kwargs): Line2D.set_data(self, *args, **kwargs) if self._invalid: self.recache() self.xorig = np.array(self._x) self.yorig = np.array(self._y)
def run(VM_parameter_unit, threshold_percentage, uptime, min_VM, shift, provider, prediction_type, vm_price_per_hour, vm_init_data, actualFileName, scaleFileName, costFileName): listVM = list() open(scaleFileName, 'w').close() open(costFileName, 'w').close() scaleCSV = open(scaleFileName, "rw+") costCSV = open(costFileName, "rw+") scaleCSV.write("Time,VM Count\n") costCSV.write("Time,Total Cost\n") #print(provider) digix_cordinates = array.array('d') digiy_cordinates = array.array('d') x_coordinates = array.array('d') y_coordinates = array.array('d') digiy_coord_actual = array.array('d') modely_coord_actual = array.array('d') #Read vlaues while converting to VM_UNITS ifile = open(actualFileName, "rb") reader = csv.reader(ifile) rownum = 0 for row in reader: x_coordinates.append(float(row[0])) y_coordinates.append(float(row[1]) / (VM_parameter_unit)) rownum += 1 ifile.close() ##print(x_coordinates) ##print(y_coordinates) # Regression xdata = np.array(x_coordinates) ydata = np.array(y_coordinates) #Plot row data rowdata = Line2D(xdata, ydata) #plot regression line of data #plt1.plot(xdata, quad(xdata,popt[0],popt[1],popt[2], popt[3]), '-') #plot EMA line2d = Line2D(xdata, EMA.ema(ydata, 3)) #plot stratos line2d_stratos = Line2D(xdata[2:len(xdata)] + 1, Stratos.Stratos(ydata)) #Initialize min_VMs #VM = [23,42] #Todo fill with randoms for j in range(0, min_VM): # id = randint(1,999) t = vm_init_data[j] #take this as arg? vm = VM(initTime=-t, endTime=-1) listVM.append(vm) #Plot number of VMs required vm_count = min_VM yvalueset = [] if (provider == "default" and prediction_type == "reactive"): for i in drange(uptime, max(xdata) - shift + uptime - 1, 0.1): #print(i-uptime) z = getValue(line2d, i - uptime) yvalueset.append(z) ##print(z) new_vm_count = math.ceil(z / threshold_percentage) if new_vm_count < min_VM: new_vm_count = min_VM vm_change = int(math.ceil(new_vm_count - vm_count)) if vm_change > 0: vm_count += startVMs(listVM, vm_change, i) elif vm_change < 0: vm_count -= removeVMs(listVM, -vm_change, provider, i) digix_cordinates.append(i) digiy_cordinates.append(new_vm_count) digiy_coord_actual.append(vm_count) scaleCSV.seek(0, 2) scaleCSV.write("%.3f,%.3f\n" % (i, vm_count * VM_parameter_unit)) digixdata = np.array(digix_cordinates) digiydata = np.array(digiy_cordinates) actualydata = np.array(digiy_coord_actual) lineAllocate = Line2D(digixdata, digiydata) #requirement digi_line = Line2D(digixdata, actualydata) #actual elif (provider == "default" and prediction_type == "stratos"): request_count = 0 sampling_distance = 0.1 for i in drange(uptime, max(xdata) - shift + uptime - 4, sampling_distance): print(i) line2d = line2d_stratos z = getValue(line2d_stratos, i - uptime) yvalueset.append(z) ##print(z) new_vm_count = math.ceil(z / threshold_percentage) if new_vm_count < min_VM: new_vm_count = min_VM vm_change = int(math.ceil(new_vm_count - vm_count)) if vm_change > 0: vm_count += startVMs(listVM, vm_change, i) request_count = 0 elif vm_change < 0: request_count += 1 if request_count > 2.0 / sampling_distance: vm_count -= removeVMs(listVM, -vm_change, provider, i) request_count = 0 digix_cordinates.append(i) digiy_cordinates.append(new_vm_count) digiy_coord_actual.append(vm_count) scaleCSV.seek(0, 2) scaleCSV.write("%.3f,%.3f\n" % (i, vm_count * VM_parameter_unit)) digixdata = np.array(digix_cordinates) digiydata = np.array(digiy_cordinates) actualydata = np.array(digiy_coord_actual) lineAllocate = Line2D(digixdata, digiydata) #requirement digi_line = Line2D(digixdata + 4, actualydata) #actual elif (provider == "aws" and prediction_type == "stratos"): request_count = 0 sampling_distance = 0.1 for i in drange(uptime, max(xdata) - shift + uptime - 4, 0.1): print(i) line2d = line2d_stratos z = getValue(line2d, i - uptime) yvalueset.append(z) ##print(z) new_vm_count = math.ceil(z / threshold_percentage) if new_vm_count < min_VM: new_vm_count = min_VM vm_change = int(math.ceil(new_vm_count - vm_count)) if vm_change > 0: vm_count += startVMs(listVM, vm_change, i) request_count = 0 elif vm_change < 0: request_count += 1 if request_count > 2.0 / sampling_distance: vm_count -= removeVMs(listVM, -vm_change, provider, i) digix_cordinates.append(i) digiy_cordinates.append(new_vm_count) digiy_coord_actual.append(vm_count) scaleCSV.seek(0, 2) scaleCSV.write("%.3f,%.3f\n" % (i, vm_count * VM_parameter_unit)) digixdata = np.array(digix_cordinates) digiydata = np.array(digiy_cordinates) actualydata = np.array(digiy_coord_actual) lineAllocate = Line2D(digixdata, digiydata) #requirement digi_line = Line2D(digixdata + 4, actualydata) #actual elif (provider == "aws" and prediction_type == "reactive"): for i in drange(uptime, max(xdata) - shift + uptime - 1, 0.1): #print(i-uptime) z = getValue(line2d, i - uptime) yvalueset.append(z) ##print(z) new_vm_count = math.ceil(z / threshold_percentage) if new_vm_count < min_VM: new_vm_count = min_VM vm_change = int(math.ceil(new_vm_count - vm_count)) if vm_change > 0: vm_count += startVMs(listVM, vm_change, i) elif vm_change < 0: vm_count -= removeVMs(listVM, -vm_change, provider, i) digix_cordinates.append(i) digiy_cordinates.append(new_vm_count) digiy_coord_actual.append(vm_count) scaleCSV.seek(0, 2) scaleCSV.write("%.3f,%.3f\n" % (i, vm_count * VM_parameter_unit)) digixdata = np.array(digix_cordinates) digiydata = np.array(digiy_cordinates) actualydata = np.array(digiy_coord_actual) lineAllocate = Line2D(digixdata, digiydata) #requirement digi_line = Line2D(digixdata, actualydata) #actual elif (provider == "default" and prediction_type == "proactive"): digix, digiy = CostModel.run(actualFileName) for i in range(0, len(digix)): new_vm_count = digiy[i] if i != 0: vm_change = int( math.ceil(digiy[i] - len([i for x in listVM if x.endTime == -1]))) else: vm_change = int( math.ceil(digiy[0] - len([i for x in listVM if x.endTime == -1]))) if vm_change > 0: vm_count += startVMs(listVM, vm_change, digix[i]) elif vm_change < 0: vm_count -= removeVMs(listVM, -vm_change, provider, digix[i]) modely_coord_actual.append(vm_count) scaleCSV.seek(0, 2) scaleCSV.write("%.3f,%.3f\n" % (i, vm_count * VM_parameter_unit)) digixdata = np.array(digix) digiydata = np.array(digiy) lineAllocate = Line2D(digixdata, digiydata) #requirement digi_line = Line2D(digixdata, modely_coord_actual) #actual elif (provider == "aws" and prediction_type == "proactive"): digix, digiy = CostModel.run(actualFileName) #print(digix) #print(digiy) for i in range(0, len(digix)): new_vm_count = digiy[i] if i != 0: vm_change = int( math.ceil(digiy[i] - len([i for x in listVM if x.endTime == -1]))) else: vm_change = int( math.ceil(digiy[0] - len([i for x in listVM if x.endTime == -1]))) if vm_change > 0: vm_count += startVMs(listVM, vm_change, digix[i]) elif vm_change < 0: vm_count -= removeVMs(listVM, -vm_change, provider, digix[i]) modely_coord_actual.append(vm_count) scaleCSV.seek(0, 2) scaleCSV.write("%.3f,%.3f\n" % (i, vm_count * VM_parameter_unit)) digixdata = np.array(digix) digiydata = np.array(digiy) lineAllocate = Line2D(digixdata, digiydata) #requirement digi_line = Line2D(digixdata, modely_coord_actual) #actual #for vm in listVM: #print("VM_id %3s: %5.1f - %5.1f = %5.1f%s" % (vm.id, vm.initTime, vm.endTime,(vm.endTime if vm.endTime > 0 else i) - vm.initTime, ("" if vm.endTime > 0 else " up"))) costValues = array.array('d') for k in drange(0, max(xdata), 1): cost = calculateAWSCost(listVM, k, vm_price_per_hour) costValues.append(cost) costCSV.seek(0, 2) costCSV.write("%.3f,%.3f\n" % (k, cost)) costydata = np.array(costValues) costxdata = np.arange(0, max(xdata), 1) cost_line = Line2D(costxdata, costydata) yvalues = np.array(yvalueset) #e = mse(digiydata, yvalues) start = max(min(line2d.get_xdata()), min(lineAllocate.get_xdata())) end = min(max(line2d.get_xdata()), max(lineAllocate.get_xdata())) #calculateViolation(predictLine=line2d, allocateline=lineAllocate, startTime= start , endTime= end) return rowdata, line2d, digi_line, cost_line
def PCA_plot(fpkmMatrix, samples, standardize=3, log=True, show_text=False, sep='_', legend_loc='best', legend_size=14): # standardize: whether to a zscore transformation on the log10 transformed FPKM ## perform PCA variance_explained, pca_transformed = perform_PCA(fpkmMatrix, standardize=standardize, log=log) fig = plt.figure(figsize=(8, 8)) ax = fig.add_subplot(111) scatter_proxies = [] labels_show = [] groups = {} conditions = list(set([s.split(sep)[0] for s in samples])) colors = COLORS10 if len(conditions) > 10: colors = COLORS20 if len(conditions) > 20: r = lambda: random.randint(0, 255) colors = [ '#%02X%02X%02X' % (r(), r(), r()) for i in range(len(conditions)) ] for row, label in zip(pca_transformed, samples): label_show = label.split(sep)[0] idx = conditions.index(label_show) ax.scatter(row[0], row[1], label='label', color=colors[idx], s=50, marker='o') if label_show not in labels_show: labels_show.append(label_show) scatter1_proxy = Line2D([0], [0], ls="none", c=colors[idx], marker='o') scatter_proxies.append(scatter1_proxy) if show_text: ax.text(row[0], row[1]-2, label.split(sep)[1], \ ha='center', va='center', rotation=0, color=colors[idx], size='large') ax.legend(scatter_proxies, labels_show, numpoints=1, frameon=True, loc=legend_loc, prop={'size': legend_size}) ax.set_xlabel('PC1 (%.2f' % variance_explained[0] + '%' + ' variance captured)', fontsize=20) ax.set_ylabel('PC2 (%.2f' % variance_explained[1] + '%' + ' variance captured)', fontsize=20) enlarge_tick_fontsize(ax, 14) fig.tight_layout() return fig
def compile_chart(data, width, title, x, offset, tick_labels, ncol, special=None, upper_label=False, patterns=fantasy_refined, colors=colors_refined, same_color=None): fig, ax = plt.subplots() ax.set_title(title) plt.rcParams['hatch.linewidth'] = 1 handler_legend = list() shift_index = 0 for i, (label, means) in enumerate(data.items()): if special is not None and i == special: # s_ax = ax.bar(x + diff, means, width - 0.02, label=label, facecolor=colors[i], edgecolor=colors[i]) s_ax = ax.bar(x, [0] * len(means), width * len(data), bottom=means, label=label, fill=False, edgecolor="black", linestyle='--', linewidth=0.7, zorder=3) handler_legend.append( Line2D([0], [0], color="black", linestyle='--', linewidth=0.7, label=label)) shift_index = -1 else: j = i + shift_index hatch = patterns[j] * density diff = width * (j - offset) if same_color is None: s_ax = ax.bar(x + diff, means, width - 0.02, label=label, hatch=hatch, facecolor="white", edgecolor=colors[j]) else: s_ax = ax.bar(x + diff, means, width - 0.02, label=label, hatch=hatch, facecolor=same_color[0], edgecolor=same_color[1]) handler_legend.append(s_ax) if upper_label: autolabel(ax, s_ax) ax.set_ylabel('Accuracy') ax.set_xticks(x) ax.set_xticklabels(tick_labels) art = [] lgd = ax.legend(handles=handler_legend, loc=9, bbox_to_anchor=(0.5, -0.25), ncol=ncol) art.append(lgd) plt.ylim(0.8, 1) ax.set_yticks(np.arange(LOWER_BOUND_CHART, 1.01, step=0.1)) ax.set_yticks(np.arange(LOWER_BOUND_CHART, 1.001, step=0.01), minor=True) # And a corresponding grid ax.grid(which='both') # Or if you want different settings for the grids: ax.grid(which='minor', alpha=0.1) ax.grid(which='major', alpha=0.3) fig.autofmt_xdate() return art
def PCA_plot2(fpkmMatrix, color_by, shape_by, standardize=3, log=True, legend_loc='best', legend_size=14): variance_explained, pca_transformed = perform_PCA(fpkmMatrix, standardize=standardize, log=log) fig = plt.figure(figsize=(8, 8)) ax = fig.add_subplot(111) scatter_proxies = [] labels_show = [] # for legend and scatter proxies color_uniq = list(set(color_by)) shape_uniq = list(set(shape_by)) colors = COLORS10 if len(color_uniq) > 10: colors = COLORS20 if len(color_uniq) > 20: r = lambda: random.randint(0, 255) colors = [ '#%02X%02X%02X' % (r(), r(), r()) for i in range(len(color_uniq)) ] shapes = 'osv^phd' for row, label_c, label_s in zip(pca_transformed, color_by, shape_by): idx_c = color_uniq.index(label_c) idx_s = shape_uniq.index(label_s) ax.scatter(row[0], row[1], label='label', color=colors[idx_c], s=50, marker=shapes[idx_s]) label = '%s-%s' % (label_c, label_s) if label not in labels_show: labels_show.append(label) scatter1_proxy = Line2D([0], [0], ls="none", c=colors[idx_c], marker=shapes[idx_s]) scatter_proxies.append(scatter1_proxy) ax.legend(scatter_proxies, labels_show, numpoints=1, frameon=True, loc=legend_loc, prop={'size': legend_size}) ax.set_xlabel('PC1 (%.2f' % variance_explained[0] + '%' + ' variance captured)', fontsize=20) ax.set_ylabel('PC2 (%.2f' % variance_explained[1] + '%' + ' variance captured)', fontsize=20) enlarge_tick_fontsize(ax, 14) fig.tight_layout() return fig
# if you add the pure test data: Xsample=np.vstack((Xsample,Xtest_pure)) #Compute the deflated matrix newXhat=Xsample-newXdef; """ Plot overlays: """ plt.figure(4) plt.xlabel('Wavenumber [$cm^{-1}$]', fontsize=14) plt.ylabel('Counts',fontsize=14) plt.plot(wavelength,(Xsample[:,:]).T,'b',linewidth=1) #this is the mixture sample in blue plt.plot(wavelength,(newXdef[:,:]).T,'y',linewidth=1) # this is the deflation (removed background) matrix in yellow plt.plot(wavelength,(newXhat).T,'r',linewidth=1) # this is the target matrix in red legend_elements= [Line2D([0], [0], lw=3, color='b', label='original'), Line2D([0], [0], lw=3, color='y', label='subtracted'), Line2D([0], [0], lw=3, color='r', label='preprocessed')] plt.legend(handles=legend_elements, frameon=False, loc='upper right',fontsize=13) #%% plot matching spectra #normalize spectra S0mcr_normalized = np.zeros((S0mcr.shape[0],wavelength.shape[0])) X_normalized = np.zeros((S0mcr.shape[0],wavelength.shape[0])) for ii in range(0,(S0mcr.shape[0])): X_normalized[ii]=np.array([S0mcr[ii,:]/S0mcr[ii,:].max()]) S0mcr_normalized=np.array(X_normalized) Lnew_sources_normalized = np.zeros((Lnew_sources.shape[0],wavelength.shape[0])) Y_normalized = np.zeros((Lnew_sources.shape[0],wavelength.shape[0]))
def PCA_3d_plot(fpkmMatrix, samples, standardize=3, log=True, show_text=False, sep='_', legend_loc='best', legend_size=14): # standardize: whether to a zscore transformation on the log10 transformed FPKM pca = PCA(n_components=None) ## preprocessing of the fpkmMatrix if log: fpkmMatrix = np.log10(fpkmMatrix + 1.) if standardize == 2: # standardize along rows/genes fpkmMatrix = zscore(fpkmMatrix, axis=1) elif standardize == 1: # standardize along cols/samples fpkmMatrix = zscore(fpkmMatrix, axis=0) ## remove genes with NaNs fpkmMatrix = fpkmMatrix[~np.isnan(np.sum(fpkmMatrix, axis=1))] ## get variance captured pca.fit(fpkmMatrix.T) variance_explained = pca.explained_variance_ratio_[0:3] variance_explained *= 100 ## compute PCA and plot pca = PCA(n_components=3) pca_transformed = pca.fit_transform(fpkmMatrix.T) fig = plt.figure(figsize=(9, 9)) ax = fig.add_subplot(111, projection='3d') labels_show = [] scatter_proxies = [] groups = {} conditions = list(set([s.split(sep)[0] for s in samples])) colors = COLORS10 if len(conditions) > 10: colors = COLORS20 if len(conditions) > 20: r = lambda: random.randint(0, 255) colors = [ '#%02X%02X%02X' % (r(), r(), r()) for i in range(len(conditions)) ] for row, label in zip(pca_transformed, samples): label_show = label.split(sep)[0] idx = conditions.index(label_show) ax.scatter(row[0], row[1], row[2], label='label', color=colors[idx], s=50, marker='o') if label_show not in labels_show: labels_show.append(label_show) scatter1_proxy = Line2D([0], [0], ls="none", c=colors[idx], marker='o') scatter_proxies.append(scatter1_proxy) if show_text: ax.text(row[0], row[1]-5, row[2]-5, label.split(sep)[1], \ ha='center', va='center', rotation=0, color=colors[idx], size='large') ax.set_xlabel('PC1 (%.2f' % variance_explained[0] + '%' + ' variance captured)', fontsize=16) ax.set_ylabel('PC2 (%.2f' % variance_explained[1] + '%' + ' variance captured)', fontsize=16) ax.set_zlabel('PC3 (%.2f' % variance_explained[2] + '%' + ' variance captured)', fontsize=16) ax.legend(scatter_proxies, labels_show, numpoints=1, frameon=True, loc='upper left', prop={'size': legend_size}) fig.tight_layout() return fig
linewidth=2.5, marker='^', markersize=10, label='Human', linestyle=':') g = sns.pointplot(x='Timestep', y='Accuracy', data=nuc, color=color_NUC, linewidth=2.5, label='NUC', dodge=True, errwidth=1, capsize=0.1) g.set(ylim=(0.65, 1.04)) custom_lines = [ Line2D([0], [0], color=color_Human, lw=2.5, marker='^', linestyle=':'), Line2D([0], [0], color=color_Analogy, lw=2.5, marker='D', linestyle='dashed'), Line2D([0], [0], color=color_Bayesian, lw=2.5, marker='s', linestyle='-.'), Line2D([0], [0], color=color_NUC, lw=2.5, marker='o') ] g.legend(custom_lines, ['Human', 'Analogy', 'Bayesian', 'NUC'], handlelength=6) plt.ylabel('Accuracy') plt.xlabel('Timestep') plt.show()
def train_model(model, train_xs, dev_xs, args): from scipy.stats import multivariate_normal N, dimensions = train_xs.shape initials, transitions, mus, sigmas = extract_parameters(model) # ------------------- PLOTTING ----------------------------- # try: import matplotlib.pyplot as plt plt.subplot(1, 2, 1) plt.scatter(train_xs[:, 0], train_xs[:, 1], marker = ',', c = 'green', alpha = 0.2) plt.title("Cluster-Centers representation for {0} clusters and {1} Iterations-{2} in X-Space".format(args.cluster_num, args.iterations, '[Tied]' if args.tied else '[Un-Tied]')) plt.xlabel("X1") plt.ylabel("X2") plt.scatter(mus[:, 0], mus[:, 1], marker = 'v', c = 'black') except: pass # --------- PLOTTING TO BE CONTINUED IN CHUNKS -------------- # train_ll_history = [] dev_ll_history = [] #TODO: train the model, respecting args (note that dev_xs is None if args.nodev is True) # ------------------------------------------ Iterations Loop Begins ----------------------------------- # for itr in range(0, args.iterations): # ------------------- E-Step Begins (Objective: to calculate gamma-γ and psi-ξ) ------------------- # alphas, ll, em, c = forward(model, train_xs, args) betas = backward(model, train_xs, args, em) # gamma-γ Calculatons gammas = alphas * betas # Re-Normalize betasi row_norm = np.sum(gammas, axis = 1, keepdims = True) gammas = gammas/row_norm # psi-ξ calculations psi_tensor = np.zeros((args.cluster_num, args.cluster_num, N)) for t in range(1, N): for j in range(0, args.cluster_num): for k in range(0, args.cluster_num): psi_tensor[j, k, t] = alphas[t - 1, j] * transitions[j, k] * betas[t, k] * em[t, k] psi_tensor[:, :, t] /= np.sum(psi_tensor[:, :, t]) print(psi_tensor[1, 1, 500]) # ----------------------------------------- E-Step ends ------------------------------------------- # # -------- M-Step Begins (Objective: Update PI-π, A and emission params - mus, sigmas)------------- # # π-updates (Initial distributions) initials = gammas[0, :]/np.sum(gammas[0, :], keepdims = True) # A-updates (Transition probability matrix) psiSum = np.sum(psi_tensor[:, :, 1:], axis=2) transitions = psiSum / np.sum(gammas, axis = 0, keepdims = True).T # B-emission distribution parameters updates i.e. means and sigmas # μ-updates gammaSum = np.sum(gammas, axis = 0, keepdims = True) mus = np.dot(gammas.T, train_xs)/gammaSum.T # Σ-updates for k in range(0, args.cluster_num): # Plotting (This try-except code chunk can be ignored) try: plt.scatter(mus[k][0], mus[k][1], marker = 'o', c = k) except: pass # Until here # Sigma-updates [TIED] if args.tied: diff = train_xs - mus[k] sigmas += np.dot(diff.T, gammas[:, k].reshape((N, 1)) * diff) # Sigma-updates [UN-TIED] else: diff = train_xs - mus[k] sigmas[k] = np.dot(diff.T, gammas[:, k].reshape((N, 1)) * diff) sigmas[k] = sigmas[k]/np.sum(gammas[:, k]) if args.tied: sigmas = sigmas/N # ----------------------------------------- M-Step Ends ------------------------------------------- # model = {'initials' : initials, 'transitions' : transitions, 'mus' : mus, 'sigmas' : sigmas} train_ll_history.append(average_log_likelihood(model, train_xs, args)) if not args.nodev: dev_ll_history.append(average_log_likelihood(model, dev_xs, args)) # ---------------------------------------------Iterations end ------------------------------------------# # --------------------- PLOTTING ---------------------------- # # 1. Plotting Gaussian Contours for each cluster try: import matplotlib import matplotlib.cm as cm import matplotlib.mlab as mlab import matplotlib.pyplot as plt delta = 0.025 x = np.arange(-11.0, 8.0, delta) y = np.arange(-8.0, 6.0, delta) X, Y = np.meshgrid(x, y) for k in range(0, args.cluster_num): if args.tied: z_k = mlab.bivariate_normal(X, Y, sigmax = sigmas[0, 0], sigmay = sigmas[1, 1], mux = mus[k, 0], muy = mus[k, 1], sigmaxy = sigmas[0, 1]) else: z_k = mlab.bivariate_normal(X, Y, sigmax = sigmas[k][0, 0], sigmay = sigmas[k][1, 1], mux = mus[k, 0], muy = mus[k, 1], sigmaxy = sigmas[k][0, 1]) CS = plt.contour(X, Y, z_k, 6, colors='k', alpha = 0.5) plt.clabel(CS, fontsize=9, inline=1) # Add custom legend from matplotlib.patches import Patch from matplotlib.lines import Line2D legend_elements = [Line2D([0], [0], color='black', lw=1, alpha = 0.5, label='Contours'), Line2D([0], [0], marker='s', color='w', label='Data Point', markerfacecolor='green', markersize=10, alpha = 0.5), Line2D([0], [0], marker='v', color='w', label='Initial Cluster-Centers', markerfacecolor='black', markersize=10), Line2D([0], [0], marker='o', color='w', label='Cluster-centers trace', markerfacecolor='black', markersize=10)] plt.legend(handles=legend_elements, loc='lower right') # 2. Plotting Accuracy vs log-likelihood plot for training and development set plt.subplot(1, 2, 2) plt.plot(list(range(1, args.iterations + 1)), train_ll_history, label = "Train") if not args.nodev: plt.plot(list(range(1, args.iterations + 1)), dev_ll_history, label = "Dev") plt.xlabel("# iterations \n {0}".format("Max-DevLL ({1}, {0})".format(max(dev_ll_history), np.argmax(dev_ll_history)) if not args.nodev else "")) plt.ylabel("Log-Likelihood for data") plt.title("Accuracy vs Log-Likelihood for {0} clusters and {1} Iterations-{2}".format(args.cluster_num, args.iterations, '[Tied]' if args.tied else '[Un-Tied]')) plt.legend(loc = 'lower right') plt.savefig("Sub2adhikarla_hmm_gaussian-k-{0}-itr-{1}-{2}.png".format(args.cluster_num, args.iterations, 'tied' if args.tied else 'untied')) plt.show() except: pass # -------------------- PLOTTING COMPLETED --------------------- # return model
y_obs = gwl.gwl[0:-1].to_numpy() y_pred = pd.merge(dfmod, gwl, how='inner', on='date') y_pred = y_pred.model.to_numpy() plt.plot(y_obs, y_obs - y_pred, '.') #MODRE_res = pd.DataFrame({'obs': y_obs, 'pred': y_pred, 'res': y_obs-y_pred}) #MODRE_res.to_pickle('data/MODREres.pkl') table_data = [["Statistic", 'Value'], ["K (md$^{-1}$)", hk], ["S$_y$", sy], ["S$_s$", ss], ["RMSE", f'{he.rmse(y_pred,y_obs):.3}'], ["NSE", f'{he.nse(y_pred,y_obs):.3}']] leg = [ Line2D([0], [0], marker='o', color='w', label='observed head', markerfacecolor='lightseagreen'), Line2D([0], [0], linestyle='dashdot', color='red', label='modelled head'), Line2D([0], [0], color='#9467bd', label='barrage'), Line2D([0], [0], color='#1f77b4', alpha=0.5, label='precipitation') ] #set up figure fig = plt.figure(figsize=(13, 6)) gs = fig.add_gridspec(1, 3) ax1 = fig.add_subplot(gs[0, :-1]) ax2 = fig.add_subplot(gs[0, -1]) ax3 = ax1.twinx() ax1.plot(dates[1:-1], ts[0:1766, 1], ls='dashdot', color='red')
def display_ef( stocks: List[str], period: str = "3mo", n_portfolios: int = 300, risk_free: bool = False, external_axes: Optional[List[plt.Axes]] = None, ): """Display efficient frontier Parameters ---------- stocks : List[str] List of the stocks to be included in the weights period : str Time period to get returns for n_portfolios: int Number of portfolios to simulate external_axes: Optional[List[plt.Axes]] Optional axes to plot on """ if external_axes is None: _, ax = plt.subplots(figsize=plot_autoscale(), dpi=PLOT_DPI) else: ax = external_axes[0] ef, rets, stds = optimizer_model.generate_random_portfolios( stocks, period, n_portfolios) # The ef needs to be deep-copied to avoid error in plotting sharpe ef2 = copy.deepcopy(ef) sharpes = rets / stds ax.scatter(stds, rets, marker=".", c=sharpes) plotting.plot_efficient_frontier(ef, ax=ax, show_assets=True) for ticker, ret, std in zip(ef.tickers, ef.expected_returns, np.sqrt(np.diag(ef.cov_matrix))): ax.scatter(std, ret, s=50, marker=".", c="w") ax.annotate(ticker, (std * 1.01, ret)) # Find the tangency portfolio rfrate = get_rf() ef2.max_sharpe(risk_free_rate=rfrate) ret_sharpe, std_sharpe, _ = ef2.portfolio_performance( verbose=True, risk_free_rate=rfrate) ax.scatter(std_sharpe, ret_sharpe, marker="*", s=100, c="r", label="Max Sharpe") # Add risk free line if risk_free: y = ret_sharpe * 1.2 b = get_rf() m = (ret_sharpe - b) / std_sharpe x2 = (y - b) / m x = [0, x2] y = [b, y] line = Line2D(x, y, label="Capital Allocation Line") ax.set_xlim(xmin=min(stds) * 0.8) ax.add_line(line) ax.set_title(f"Efficient Frontier simulating {n_portfolios} portfolios") theme.style_primary_axis(ax) if external_axes is None: theme.visualize_output()
def set_color(self, color): self.color = color ret = Line2D.set_color(self, color) if self.canvas: self.canvas.draw() return ret
def __init__(self, **attrs): styles = {'color': '0.5', 'linestyle': '-', 'linewidth': 1} styles.update(attrs) Line2D.__init__(self, [], [], **styles)
def plot_charged_compars(*args, cut_off=2, pngname='plot_charged_compars', save=False, labels, dotsperinch=300, normalized=True, deformation_strain_end=1.72002, plot_strain_end=1.0): """ Similar to specific_cutoff_pairs for multiple simulations""" distances = args[0] strain = np.linspace(0, deformation_strain_end, len(distances.keys()) - 1) fig1 = plt.figure(figsize=(8, 8), dpi=150) ax = plt.gca() index_out = 0 ax = set_x_props(ax, 'Strain') ax.set_xlim(0, plot_strain_end) ax = set_y_props(ax, 'Number of pairs') c = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728'] colornames = ['red', 'blue', 'green', 'yellow'] while index_out < len(args): distances = args[index_out] #prop_constant=args[index_out+1] #dist_vec=args[index_out+2] if normalized: num_of_pairs_tracked = find_tracked_pairs(distances, cut_off) #num_of_pairs_hypothetical=find_hypothetical_pairs(dist_vec,distances, # prop_constant, # cut_off) num_of_pairs_cumulative = find_cumulative_pairs(distances, cut_off) ax.plot(strain, num_of_pairs_tracked, '-', color=c[index_out // 3]) ax.plot(strain, num_of_pairs_cumulative, '--', color=c[index_out // 3]) #ax.plot(strain,num_of_pairs_hypothetical,':',color=c[index_out//3]) else: num_of_pairs_tracked = find_tracked_pairs(distances, cut_off, normalized=False) #num_of_pairs_hypothetical=find_hypothetical_pairs(dist_vec,distances, # prop_constant, # cut_off, # normalized=False) num_of_pairs_cumulative = find_cumulative_pairs(distances, cut_off, normalized=False) ax.plot(strain, num_of_pairs_tracked[1:], '-', color=c[index_out // 3]) ax.plot(strain, num_of_pairs_cumulative[1:], '--', color=c[index_out // 3]) #ax.plot(strain,num_of_pairs_hypothetical,':',color=c[index_out//3]) index_out += 1 patches = {} for index in range(len(labels)): patches[index] = mpatches.Patch(color=c[index], label=labels[index]) first_legend = plt.legend( handles=[patches[0], patches[1], patches[2], patches[3]], loc='lower left') axl = plt.gca().add_artist(first_legend) custom_lines = [ Line2D([0], [0], color='k', lw=1), Line2D([0], [0], color='k', linestyle='--', lw=1), Line2D([0], [0], color='k', linestyle=':', lw=1) ] ax.legend(custom_lines, ['Tracked', 'Cumulative'], loc='lower right') title = "Cut off = " + str(cut_off)[:4] #ax.set_title(title,fontsize=20,fontfamily='Serif') props = dict(boxstyle='round', facecolor='wheat', alpha=0.5) textstr = '\n'.join( ('Perpendicular electric field', 'Electric field = 1.2')) #ax.text(0.03,0.08,textstr, transform=ax.transAxes, fontsize=14,verticalalignment='top', bbox=props) if save: plt.tight_layout() fname = imagesavepath + pngname + '_comparison_charged_cutoff_' + str( cut_off)[:4] + '.png' fig1.savefig(fname, dpi=dotsperinch)
======= def __init__(self, ticksize=None, tick_out=False, **kwargs): if ticksize is None: ticksize = rcParams['xtick.major.size'] >>>>>>> upstream/master self.set_ticksize(ticksize) self.set_tick_out(tick_out) # FIXME: tick_out is incompatible with Matplotlib tickdir option self.clear() line2d_kwargs = { 'color': rcParams['xtick.color'], 'linewidth': rcParams['xtick.major.width'] } line2d_kwargs.update(kwargs) Line2D.__init__(self, [0.], [0.], **line2d_kwargs) self.set_visible_axes('all') def set_tick_out(self, tick_out): """ set True if tick need to be rotated by 180 degree. """ self._tick_out = tick_out def get_tick_out(self): """ Return True if the tick will be rotated by 180 degree. """ return self._tick_out
def __init__(self, path, *kl, **kw): Line2D.__init__(self, [], [], *kl, **kw) self._path = path self._invalid = False
def __init__(self, mass, resistance, hooke, x0, v0, endtime, forcing_frequency, forcing_amplitude): # all inputs are ints or floats. Must have endtime>0. Physical # solutions for mass, resistance, hooke >0 # define a,b to get x''+ax'+cx=0 a = resistance / mass b = hooke / mass # name forcing coefficients for convenience omega = forcing_frequency R = forcing_amplitude # compute discriminant disc = a**2 - 4 * b # compute coefficients of particular integral (except special case) if not (disc < 0 and a == 0 and omega**2 == b): det = b**2 - b * (omega**2) + (a**2) * (omega**2) + a * (omega**3) cos_term = b * R / det sin_term = ((omega + a) * omega * R) / det # compute solution for different values of discriminant if disc > 0: # overdamped rootdisc = np.sqrt(a**2 - 4 * b) # roots of auxiliary equation mplus = (-a + rootdisc) / 2 mneg = (-a - rootdisc) / 2 # coefficients of exp terms cplus = ((v0 - sin_term * omega) - mneg * (x0 - cos_term)) / rootdisc cneg = (mplus * (x0 - cos_term) - (v0 - sin_term * omega)) / rootdisc def position(t): return (cplus * np.exp(mplus * t) + cneg * np.exp(mneg * t) + cos_term * np.cos(omega * t) + sin_term * np.sin(omega * t)) def velocity(t): return (cplus * mplus * np.exp(mplus * t) + cneg * mneg * np.exp(mneg * t) + omega * (sin_term * np.cos(omega * t) - cos_term * np.sin(omega * t))) elif disc == 0: # critically damped exp_term = x0 - cos_term # exp(t) term texp_term = v0 - sin_term * omega + a * (x0 - cos_term) / 2 # t*exp(t) def position(t): return ((exp_term + texp_term * t) * np.exp(-a * t / 2) + cos_term * np.cos(omega * t) + sin_term * np.sin(omega * t)) def velocity(t): return ( (texp_term - a * exp_term / 2 - a * texp_term * t / 2) * np.exp(-a * t / 2) + omega * (sin_term * np.cos(omega * t) - cos_term * np.sin(omega * t))) else: # underdamped rootdisc = np.sqrt(4 * b - a**2) # coefficients of trig terms csin = (2 * v0 + a * (x0 - cos_term) - 2 * sin_term * omega) / rootdisc ccos = x0 - cos_term if not (a == 0 and omega**2 == b): # not in special case def position(t): return (np.exp(-a * t / 2) * (ccos * np.cos(rootdisc * t / 2) + csin * np.sin(rootdisc * t / 2)) + cos_term * np.cos(omega * t) + sin_term * np.sin(omega * t)) def velocity(t): return ((np.exp(-a * t / 2) / 2 * ((-a * ccos + csin * rootdisc) * np.cos(rootdisc * t / 2) - ((a * csin + ccos * rootdisc) * np.sin(rootdisc * t / 2)))) + omega * (sin_term * np.cos(omega * t) - cos_term * np.sin(omega * t))) # special case where particular integral clashes with general soln; # i.e., both are non-decaying trig terms with same frequency, so # need to take linear*trig for particular integral # physically, this occurs with resonance and zero damping else: def position(t): return (x0 * np.cos(rootdisc * t / 2) + (v0 / omega) * np.sin(rootdisc * t / 2) + (R / (2 * omega)) * t * np.sin(omega * t)) def velocity(t): return ((v0 * rootdisc / (2 * omega)) * np.cos(rootdisc * t / 2) - (x0 * rootdisc / 2) * np.sin(rootdisc * t / 2) + ((R / (2 * omega)) * np.sin(omega * t) + (R / 2) * t * np.cos(omega * t))) # set up figure and subplots fig = plt.figure() ax1 = fig.add_subplot(2, 2, 1) ax2 = fig.add_subplot(2, 2, 2) ax3 = fig.add_subplot(2, 2, 3) ax4 = fig.add_subplot(2, 2, 4) fig.subplots_adjust(hspace=.5, wspace=0.5) # set up data to be plotted self.t = np.linspace(0, endtime, 400) self.x = position(self.t) self.v = velocity(self.t) self.f = -resistance * self.v - hooke * self.x + R * np.cos( omega * self.t) # set up axes and labels ax1.set_xlabel('time') ax1.set_ylabel('position') self.xgraph = Line2D([], [], color='black') self.xpoint = Line2D([], [], color='red', marker='o', markeredgecolor='r') ax1.add_line(self.xgraph) ax1.add_line(self.xpoint) ax1.set_xlim(0, endtime) ax1.set_ylim( np.amin(self.x) - 0.1 * abs(np.amin(self.x)), np.amax(self.x) + 0.1 * abs(np.amin(self.x))) ax2.set_ylabel('position') self.particle = Line2D([], [], color='red', marker='o', markeredgecolor='r') self.arrow = Line2D([], [], color='blue') ax2.add_line(self.particle) ax2.add_line(self.arrow) ax2.set_xlim(-1, 1) ax2.set_ylim( np.amin(self.x) - 0.1 * abs(np.amin(self.x)), np.amax(self.x) + 0.1 * abs(np.amin(self.x))) ax3.set_xlabel('time') ax3.set_ylabel('velocity') self.vgraph = Line2D([], [], color='black') self.vpoint = Line2D([], [], color='red', marker='o', markeredgecolor='r') ax3.add_line(self.vgraph) ax3.add_line(self.vpoint) ax3.set_xlim(0, endtime) ax3.set_ylim( np.amin(self.v) - 0.1 * abs(np.amin(self.v)), np.amax(self.v) + 0.1 * abs(np.amin(self.v))) ax4.set_xlabel('time') ax4.set_ylabel('force') self.fgraph = Line2D([], [], color='black') self.fpoint = Line2D([], [], color='red', marker='o', markeredgecolor='r') ax4.add_line(self.fgraph) ax4.add_line(self.fpoint) ax4.set_xlim(0, endtime) ax4.set_ylim( np.amin(self.f) - 0.1 * abs(np.amin(self.f)), np.amax(self.f) + 0.1 * abs(np.amin(self.f))) animation.TimedAnimation.__init__(self, fig, interval=25, blit=True)
def __init__(self, ax, *args, **kwargs): Line2D.__init__(self, *args, **kwargs) self.ax = ax
def main(): print("start script...") dataset_names=constants_cmap.ALL_DATASET_NAMES dataset= datasets.Dataset(dataset_names=dataset_names, data_type=constants_cmap.DATA_TYPE) dataloader_ctor= datasets.DataLoader(dataset, 0.2, 0.2) trainloader = dataloader_ctor.train_loader() testloader = dataloader_ctor.test_loader() # dataset_names=dataset_names_tcga+dataset_names_icgc data_train=tensor([]) labels_train=tensor([]).long() for batch_idx, (data, label) in enumerate(trainloader): data_train=torch.cat((data_train, data), 0) labels_train=torch.cat((labels_train, label), 0) n_tcga_unique_labels=len(dataset_names) data_test=tensor([]) labels_test=tensor([]).long() for batch_idx, (data, label) in enumerate(testloader): data_test=torch.cat((data_test, data), 0) labels_test=torch.cat((labels_test, label), 0) data_train=data_train.cpu().numpy() labels_train=labels_train.cpu().numpy() data_test=data_test.cpu().numpy() labels_test=labels_test.cpu().numpy() n_components=2 print("start pca...") pca = PCA(n_components=n_components).fit(data_train) X_train=pca.transform(data_train) X_test=pca.transform(data_test) print("start tsne...") y_train=labels_train y_test=labels_test # [constants.ICGC_PSEUDO_LABELS[a-n_tcga_unique_labels] for a in labels_test] knn(X_train,y_train, X_test,y_test) # X_test, y_test) fig = plt.figure(1, figsize=(20, 20)) ax = fig.add_subplot(111) X=np.vstack([X_train,X_test]) xs=X[:, 0] ys=X[:, 1] labels=np.hstack([labels_train,labels_test]) ax.scatter(xs, ys, c=labels) colormap = cm.jet plt.scatter(xs,ys, c=[a for a in labels], cmap=colormap) # sns.color_palette("Paired", n_colors=len(constants.DATASETS_INCLUDED))[a] label_unique = np.arange(len(np.unique(labels))) colorlist_unique = [ml_colors.rgb2hex(colormap(a)) for a in label_unique / float(max(labels))] patches = [Line2D([0], [0], marker='o', color='gray', label=dataset_names[a], markerfacecolor=c) for a, c in zip(label_unique, colorlist_unique)] for a in label_unique: plt.scatter([np.median([xs[i] for i, b in enumerate(labels) if a==b])],[np.median([ys[i] for i, b in enumerate(labels) if a==b])], s=2000, c=colorlist_unique[a], cmap=colormap, alpha=0.5) plt.annotate(dataset_names[a], xy=(np.median([xs[i] for i, b in enumerate(labels) if a==b]), np.median([ys[i] for i, b in enumerate(labels) if a==b])), xytext=(-20, 20), textcoords='offset points', bbox=dict(boxstyle='round,pad=0.5', fc=('yellow' if a<n_tcga_unique_labels else 'blue') , alpha=0.5), arrowprops=dict(facecolor='black', shrink=0.05, width=2, headwidth=3, headlength=2)) plt.legend(handles=patches) plt.savefig(os.path.join(constants_cmap.OUTPUT_GLOBAL_DIR, "clustering_pca_cmap_knn.png"))
ax.set_ylim(-1, 2*N+1) for i in range(0, no): if i is FcNo: nd_color = "r" elif i in BdNo: nd_color = "b" else: nd_color = "c" node_plot = patches.Circle((No[i, 0] + v[i*2], No[i,1] + v[i*2+1]), 0.1, color=nd_color) ax.add_patch(node_plot) t = plt.text(No[i,0], No[i,1], str(i)) for i in range(0, len(St)): if i in BrokenSt: st_color = "y" else: st_color = "b" strut_plot = Line2D((No[St[i, 0], 0] + v[St[i, 0] * 2], No[St[i, 1], 0] + v[St[i, 1] * 2]), (No[St[i, 0], 1] + v[St[i, 0] * 2 + 1], No[St[i, 1], 1] + v[St[i, 1] * 2 + 1]), color=st_color) ax.add_line(strut_plot) t = plt.text( No[St[i, 0], 0]+(No[St[i, 1], 0] - No[St[i, 0], 0])/2, No[St[i, 0], 1]+(No[St[i, 1], 1] - No[St[i, 0], 1])/2, str(i), color="b") plt.show()
def __init__(self, *args, **kwargs): Line2D.__init__(self, *args, **kwargs)
def __init__(self, mass, resistance, hooke, x0, v0, endtime): # all inputs are ints or floats. mass, resistance, and hooke should be # >=0 for physical meaning. endtime must be >0 # x0, v0 are initial position, velocity of particle; animation runs # from t=0 to t=endtime # define a,b to get x''+ax'+cx=0 a = resistance / mass b = hooke / mass # define discriminant disc = a**2 - 4 * b # compute position and velocity solutions for each different # possible sign of disc if disc > 0: # overdamped; exponential solutions rootdisc = np.sqrt(disc) # roots of auxiliary equation mplus = (-a + rootdisc) / 2 mneg = (-a - rootdisc) / 2 # coefficients of exp terms cplus = (v0 - mneg * x0) / rootdisc cneg = (mplus * x0 - v0) / rootdisc def position(t): return cplus * np.exp(mplus * t) + cneg * np.exp(mneg * t) def velocity(t): return cplus * mplus * np.exp( mplus * t) + cneg * mneg * np.exp(mneg * t) elif disc == 0: # critical damping; exp*linear solution def position(t): return (x0 + (v0 + a * x0 / 2) * t) * np.exp(-a * t / 2) def velocity(t): return (v0 - (a / 2) * (v0 + a * x0 / 2) * t) * np.exp(-a * t / 2) else: # underdamped; exp*(sin + cos) solutions rootdisc = np.sqrt(4 * b - a**2) # coefficients of sin and cosine terms csin = (2 * v0 + a) / rootdisc ccos = x0 def position(t): return np.exp(-a * t / 2) * (ccos * np.cos(rootdisc * t / 2) + csin * np.sin(rootdisc * t / 2)) def velocity(t): return (np.exp(-a * t / 2) * ( (-a * x0 + csin * rootdisc) * np.cos(rootdisc * t / 2) + (-a * csin - x0 * rootdisc) * np.sin(rootdisc * t / 2)) / 2) # create figure and subplots fig = plt.figure() ax1 = fig.add_subplot(2, 2, 1) ax2 = fig.add_subplot(2, 2, 2) ax3 = fig.add_subplot(2, 2, 3) ax4 = fig.add_subplot(2, 2, 4) fig.subplots_adjust(hspace=.5, wspace=0.5) # set time, position, velocity, and force data self.t = np.linspace(0, endtime, 400) self.x = position(self.t) self.v = velocity(self.t) self.f = -resistance * self.v - hooke * self.x # set axes etc. ax1.set_xlabel('time') ax1.set_ylabel('position') self.xgraph = Line2D([], [], color='black') self.xpoint = Line2D([], [], color='red', marker='o', markeredgecolor='r') ax1.add_line(self.xgraph) ax1.add_line(self.xpoint) ax1.set_xlim(0, endtime) ax1.set_ylim( np.amin(self.x) - 0.1 * abs(np.amin(self.x)), np.amax(self.x) + 0.1 * abs(np.amin(self.x))) ax2.set_ylabel('position') self.particle = Line2D([], [], color='red', marker='o', markeredgecolor='r') self.arrow = Line2D([], [], color='blue') ax2.add_line(self.particle) ax2.add_line(self.arrow) ax2.set_xlim(-1, 1) ax2.set_ylim( np.amin(self.x) - 0.1 * abs(np.amin(self.x)), np.amax(self.x) + 0.1 * abs(np.amin(self.x))) ax3.set_xlabel('time') ax3.set_ylabel('velocity') self.vgraph = Line2D([], [], color='black') self.vpoint = Line2D([], [], color='red', marker='o', markeredgecolor='r') ax3.add_line(self.vgraph) ax3.add_line(self.vpoint) ax3.set_xlim(0, endtime) ax3.set_ylim( np.amin(self.v) - 0.1 * abs(np.amin(self.v)), np.amax(self.v) + 0.1 * abs(np.amin(self.v))) ax4.set_xlabel('time') ax4.set_ylabel('force') self.fgraph = Line2D([], [], color='black') self.fpoint = Line2D([], [], color='red', marker='o', markeredgecolor='r') ax4.add_line(self.fgraph) ax4.add_line(self.fpoint) ax4.set_xlim(0, endtime) ax4.set_ylim( np.amin(self.f) - 0.1 * abs(np.amin(self.f)), np.amax(self.f) + 0.1 * abs(np.amin(self.f))) # call animation animation.TimedAnimation.__init__(self, fig, interval=20, blit=True)
def __init__(self, path, *args, **kwargs): Line2D.__init__(self, [], [], *args, **kwargs) self._path = path self._invalid = False
def __init__(self, mass, resistance, hooke, x0, v0, endtime): # inputs as above # define a,b to get x''+ax'+cx=0 a = resistance / mass b = hooke / mass g = 9.8 # discriminant disc = a**2 - 4 * b # define solutions if disc > 0: # overdamped rootdisc = np.sqrt(a**2 - 4 * b) # roots of auxiliary equation mplus = (-a + rootdisc) / 2 mneg = (-a - rootdisc) / 2 # coefficients of exp terms cplus = (v0 - mneg * (x0 + g / b)) / rootdisc cneg = (mplus * (x0 + g / b) - v0) / rootdisc def position(t): return cplus * np.exp(mplus * t) + cneg * np.exp( mneg * t) - g / b def velocity(t): return cplus * mplus * np.exp( mplus * t) + cneg * mneg * np.exp(mneg * t) elif disc == 0: # critical damping def position(t): return ((x0 + g / b) * np.exp(-a * t / 2) + (v0 + a * (x0 + g / b) / 2) * t * np.exp(-a * t / 2) - g / b) def velocity(t): return (v0 - (a / 2) * (v0 + a * (x0 + g / b) / 2) * t) * np.exp(-a * t / 2) else: # underdamped rootdisc = np.sqrt(4 * b - a**2) # trig term coefficients ccos = x0 + g / b csin = (2 * v0 + a * (x0 + g / b)) / rootdisc def position(t): return np.exp( -a * t / 2) * (ccos * np.cos(rootdisc * t / 2) + csin * np.sin(rootdisc * t / 2)) - g / b def velocity(t): return (np.exp(-a * t / 2) * ( (-a * ccos + csin * rootdisc) * np.cos(rootdisc * t / 2) + (-a * csin - ccos * rootdisc) * np.sin(rootdisc * t / 2)) / 2) # create figure and subplots fig = plt.figure() ax1 = fig.add_subplot(2, 2, 1) ax2 = fig.add_subplot(2, 2, 2) ax3 = fig.add_subplot(2, 2, 3) ax4 = fig.add_subplot(2, 2, 4) fig.subplots_adjust(hspace=.5, wspace=0.5) # set time, position, velocity, and force data self.t = np.linspace(0, endtime, 400) self.x = position(self.t) self.v = velocity(self.t) self.f = -resistance * self.v - hooke * self.x - mass * g # format axes ax1.set_xlabel('time') ax1.set_ylabel('position') self.xgraph = Line2D([], [], color='black') self.xpoint = Line2D([], [], color='red', marker='o', markeredgecolor='r') ax1.add_line(self.xgraph) ax1.add_line(self.xpoint) ax1.set_xlim(0, endtime) ax1.set_ylim( np.amin(self.x) - 0.1 * abs(np.amin(self.x)), np.amax(self.x) + 0.1 * abs(np.amin(self.x))) ax2.set_ylabel('position') self.particle = Line2D([], [], color='red', marker='o', markeredgecolor='r') self.arrow = Line2D([], [], color='blue') ax2.add_line(self.particle) ax2.add_line(self.arrow) ax2.set_xlim(-1, 1) ax2.set_ylim( np.amin(self.x) - 0.1 * abs(np.amin(self.x)), np.amax(self.x) + 0.1 * abs(np.amin(self.x)) + 0.1) ax3.set_xlabel('time') ax3.set_ylabel('velocity') self.vgraph = Line2D([], [], color='black') self.vpoint = Line2D([], [], color='red', marker='o', markeredgecolor='r') ax3.add_line(self.vgraph) ax3.add_line(self.vpoint) ax3.set_xlim(0, endtime) ax3.set_ylim( np.amin(self.v) - 0.1 * abs(np.amin(self.v)), np.amax(self.v) + 0.1 * abs(np.amin(self.v))) ax4.set_xlabel('time') ax4.set_ylabel('force') self.fgraph = Line2D([], [], color='black') self.fpoint = Line2D([], [], color='red', marker='o', markeredgecolor='r') ax4.add_line(self.fgraph) ax4.add_line(self.fpoint) ax4.set_xlim(0, endtime) ax4.set_ylim( np.amin(self.f) - 0.1 * abs(np.amin(self.f)), np.amax(self.f) + 0.1 * abs(np.amin(self.f))) # call animation animation.TimedAnimation.__init__(self, fig, interval=25, blit=True)