def render(self, mode='png', name=''): fig, ax = plt.subplots(dpi=100) # plot boundary rads = np.linspace(0, 2 * np.pi, 100) ax.plot(np.cos(rads), np.sin(rads)) ax.set_aspect('equal', 'box') # plot view ax.add_artist(mpatches.Arc((0,0),0.7,0.7,theta1=(self.view_angle-self.sight_range/2)*180/np.pi, \ theta2=self.view_angle*180/np.pi, ec='y')) ax.add_artist(mpatches.Arc((0,0),0.7,0.7,theta1=self.view_angle*180/np.pi, \ theta2=(self.view_angle+self.sight_range/2)*180/np.pi, ec='g')) # plot prey if (self.prey): ax.scatter(self.prey_location[0], self.prey_location[1]) ax.plot([self.prey_location[0], self.prey_location[0] + 3 * self.prey_speed * self.prey_dirn_vec[0]], \ [self.prey_location[1], self.prey_location[1] + 3 * self.prey_speed * self.prey_dirn_vec[1]], \ 'k--') # plot animal ax.add_artist(plt.Circle((0, 0), 0.1)) left_angle = self.view_angle - np.pi / 6 right_angle = self.view_angle + np.pi / 6 ax.plot([0, np.cos(left_angle)], [0, np.sin(left_angle)], 'k--') ax.plot([0, np.cos(right_angle)], [0, np.sin(right_angle)], 'k--') if (mode == 'human'): plt.show() elif (mode == 'png'): filename = 'render' + name + '.png' plt.title(name) plt.savefig(filename) plt.close()
def animate(i): thisx = [0, x1[i], x2[i]] thisy = [0, y1[i], y2[i]] line.set_data(thisx, thisy) time_text.set_text(time_template % (i * dt)) time.append(i * dt) angle1 = math.degrees(np.arctan2(x1[i], y1[i])) angle2 = math.degrees(np.arctan2(x2[i] - x1[i], y2[i] - y1[i])) if angle1 > 0: c1 = pat.Arc((0, 0), L1, L1, 90, 180, -angle1) else: c1 = pat.Arc((0, 0), L1, L1, 90, -angle1, 180) if angle2 > 0: c2 = pat.Arc((x1[i], y1[i]), L2, L2, 90, 180, -angle2) else: c2 = pat.Arc((x1[i], y1[i]), L2, L2, 90, -angle2, 180) ax.add_patch(c1) ax.add_patch(c2) angle1_raw.append(angle1) angle2_raw.append(angle2) return line, time_text, c1, c2
def test_arc_ellipse(): from matplotlib import patches xcenter, ycenter = 0.38, 0.52 width, height = 1e-1, 3e-1 angle = -30 theta = np.arange(0.0, 360.0, 1.0)*np.pi/180.0 x = width/2. * np.cos(theta) y = height/2. * np.sin(theta) rtheta = angle*np.pi/180. R = np.array([ [np.cos(rtheta), -np.sin(rtheta)], [np.sin(rtheta), np.cos(rtheta)], ]) x, y = np.dot(R, np.array([x, y])) x += xcenter y += ycenter fig = plt.figure() ax = fig.add_subplot(211, aspect='auto') ax.fill(x, y, alpha=0.2, facecolor='yellow', edgecolor='yellow', linewidth=1, zorder=1) e1 = patches.Arc((xcenter, ycenter), width, height, angle=angle, linewidth=2, fill=False, zorder=2) ax.add_patch(e1) ax = fig.add_subplot(212, aspect='equal') ax.fill(x, y, alpha=0.2, facecolor='green', edgecolor='green', zorder=1) e2 = patches.Arc((xcenter, ycenter), width, height, angle=angle, linewidth=2, fill=False, zorder=2) ax.add_patch(e2)
def plot_group_arc(self, node_table, ax): params = { "lw": 3, "fc": "white", "alpha": 0.3, "zorder": 0, "angle": 90 } a3 = patches.Arc( (0, 0), 2 * self.radius * (self.group_arc_node_margin + 1), 2 * self.radius * (self.group_arc_node_margin + 1), theta1=-180 * self.angles["reciprocal"]["end"] / np.pi, theta2=-180 * self.angles["source"]["start"] / np.pi, ec="black", ls="-", **params) ax.add_patch(a3) a3 = patches.Arc( (0, 0), 2 * self.radius * (self.group_arc_node_margin + 1.05), 2 * self.radius * (self.group_arc_node_margin + 1.05), theta2=-180 * self.angles["reciprocal"]["start"] / np.pi, theta1=-180 * self.angles["target"]["end"] / np.pi, ec="black", ls="--", **params) ax.add_patch(a3)
def plot(self, ax): width, height, r = self.width, self.height, self.radius # config ax ax.set_xlim([0, width]) ax.set_ylim([0, height]) # entities lbasket = self.basket rbasket = np.array([self.width - self.basket[0], self.basket[1]]) lchoop = plt.Circle(lbasket, 1.5/2, fill=False, color="k") rchoop = plt.Circle(rbasket, 1.5/2, fill=False, color="k") lc3pt = patches.Arc(lbasket, r*2, r*2, theta1=-68.31, theta2=68.31) rc3pt = patches.Arc(rbasket, r*2, r*2, theta1=180-68.31, theta2=180+68.31) # entities to plot lhoop = ax.add_artist(lchoop) # the basket left rhoop = ax.add_artist(rchoop) # the basket right l3pt = ax.add_patch(lc3pt) # 3 pointer line r3pt = ax.add_patch(rc3pt) # 3 pointer right llt = ax.plot([0, 14], [47, 47], "k") # top 3pt line left rlt = ax.plot([width, width-14], [47, 47], "k") # top 3pt line right llb = ax.plot([0, 14], [3, 3], "k") # bottom 3pt line left rlb = ax.plot([width, width-14], [3, 3], "k") # bottom 3pt line right lft = ax.plot([19, 19], [17, 33], "k") # left free throw line rft = ax.plot([width-19, width-19], [17, 33], "k") # right free throw line hc = ax.plot([46.998, 46.998], [0, height], "k") # half court line return (lhoop, l3pt, llt, llb, lft, rhoop, r3pt, rlt, rlb, rft, hc)
def draw_loop( center, width=1, height=1, ): # Create loop inner_width, inner_height = 0.9 * width, 0.9 * height arc1 = patches.Arc(center, width, height, theta1=90, theta2=180, **patch_kwargs) arc2 = patches.Arc(center, inner_width, inner_height, theta1=90, theta2=180, **patch_kwargs) cap_width = 0.1 * width / 2 cap_height = cap_width / 2 cap1 = patches.Ellipse( (center[0] - (width / 2 + inner_width / 2) / 2, center[1]), cap_width, cap_height, **patch_kwargs) cap2 = patches.Ellipse( (center[0], center[1] + (height / 2 + inner_height / 2) / 2), cap_height, cap_width, **patch_kwargs) # Draw loop ax.add_patch(arc1) ax.add_patch(arc2) ax.add_patch(cap1) ax.add_patch(cap2)
def world_frame_rotation(): plot = WorldFramePlotter("Rotation between coordinate frames A and B", "W", (0, 100), (0, 100), 0.04) frame1_colors = (plot.colormap[3], plot.colormap[2]) origin = np.array((50, 50)) plot.add_coordinate_frame("A", 0, origin, 20, frame1_colors) frame2_colors = (plot.colormap[5], plot.colormap[4]) plot.add_coordinate_frame("B", 33, origin, 20, frame2_colors, False) arcx = mpatches.Arc(origin, 25, 25, theta2=33, ls=(0, (5, 5)), alpha=0.4) plot.ax.add_patch(arcx) arcy = mpatches.Arc(origin, 25, 25, theta1=90, theta2=123, ls=(0, (5, 5)), alpha=0.4) plot.ax.add_patch(arcy) plot.ax.text(63, 53, r"$\theta$", fontsize=18, alpha=0.4) plot.ax.text(45, 63, r"$\theta$", fontsize=18, alpha=0.4) print_offset = plot.print_offset(plot.ax.get_xlim()) plot.ax.text(origin[0] - print_offset, origin[1] - 2.5 * print_offset, r"$O_B$", fontsize=22) plot.show()
def courtPlot(ax): # basic parameters width = 93.996 height = 50 r = 23.75 # config ax ax.set_xlim([0, width]) ax.set_ylim([0, height]) # entities lbasket = (5.2493, 25) rbasket = (width-5.2493, 25) lchoop = plt.Circle(lbasket, 1.5/2, fill=False, color="k") rchoop = plt.Circle(rbasket, 1.5/2, fill=False, color="k") lc3pt = patches.Arc(lbasket, r*2, r*2, theta1=-68.31, theta2=68.31) rc3pt = patches.Arc(rbasket, r*2, r*2, theta1=180-68.31, theta2=180+68.31) # entities to plot # court lhoop = ax.add_artist(lchoop) rhoop = ax.add_artist(rchoop) l3pt = ax.add_patch(lc3pt) r3pt = ax.add_patch(rc3pt) llt = ax.plot([0, 14], [47, 47], "k") rlt = ax.plot([width, width-14], [47, 47], "k") llb = ax.plot([0, 14], [3, 3], "k") rlb = ax.plot([width, width-14], [3, 3], "k") lft = ax.plot([19, 19], [17, 33], "k") rft = ax.plot([width-19, width-19], [17, 33], "k") hc = ax.plot([46.998, 46.998], [0, height], "k") return (lhoop, l3pt, llt, llb, lft, rhoop, r3pt, rlt, rlb, rft, hc)
def featureCircle(fig,features,totlen=mt_len,radi=1.1,colorset={'rep_origin':'green','rRNA':'blue','tRNA':'magenta','CDS':'orange'},clockwise=1): ax = fig.gca() codons = [] for f in features: dx = 0 if totlen > 85779: if f.location.start.position>30000: dx = totlen-85779 if f.type in colorset.keys(): t1 = (f.location.start.position+dx)/(totlen+1)*360 t2 = (f.location.end.position+dx)/(totlen+1)*360 if clockwise: tp1 = t1 t1 = 90-t2 t2 = 90-tp1 ac1 = patches.Arc((0,0),2*radi,2*radi,theta1=t1,theta2=t2,color=colorset[f.type],linewidth=2) ax.add_patch(ac1) if f.type=='CDS': for p in f.location.parts: pos = [p.start.position+dx,p.end.position+dx] if pos not in codons: codons.append(pos) #if f.qualifiers['gene'][0]=='COX1': for pos in codons: t1 = pos[0]/(totlen+1)*360 t2 = pos[1]/(totlen+1)*360 if clockwise: tp1 = t1 t1 = 90-t2 t2 = 90-tp1 ac1 = patches.Arc((0,0),2*radi,2*radi,theta1=t1,theta2=t2,color='red',linewidth=2) ax.add_patch(ac1) return fig
def plotCircle(fig,rpairs,totlen=mt_len,shif=0.2,alpha=0.2,color=['C6','C9'],ellips=1,clockwise=1,wt=[]): ax = fig.gca() #fig.add_subplot(111, aspect='auto') ac0 = patches.Arc((0,0),2,2,angle=0,color='black',linewidth=2) ax.add_patch(ac0) for i,r in enumerate(rpairs): r1 = abs(r[0]) r2 = abs(r[1]) if np.sign(r[0]*r[1])>0: cid = 0 else: cid = 1 t1 = r1/(totlen+1)*2*np.pi t2 = r2/(totlen+1)*2*np.pi if clockwise: t1 = np.pi/2-t1 t2 = np.pi/2-t2 x1 = np.cos(t1) y1 = np.sin(t1) x2 = np.cos(t2) y2 = np.sin(t2) x12 = (x1+x2)/2 y12 = (y1+y2)/2 r12 = np.sqrt(x12**2+y12**2) x0 = x12/r12*(1+shif) y0 = y12/r12*(1+shif) angle = 180*np.arctan(y0/x0)/np.pi x01 = x1-x0 y01 = y1-y0 xp = (x01*x0+y01*y0)/(1+shif) yp2 = max(x01**2+y01**2-xp**2,1e-5) rp = np.sqrt(xp**2+yp2) if ellips: a = 2*shif+(1-shif)*(rp-shif)/(np.sqrt(1+(1+shif)**2)-shif) b = np.sqrt(yp2/(1-xp**2/a**2)) else: if rp<(1+shif): a = rp b = rp else: a = 1+shif b = np.sqrt(yp2/(1-xp**2/a**2)) theta = 180*np.arctan(-np.sqrt(yp2)/abs(xp)*a/b)/np.pi if x0>0: theta1 = 180+theta theta2 = 180-theta else: theta1 = theta theta2 = -theta if len(wt)==0: ac1 = patches.Arc((x0,y0),2*a,2*b,angle=angle,theta1=theta1,theta2=theta2,alpha=alpha,color=color[cid],linewidth=2) else: ac1 = patches.Arc((x0,y0),2*a,2*b,angle=angle,theta1=theta1,theta2=theta2,alpha=wt[i]/max(wt),color=color[cid],linewidth=2) ax.add_patch(ac1) ax.axis('square') ax.set_xlim(-1.2,1.2) ax.set_ylim(-1.2,1.2) return fig
def draw_arc(center, radius, thetas=None): if thetas == None: arc = patches.Arc(center, radius * 2, radius * 2) else: arc = patches.Arc(center, radius * 2, radius * 2, theta1=deg(thetas[0]), theta2=deg(thetas[1])) ax.add_patch(arc)
def draw_track(L1, w, R1, alpha, L2): plt.figure() ax = plt.gca() ax.axis('equal') ax.axis([0, 200, -100, 100]) R11 = R1 - w / 2.0 R12 = R1 + w / 2.0 straight11 = lines.Line2D([0.0, L1], [w / 2.0, w / 2.0], color='black', linewidth=2) straight12 = lines.Line2D([0.0, L1], [-w / 2.0, -w / 2.0], color='black', linewidth=2) int_edge1 = patches.Arc((L1, R1), 2 * (R11), 2 * (R11), 270.0, 0.0, alpha, linewidth=2) ext_edge1 = patches.Arc((L1, R1), 2 * (R12), 2 * (R12), 270.0, 0.0, alpha, linewidth=2) x21_min = L1 + 2 * (R11) * np.sin(np.deg2rad(alpha / 2.0)) * np.cos( np.deg2rad(alpha / 2.0)) x21_max = x21_min + L2 * np.cos(np.deg2rad(alpha)) y21_min = w / 2.0 + 2 * R11 * np.square(np.sin(np.deg2rad(alpha / 2.0))) y21_max = y21_min + L2 * np.sin(np.deg2rad(alpha)) x22_min = L1 + 2 * (R12) * np.sin(np.deg2rad(alpha / 2.0)) * np.cos( np.deg2rad(alpha / 2.0)) x22_max = x22_min + L2 * np.cos(np.deg2rad(alpha)) y22_min = -w / 2.0 + 2 * R12 * np.square(np.sin(np.deg2rad(alpha / 2.0))) y22_max = y22_min + L2 * np.sin(np.deg2rad(alpha)) straight21 = lines.Line2D([x21_min, x21_max], [y21_min, y21_max], color='black', linewidth=2) straight22 = lines.Line2D([x22_min, x22_max], [y22_min, y22_max], color='black', linewidth=2) ax.add_line(straight11) ax.add_line(straight12) ax.add_patch(int_edge1) ax.add_patch(ext_edge1) ax.add_line(straight21) ax.add_line(straight22)
def futsal_pitch_h(ax): """ BEFORE fig = plt.figure(figsize=(10, 6)) ax = plt.axes() AFTER plt.show() """ center_circle = mpatches.Circle(xy=(0, 0), radius=3, ec="k", fill=False) dot1 = mpatches.Circle(xy=(-14, 0), radius=0.1, ec="k", fill=True) dot2 = mpatches.Circle(xy=(-10, 0), radius=0.1, ec="k", fill=True) dot3 = mpatches.Circle(xy=(0, 0), radius=0.1, ec="k", fill=True) dot4 = mpatches.Circle(xy=(10, 0), radius=0.1, ec="k", fill=True) dot5 = mpatches.Circle(xy=(14, 0), radius=0.1, ec="k", fill=True) pitch = mpatches.Rectangle(xy=(-20, -12), width=40, height=24, ec="k", fill=False) half = mpatches.Rectangle(xy=(0, -12), width=0, height=24, ec="k", fill=False) goal1 = mpatches.Rectangle(xy=(-21, -1.5), width=1, height=3, ec="k", fill=False) goal2 = mpatches.Rectangle(xy=(20, -1.5), width=1, height=3, ec="k", fill=False) goal_area1 = mpatches.Arc(xy=(-20, 1.5), width=12, height=12, theta1=0, theta2=90, ec="k") goal_area2 = mpatches.Arc(xy=(-20, -1.5), width=12, height=12, theta1=-90, theta2=0, ec="k") goal_area3 = mpatches.Rectangle(xy=(-14, -1.5), width=0, height=3, ec="k") goal_area4 = mpatches.Arc(xy=(20, 1.5), width=12, height=12, theta1=90, theta2=180, ec="k") goal_area5 = mpatches.Arc(xy=(20, -1.5), width=12, height=12, theta1=180, theta2=270, ec="k") goal_area6 = mpatches.Rectangle(xy=(13.9, -1.5), width=0, height=3, ec="k") sub1 = mpatches.Rectangle(xy=(-10, -12.5), width=0, height=1, ec="k") sub2 = mpatches.Rectangle(xy=(-5, -12.5), width=0, height=1, ec="k") sub3 = mpatches.Rectangle(xy=(5, -12.5), width=0, height=1, ec="k") sub4 = mpatches.Rectangle(xy=(10, -12.5), width=0, height=1, ec="k") ax.add_patch(center_circle) ax.add_patch(dot1) ax.add_patch(dot2) ax.add_patch(dot3) ax.add_patch(dot4) ax.add_patch(dot5) ax.add_patch(pitch) ax.add_patch(half) ax.add_patch(goal1) ax.add_patch(goal2) ax.add_patch(goal_area1) ax.add_patch(goal_area2) ax.add_patch(goal_area3) ax.add_patch(goal_area4) ax.add_patch(goal_area5) ax.add_patch(goal_area6) ax.add_patch(sub1) ax.add_patch(sub2) ax.add_patch(sub3) ax.add_patch(sub4) ax.xaxis.set_major_formatter(mpl.ticker.NullFormatter()) ax.yaxis.set_major_formatter(mpl.ticker.NullFormatter()) plt.axis("scaled") ax.set_aspect("equal")
def AddZone(self, xy, radius, color): circlePatch = patches.Arc((xy[0], xy[1]), width=2 * radius, height=2 * radius, fill=False, color=color) circlePatch_zoom = patches.Arc((xy[0], xy[1]), width=2 * radius, height=2 * radius, fill=False, color=color) self.ax.add_patch(circlePatch) self.axins.add_patch(circlePatch_zoom)
def plot_sheet_bending(curvature=0.001, l0=10, t=2): if curvature == 0: curvature = 1e-10 rho = 1 / curvature Ri = rho - t / 2 Re = rho + t / 2 alfa = l0 / rho alfaG = np.rad2deg(alfa) fig, ax = plt.subplots() p0x, p0y = 0, -rho / 2 p1x, p1y = Ri / 2 * math.sin( -alfa / 2), -rho / 2 + Ri / 2 * math.cos(-alfa / 2) p2x, p2y = Re / 2 * math.sin( -alfa / 2), -rho / 2 + Re / 2 * math.cos(-alfa / 2) p3x, p3y = Re / 2 * math.sin( alfa / 2), -rho / 2 + Re / 2 * math.cos(alfa / 2) p4x, p4y = Ri / 2 * math.sin( alfa / 2), -rho / 2 + Ri / 2 * math.cos(alfa / 2) arc = patches.Arc([p0x, p0y], Ri, Ri, angle=90, theta1=-alfaG / 2, theta2=alfaG / 2) ax.add_patch(arc) arc = patches.Arc([p0x, p0y], Re, Re, angle=90, theta1=-alfaG / 2, theta2=alfaG / 2) ax.add_patch(arc) ax.plot([p0x, p1x], [p0y, p1y], 'k:') ax.plot([p1x, p2x], [p1y, p2y], 'k-') ax.plot([p4x, p0x], [p4y, p0y], 'k:') ax.plot([p3x, p4x], [p3y, p4y], 'k-') ax.text(0, -1, r'$l_0 = %.0f$ mm' % l0, horizontalalignment='center') ax.text(0, -1.5, r'$R_i = %.0f$ mm, $R_e = %.0f$ mm' % (rho, rho + t), horizontalalignment='center') ax.text(0, -2, r'$\alpha = l_0/R = %.0f^{\circ}$' % alfaG, horizontalalignment='center') ax.axis([-5, 5, -2, 1]) ax.set_axis_off() ax.set_aspect("equal") plt.show()
def draw_geodesic(a, b, c, ax, verbose=False): if verbose: print("Geodesic points are ", a, "\n", b, "\n", c, "\n") is_collinear = False if collinear(a, b, c): is_collinear = True else: cent = get_circle_center(a, b, c) radius = euclid_dist(a, cent) t1 = get_angles(cent, b) t2 = get_angles(cent, a) if verbose: print("\ncenter at ", cent) print("radius is ", radius) print("angles are ", t1, " ", t2) print("dist(a,center) = ", euclid_dist(cent, a)) print("dist(b,center) = ", euclid_dist(cent, b)) print("dist(c,center) = ", euclid_dist(cent, c)) # if the angle is really tiny, a line is a good approximation if is_collinear or (np.abs(t1 - t2) < 2): coordsA = "data" coordsB = "data" e = patches.ConnectionPatch(a, b, coordsA, coordsB, linewidth=2) else: if verbose: print("angles are theta_1 = ", t1, " theta_2 = ", t2) if (t2 > t1 and t2 - t1 < 180) or (t1 > t2 and t1 - t2 >= 180): e = patches.Arc((cent[0], cent[1]), 2 * radius, 2 * radius, theta1=t1, theta2=t2, linewidth=2, fill=False, zorder=2) else: e = patches.Arc((cent[0], cent[1]), 2 * radius, 2 * radius, theta1=t2, theta2=t1, linewidth=2, fill=False, zorder=2) ax.add_patch(e) ax.plot(a[0], a[1], "o") ax.plot(b[0], b[1], "o")
def make_semicircle(x1: float, x2: float, y: float, valley: bool, color: str, opacity: float = 1., linewidth: float = 1.): """ Makes a semicircular link between (x1, y) and (x2, y) Parameters ---------- x1 x2 y valley if True, ∪ else ∩ color opacity linewidth Returns ------- maximum height of the semicircle (to use when deciding the axis limit), matplotlib patch """ middle = (x1 + x2) / 2 height = 2 * np.abs(x1 - middle) if height == 0: return 0, None if valley: return -height, m_patches.Arc((middle, y), -height, -height, angle=-180, theta1=180, theta2=360, lw=linewidth, color=color, alpha=opacity) else: return height, m_patches.Arc((middle, y), height, height, angle=0, theta1=0, theta2=180, lw=linewidth, color=color, alpha=opacity)
def test_rotated_arcs(): fig, ax_arr = plt.subplots(2, 2, squeeze=False, figsize=(10, 10)) scale = 10_000_000 diag_centers = ((-1, -1), (-1, 1), (1, 1), (1, -1)) on_axis_centers = ((0, 1), (1, 0), (0, -1), (-1, 0)) skews = ((2, 2), (2, 1 / 10), (2, 1 / 100), (2, 1 / 1000)) for ax, (sx, sy) in zip(ax_arr.ravel(), skews): k = 0 for prescale, centers in zip((1 - .0001, (1 - .0001) / np.sqrt(2)), (on_axis_centers, diag_centers)): for j, (x_sign, y_sign) in enumerate(centers, start=k): a = mpatches.Arc( (x_sign * scale * prescale, y_sign * scale * prescale), scale * sx, scale * sy, lw=4, color=f"C{j}", zorder=1 + j, angle=np.rad2deg(np.arctan2(y_sign, x_sign)) % 360, label=f'big {j}', gid=f'big {j}') ax.add_patch(a) k = j + 1 ax.set_xlim(-scale / 4000, scale / 4000) ax.set_ylim(-scale / 4000, scale / 4000) ax.axhline(0, color="k") ax.axvline(0, color="k") ax.set_axis_off() ax.set_aspect("equal")
def draw_path(u, v, arrow_length=.1, color=(.8, .8, .8), lw=1): du = u.direction plt.arrow(u.pose[X], u.pose[Y], du[0] * arrow_length, du[1] * arrow_length, head_width=.05, head_length=.1, fc=color, ec=color) dv = v.direction plt.arrow(v.pose[X], v.pose[Y], dv[0] * arrow_length, dv[1] * arrow_length, head_width=.05, head_length=.1, fc=color, ec=color) center, radius = find_circle(u, v) du = u.position - center theta1 = np.arctan2(du[1], du[0]) dv = v.position - center theta2 = np.arctan2(dv[1], dv[0]) # Check if the arc goes clockwise. if np.cross(u.direction, du).item() > 0.: theta1, theta2 = theta2, theta1 ax.add_patch( patches.Arc(center, radius * 2., radius * 2., theta1=theta1 / np.pi * 180., theta2=theta2 / np.pi * 180., color=color, lw=lw))
def _measure(self, qxy, cxy, cid): qx, qy = qxy cx, cy = cxy self._gate(qxy, fc=self._style.dispcol['meas']) # add measure symbol arc = patches.Arc(xy=(qx, qy - 0.15 * HIG), width=WID * 0.7, height=HIG * 0.7, theta1=0, theta2=180, fill=False, ec=self._style.not_gate_lc, linewidth=2, zorder=PORDER_GATE) self.ax.add_patch(arc) self.ax.plot([qx, qx + 0.35 * WID], [qy - 0.15 * HIG, qy + 0.20 * HIG], color=self._style.not_gate_lc, linewidth=2, zorder=PORDER_GATE) # arrow self._line(qxy, [cx, cy + 0.35 * WID], lc=self._style.cc, ls=self._style.cline) arrowhead = patches.Polygon(((cx - 0.20 * WID, cy + 0.35 * WID), (cx + 0.20 * WID, cy + 0.35 * WID), (cx, cy)), fc=self._style.cc, ec=None) self.ax.add_artist(arrowhead) # target if self._style.bundle: self.ax.text(cx + .25, cy + .1, str(cid), ha='left', va='bottom', fontsize=0.8 * self._style.fs, color=self._style.tc, clip_on=True, zorder=PORDER_TEXT)
def circles( data: [], max_: int, ) -> plt: # Plot values as zeroes on numberline, drawing arcs between fg, ax = plt.subplots(1, 1) # Data plotting x = data y = [0] * len(data) # Alternate thetas for arc plotting _v0 = 180 _v1 = 0 for i in range(0, len(data) - 1): # plot arcs between numbers p0 = data[i] p1 = data[i + 1] midpoint = (p0 + p1) / 2 diameter = 2 * abs(p0 - midpoint) _v0 = abs(_v0 - 180) _v1 = abs(_v1 - 180) arc = patches.Arc([midpoint, 0], width=diameter, height=diameter, theta1=_v0, theta2=_v1) ax.add_patch(arc) ax.axis([0, max_, -max_ / 2, max_ / 2]) return plt
def draw(self, cnvs, color, draw_center=True): if eeq(self.p0.x - self.center.x, 0.0): p0_angle = 90 if self.p0.y > self.center.y else 270 else: p0_angle = get_angle( float(self.p0.x - self.center.x), float(self.p0.y - self.center.y)) * 180. / np.pi if eeq(self.p1.x - self.center.x, 0.0): p1_angle = 90 if self.p1.y > self.center.y else 270 else: p1_angle = get_angle( float(self.p1.x - self.center.x), float(self.p1.y - self.center.y)) * 180. / np.pi halfpl = get_halfplane(self.p0, self.p1, self.center) if -1 == halfpl: p0_angle, p1_angle = p1_angle, p0_angle arc = mpt.Arc((float(self.center.x), float(self.center.y)), 2. * float(self.radius), 2. * float(self.radius), angle=0, theta1=p0_angle, theta2=p1_angle, ec=color) cnvs.add_patch(arc) if draw_center: crc = mpt.Circle((float(self.center.x), float(self.center.y)), radius=float(0.2), color=color, fill=False) cnvs.add_patch(crc)
def get_angle_plot(line1, line2, offset=5, color="red", origin=[0, 4], len_x_axis=1, len_y_axis=1): slope1 = -(line1[0] / line1[1]) angle1 = abs(math.degrees(math.atan(slope1))) slope2 = -(line2[0] / line2[1]) angle2 = abs(math.degrees(math.atan(slope2))) theta1 = min(angle1, angle2) theta2 = max(angle1, angle2) angle = theta2 - theta1 if color is None: color = line1.get_color( ) # Uses the color of line 1 if color parameter is not passed. return mpatch.Arc(origin, len_x_axis * offset, len_y_axis * offset, 0, theta1, theta2, color=color, label=str(angle) + u"\u00b0")
def _render_on_subplot(self, subplot): """ TESTS:: sage: A = arc((1,1),3,4,pi/4,(pi,4*pi/3)); A Graphics object consisting of 1 graphics primitive """ import matplotlib.patches as patches from sage.plot.misc import get_matplotlib_linestyle options = self.options() p = patches.Arc( (self.x,self.y), 2.*self.r1, 2.*self.r2, fmod(self.angle,2*pi)*(180./pi), self.s1*(180./pi), self.s2*(180./pi)) p.set_linewidth(float(options['thickness'])) a = float(options['alpha']) p.set_alpha(a) z = int(options.pop('zorder',1)) p.set_zorder(z) c = to_mpl_color(options['rgbcolor']) p.set_linestyle(get_matplotlib_linestyle(options['linestyle'],return_type='long')) p.set_edgecolor(c) subplot.add_patch(p)
def _render_on_subplot(self, subplot): """ TESTS:: sage: A = arc((1,1),3,4,pi/4,(pi,4*pi/3)); A """ import matplotlib.patches as patches options = self.options() p = patches.Arc( (self.x,self.y), 2.*self.r1, 2.*self.r2, fmod(self.angle,2*pi)*(180./pi), self.s1*(180./pi), self.s2*(180./pi)) p.set_linewidth(float(options['thickness'])) a = float(options['alpha']) p.set_alpha(a) z = int(options.pop('zorder',1)) p.set_zorder(z) c = to_mpl_color(options['rgbcolor']) p.set_linestyle(options['linestyle']) p.set_edgecolor(c) subplot.add_patch(p)
def drawCirc(ax, radius, centX, centY, angle_, theta2_, lineWidth=3, color_='black'): # ========Line arc = patches.Arc([centX, centY], radius, radius, angle=angle_, theta1=0, theta2=theta2_, capstyle='round', linestyle='-', lw=lineWidth, color=color_) ax.add_patch(arc) # ========Create the arrow head # endX=centX+(radius/2)*np.cos((theta2_+angle_)/180*np.pi) #Do trig to determine end position # endY=centY+(radius/2)*np.sin((theta2_+angle_)/180*np.pi) # ax.add_patch( #Create triangle as arrow head # patches.RegularPolygon( # (endX, endY), # (x,y) # 3, # number of vertices # radius/10, # radius # (angle_+theta2_)/180*np.pi, # orientation # color=color_ # ) # ) # ========Create the arrow head begX = centX + (radius / 2) * np.cos((angle_) / 180 * np.pi) # Do trig to determine end position begY = centY + (radius / 2) * np.sin((angle_) / 180 * np.pi) ax.add_patch( # Create triangle as arrow head patches.RegularPolygon( (begX, begY), # (x,y) 3, # number of vertices radius / 20, # radius (180 + angle_) / 180 * np.pi, # orientation color=color_ ) ) ax.set_xlim([centX - radius, centY + radius]) and ax.set_ylim([centY - radius, centY + radius])
def gen_steer_curve(self, x, y, heading, steer_angle): """Generate Steering Curve to predict car trajectory""" if abs(math.tan(math.radians(steer_angle))) > 0.0001: R = self.vehiclelength / math.tan(math.radians(steer_angle)) else: R = 100000 radius = abs(R) lengthangle = 7200 / (2 * math.pi * radius) if R >= 0: centerangle = math.pi / 2 + heading startangle = math.degrees(heading - math.pi / 2) theta1 = 0 theta2 = lengthangle else: centerangle = heading - math.pi / 2 startangle = math.degrees(math.pi / 2 + heading) theta1 = -lengthangle theta2 = 0 centerx = x + math.cos(centerangle) * radius centery = y + math.sin(centerangle) * radius curve = patches.Arc((centerx, centery), 2 * radius, 2 * radius, angle=startangle, theta1=theta1, theta2=theta2, linewidth=2, zorder=2) return curve
def make_semicircle(center_x, diameter, angles): return patches.Arc((center_x, 0), diameter, diameter, theta1=angles[0], theta2=angles[1], linewidth=LINE_WIDTH)
def viz(self): """ Visualize the current geometry """ nh = log10(self.NH) + 22 import matplotlib import matplotlib.pyplot as plt from matplotlib.collections import PatchCollection import matplotlib.path as mpath import matplotlib.patches as mpatches import matplotlib.lines as mlines plt.figure(figsize=(5,5)) font = 'sans-serif' ax = plt.axes([0,0,1,1]) thickness = max(0, min(1, (nh - 20.) / 5)) plt.text(0.35, 0.5, "nH=%2.1f" % nh, ha="right", va='center', family=font, size=14) #print thickness ax.add_line(mlines.Line2D([0,0.9], [0.5,0.5], lw=1.,alpha=0.4, ls='dashed', color='grey')) ax.add_line(mlines.Line2D([0.4,0.4], [0.5,0.9], lw=1.,alpha=0.4, ls='dashed', color='grey')) ax.add_patch( mpatches.Arc((0.4,0.5), 0.5, 0.5, theta2=90, theta1=90, color='black', linewidth=1, alpha=1, fill=False, ls='dashed') ) ax.add_patch( mpatches.Wedge((0.4,0.5), 0.3, -90, 90, color='black', linewidth=0, alpha=thickness, fill=True) ) ax.add_patch( mpatches.Wedge((0.4,0.5), 0.3, 90, -90, color='black', linewidth=0, alpha=thickness, fill=True) ) ax.add_patch( mpatches.Circle((0.4,0.5), 0.02, color='red', linewidth=0, alpha=1, fill=True) ) ax.set_xticks([]) ax.set_yticks([])
def drawArrow(locTo, locFrom, w,ax): if w>0: ls='solid' col="blue" else: ls='solid' #'dashed' #'dotted' col="turquoise" if locFrom == locTo: arc = mpatches.Arc((locFrom['x']-1.5*nradius,locFrom['y']),2*nradius,nradius,0,45,-45, linestyle=ls,color=col) ax.add_patch(arc) elif locFrom['y'] == locTo['y'] : if locFrom['x']<locTo['x']: start=(locFrom['x']+nradius*0.9,locFrom['y']-nradius*0.2) end=(locTo['x']-nradius*0.9,locTo['y']-nradius*0.2) cstyle = 'arc3,rad=0.3' else: start=(locFrom['x']-nradius*0.8,locFrom['y']-nradius*0.2) end=(locTo['x']+nradius*0.8,locTo['y']-nradius*0.2) cstyle = 'arc3,rad=-0.3' arrow = mpatches.FancyArrowPatch(start, end, connectionstyle=cstyle, mutation_scale=20, arrowstyle='->', linestyle=ls,color=col) ax.add_patch(arrow) else: angle = math.atan((locTo['x'] - locFrom['x']) / float(locTo['y'] - locFrom['y'])) xAdjustment = nradius * math.sin(angle) yAdjustment = nradius * math.cos(angle) if locTo['y'] > locFrom['y'] : start = (locFrom['x'] + xAdjustment, locFrom['y'] + yAdjustment) end = (locTo['x'] - xAdjustment, locTo['y'] - yAdjustment) else: start = (locFrom['x'] - xAdjustment, locFrom['y'] - yAdjustment) end = (locTo['x'] + xAdjustment, locTo['y'] + yAdjustment) arrow = mpatches.FancyArrowPatch(start, end, mutation_scale=20, arrowstyle='->', linestyle=ls,color=col) ax.add_patch(arrow)