def plot(self, ax: plt.Axes = None, fout: str = None, aspect: int = 5, colour='k'): nodes = np.array(self.nodes) if not ax: fig, ax = plt.subplots() #plt.grid(True) ax.xaxis.set_major_locator(MultipleLocator(20)) ax.yaxis.set_major_locator(MultipleLocator(5)) ax.plot(nodes[:, 0], nodes[:, 1], 'kx', ms=0.5) ax.set_xlabel("No. of nucleotides") ax.set_ylabel("No. of strands") for edge in self.edges: ax.arrow(edge.vertices[0][0], edge.vertices[0][1], edge.vector[0], edge.vector[1], width=0.02, color=colour, length_includes_head=True) plt.gca().set_aspect(aspect) if fout: plt.savefig(fout, dpi=500) plt.show()
def _plot_arrow(self, x, y, dx, dy, style: Style, axes: plt.Axes): """Draw arrow (dx,dy) at (x,y). `style` is '->', '<-' or '<->'.""" mpl_style = MatplotlibStyle(style) if style.arrow in [Style.ArrowStyle.START, Style.ArrowStyle.DOUBLE]: axes.arrow( x, y, dx, dy, facecolor=mpl_style.line_color, edgecolor=mpl_style.line_color, linestyle=mpl_style.line_style, linewidth=mpl_style.line_width, # head_width=self.arrow_head_width, # head_width=0.1, # width=1, # width of arrow body in coordinate scale length_includes_head=True, shape="full", ) if style.arrow in [Style.ArrowStyle.END, Style.ArrowStyle.DOUBLE]: axes.arrow( x + dx, y + dy, -dx, -dy, facecolor=mpl_style.line_color, edgecolor=mpl_style.line_color, linewidth=mpl_style.line_width, head_width=0.1, # width=1, length_includes_head=True, shape="full", )
def _plot_arrow(self, x, y, dx, dy, style: Style, axes: plt.Axes): """Draw arrow (dx,dy) at (x,y). `style` is '->', '<-' or '<->'.""" if style.arrow == Style.ArrowStyle.DOUBLE: raise ValueError("Only a single ended arrow is supported by this method.") mpl_style = MatplotlibStyle(style) if style.arrow == Style.ArrowStyle.END: x = x + dx y = y + dy dx = -dx dy = -dy axes.arrow( x, y, dx, dy, facecolor=mpl_style.line_color, edgecolor=mpl_style.line_color, linestyle=mpl_style.line_style, linewidth=mpl_style.line_width, head_width=0.05, # head_width=self.arrow_head_width, # width=1, # width of arrow body in coordinate scale length_includes_head=True, shape="full", )
def v_arrow( axes_: Axes, xy_pt1: np.ndarray, xy_pt2: np.ndarray, dxy: Tuple[float, float], a_f: float = 0.54, v_f: float = 0.5, v_label: str = r"$\mathbf{v}$", color_: str = red_, do_dashing: bool = False, do_label: bool = False, ) -> None: v_lw = 1.5 axes_.arrow( *((xy_pt1 * a_f + xy_pt2 * (1 - a_f))), *((xy_pt1 - xy_pt2) * 0.01), lw=1, facecolor=color_, edgecolor=color_, head_width=0.05, overhang=0.3, transform=axes_.transAxes, length_includes_head=True, ) axes_.plot( [xy_pt1[0], xy_pt2[0]], [xy_pt1[1], xy_pt2[1]], ls="--" if do_dashing else "-", lw=v_lw, color=color_, ) f = v_f v_xy = ( xy_pt1[0] * f + xy_pt2[0] * (1 - f) + dxy[0], xy_pt1[1] * f + xy_pt2[1] * (1 - f) + dxy[1], ) if do_label: axes_.text( *v_xy, v_label, color=color_, fontsize=18, rotation=0, transform=axes_.transAxes, horizontalalignment="right", verticalalignment="bottom", )
def visualize_countries(model: Word2Vec, vocabulary: Vocabulary, ax: plt.Axes = None): countries = [ 'u.s.', 'u.k.', 'italy', 'korea', 'china', 'germany', 'japan', 'france', 'russia', 'egypt' ] capitals = [ 'washington', 'london', 'rome', 'seoul', 'beijing', 'berlin', 'tokyo', 'paris', 'moscow', 'cairo' ] vectors_2d = project_to_2d_by_pca(model, vocabulary) if ax is None: fig, ax = plt.subplots() else: fig = None # Plot countries country_ids = [vocabulary.to_id(word) for word in countries] country_vectors = vectors_2d[country_ids] ax.scatter(country_vectors[:, 0], country_vectors[:, 1], c='blue', alpha=0.7) for i, label in enumerate(countries): ax.annotate(label, (country_vectors[i, 0], country_vectors[i, 1])) # Plot capitals capital_ids = [vocabulary.to_id(word) for word in capitals] capital_vectors = vectors_2d[capital_ids] ax.scatter(capital_vectors[:, 0], capital_vectors[:, 1], c='orange', alpha=0.7) for i, label in enumerate(capitals): ax.annotate(label, (capital_vectors[i, 0], capital_vectors[i, 1])) # Draw arrows for country, capital in zip(countries, capitals): v1 = vectors_2d[vocabulary.to_id(country)] v2 = vectors_2d[vocabulary.to_id(capital)] ax.arrow(v1[0], v1[1], (v2 - v1)[0], (v2 - v1)[1], alpha=0.5) if fig is not None: fig.show()
def plot_track(self, frame_idx: int, ax: plt.Axes, max_track_idx: int, arrow_scale: float = 4.0): """ Plot the track on this frame :param int frame_idx: The frame index to plot the track for :param Axes ax: The matplotlib axis to plot this track on :param int max_track_idx: Maximum track index to plot (sets this track's color) :param float arrow_scale: Scale factor to exaggerate the velocity arrows by """ # Only plot frames where this track is found if frame_idx not in self.frames: return # Find the index in this track that corresponds to the global frame number this_frame = self.frames.index(frame_idx) # Work out the color for this track using the track index and maximum track number cmap = copy.copy(get_cmap('Set1')) cmap.set_under('black') rgba = cmap((self.track_idx - 1)/(max_track_idx)) # Load the data for this frame cx, cy = self.centers[this_frame] perimeter = self.perimeters[this_frame] # Try to calculate velocity, if we can dx, dy = self.calc_delta(frame_idx) # Plot center of mass and perimeter ax.plot([cy], [cx], '.', color=rgba, linestyle='') ax.plot(perimeter[:, 1], perimeter[:, 0], color=rgba, linestyle='-', linewidth=2) # Plot velocity arrows, if available if dx is not None and dy is not None: ax.arrow(cy, cx, dy*arrow_scale, dx*arrow_scale, color=rgba, linestyle='-', linewidth=2, width=0.01)
def render_bev_labels_mpl( self, city_name: str, ax: plt.Axes, axis: str, lidar_pts: np.ndarray, local_lane_polygons: np.ndarray, local_das: np.ndarray, log_id: str, timestamp: int, city_to_egovehicle_se3: SE3, avm: ArgoverseMap, ) -> None: """Plot nearby lane polygons and nearby driveable areas (da) on the Matplotlib axes. Args: city_name: The name of a city, e.g. `"PIT"` ax: Matplotlib axes axis: string, either 'ego_axis' or 'city_axis' to demonstrate the lidar_pts: Numpy array of shape (N,3) local_lane_polygons: Polygons representing the local lane set local_das: Numpy array of objects of shape (N,) where each object is of shape (M,3) log_id: The ID of a log timestamp: In nanoseconds city_to_egovehicle_se3: Transformation from egovehicle frame to city frame avm: ArgoverseMap instance """ if axis is not "city_axis": # rendering instead in the egovehicle reference frame for da_idx, local_da in enumerate(local_das): local_da = city_to_egovehicle_se3.inverse_transform_point_cloud( local_da) local_das[da_idx] = rotate_polygon_about_pt( local_da, city_to_egovehicle_se3.rotation, np.zeros(3)) for lane_idx, local_lane_polygon in enumerate(local_lane_polygons): local_lane_polygon = city_to_egovehicle_se3.inverse_transform_point_cloud( local_lane_polygon) local_lane_polygons[lane_idx] = rotate_polygon_about_pt( local_lane_polygon, city_to_egovehicle_se3.rotation, np.zeros(3)) draw_lane_polygons(ax, local_lane_polygons) draw_lane_polygons(ax, local_das, color="tab:pink") if axis is not "city_axis": lidar_pts = rotate_polygon_about_pt( lidar_pts, city_to_egovehicle_se3.rotation, np.zeros((3, ))) draw_point_cloud_bev(ax, lidar_pts) objects = self.log_timestamp_dict[log_id][timestamp] all_occluded = True for frame_rec in objects: if frame_rec.occlusion_val != IS_OCCLUDED_FLAG: all_occluded = False if not all_occluded: for i, frame_rec in enumerate(objects): bbox_city_fr = frame_rec.bbox_city_fr bbox_ego_frame = frame_rec.bbox_ego_frame color = frame_rec.color if frame_rec.occlusion_val != IS_OCCLUDED_FLAG: bbox_ego_frame = rotate_polygon_about_pt( bbox_ego_frame, city_to_egovehicle_se3.rotation, np.zeros((3, ))) if axis is "city_axis": plot_bbox_2D(ax, bbox_city_fr, color) if self.plot_lane_tangent_arrows: bbox_center = np.mean(bbox_city_fr, axis=0) tangent_xy, conf = avm.get_lane_direction( query_xy_city_coords=bbox_center[:2], city_name=city_name) dx = tangent_xy[0] * LANE_TANGENT_VECTOR_SCALING dy = tangent_xy[1] * LANE_TANGENT_VECTOR_SCALING ax.arrow(bbox_center[0], bbox_center[1], dx, dy, color="r", width=0.5, zorder=2) else: plot_bbox_2D(ax, bbox_ego_frame, color) cuboid_lidar_pts, _ = filter_point_cloud_to_bbox_2D_vectorized( bbox_ego_frame[:, :2], copy.deepcopy(lidar_pts)) if cuboid_lidar_pts is not None: draw_point_cloud_bev(ax, cuboid_lidar_pts, color) else: logger.info(f"all occluded at {timestamp}") xcenter = city_to_egovehicle_se3.translation[0] ycenter = city_to_egovehicle_se3.translation[1] ax.text(xcenter - 146, ycenter + 50, "ALL OBJECTS OCCLUDED", fontsize=30)