def scatter(self, x, y, s, ax=None, fancy=False, **kwargs): """ takes data coordinate x, y and plot them to a data coordinate axes, s is the radius in data units. When fancy is True, apply a radient filter so that the edge is blent into the background; better with marker='o' or marker='+'. """ X, Y, S = numpy.asarray([x, y, s]) if ax is None: ax = self.default_axes def filter(image, dpi): # this is problematic if the marker is clipped. if image.shape[0] <= 1 and image.shape[1] <= 1: return image xgrad = 1.0 \ - numpy.fabs(numpy.linspace(0, 2, image.shape[0], endpoint=True) - 1.0) ygrad = 1.0 \ - numpy.fabs(numpy.linspace(0, 2, image.shape[1], endpoint=True) - 1.0) image[..., 3] *= xgrad[:, None]**0.5 image[..., 3] *= ygrad[None, :]**0.5 return image, 0, 0 marker = kwargs.pop('marker', 'x') verts = kwargs.pop('verts', None) # to be API compatible if marker is None and not (verts is None): marker = (verts, 0) verts = None objs = [] color = kwargs.pop('color', None) edgecolor = kwargs.pop('edgecolor', None) linewidth = kwargs.pop('linewidth', kwargs.pop('lw', None)) marker_obj = MarkerStyle(marker) if not marker_obj.is_filled(): edgecolor = color for x, y, r in numpy.nditer([X, Y, S], flags=['zerosize_ok']): path = marker_obj.get_path().transformed( marker_obj.get_transform().scale(r).translate(x, y)) obj = PathPatch( path, facecolor=color, edgecolor=edgecolor, linewidth=linewidth, transform=ax.transData, ) obj.set_alpha(1.0) if fancy: obj.set_agg_filter(filter) obj.rasterized = True objs += [obj] ax.add_artist(obj) ax.autoscale_view() return objs
def scatter(self, x, y, s, ax=None, fancy=False, **kwargs): """ takes data coordinate x, y and plot them to a data coordinate axes, s is the radius in data units. When fancy is True, apply a radient filter so that the edge is blent into the background; better with marker='o' or marker='+'. """ X, Y, S = numpy.asarray([x, y, s]) if ax is None: ax=self.default_axes def filter(image, dpi): # this is problematic if the marker is clipped. if image.shape[0] <=1 and image.shape[1] <=1: return image xgrad = 1.0 \ - numpy.fabs(numpy.linspace(0, 2, image.shape[0], endpoint=True) - 1.0) ygrad = 1.0 \ - numpy.fabs(numpy.linspace(0, 2, image.shape[1], endpoint=True) - 1.0) image[..., 3] *= xgrad[:, None] ** 0.5 image[..., 3] *= ygrad[None, :] ** 0.5 return image, 0, 0 marker = kwargs.pop('marker', 'x') verts = kwargs.pop('verts', None) # to be API compatible if marker is None and not (verts is None): marker = (verts, 0) verts = None objs = [] color = kwargs.pop('color', None) edgecolor = kwargs.pop('edgecolor', None) linewidth = kwargs.pop('linewidth', kwargs.pop('lw', None)) marker_obj = MarkerStyle(marker) if not marker_obj.is_filled(): edgecolor = color for x,y,r in numpy.nditer([X, Y, S], flags=['zerosize_ok']): path = marker_obj.get_path().transformed( marker_obj.get_transform().scale(r).translate(x, y)) obj = PathPatch( path, facecolor = color, edgecolor = edgecolor, linewidth = linewidth, transform = ax.transData, ) obj.set_alpha(1.0) if fancy: obj.set_agg_filter(filter) obj.rasterized = True objs += [obj] ax.add_artist(obj) ax.autoscale_view() return objs
def GenWedge(ofs, rx, ry, facecolor='r', label='', alpha=0.3): codes, verts = [], [] amin = 0 ox, oy = ofs if ox == 0 and oy == 0: amax = np.pi * 2 ngoes = 1 elif ox == 0: amax = np.pi ngoes = 2 else: amax = np.pi / 2 ngoes = 4 phi = np.linspace(amin, amax, 100) for g in range(ngoes): if g == 0: cofs = ofs elif g == 1: if (ngoes == 2): cofs = [0, -ofs[1]] else: cofs = [-ofs[0], ofs[1]] elif g == 2: cofs = [-ofs[0], -ofs[1]] elif g == 3: cofs = [ofs[0], -ofs[1]] codes.append(Path.MOVETO) if ngoes > 1: verts.append(cofs) else: verts.append([cofs[0] + rx, cofs[1]]) for p in phi: if p == 0: codes.append(Path.LINETO) else: #print dir(Path) codes.append(Path.CURVE4) verts.append([ cofs[0] + rx * cos(p + 2 * np.pi / ngoes * g), cofs[1] + ry * sin(p + 2 * np.pi / ngoes * g) ]) if (ngoes > 1): codes.append(Path.CLOSEPOLY) verts.append(ofs) w = PathPatch(Path(verts, codes), label=label) w.set_alpha(alpha) w.set_facecolor(facecolor) w.set_linewidth(0.0) lhandles.append(w) return w
def gen_patch(x): v1 = gen_arc((x, 0), 1, numpy.radians(-45), numpy.radians(45)) v2 = gen_arc((-x, 0), 1, numpy.radians(135), numpy.radians(225)) verts = numpy.concatenate((v1, v2, (v1[0], v1[0]))) codes = [1] + [2] * (63 + 64 + 1) + [79] patch = PathPatch(Path(verts, codes)) patch.set_alpha(0.5) return patch, v1[-1] # last point
def gen_patches(drop, h, resolution=64): """Generate patches for droplet at height h """ # Generate arc starting form theta1 to theta2 def gen_arc(center, r, theta1, theta2): t = numpy.linspace(theta1, theta2, resolution) vert = r * numpy.vstack((numpy.cos(t), numpy.sin(t))).T return vert + center print(h) drop.h = h # set height and update delta_t, delta_b = drop.get_separate_height() r1 = drop.r1; r2 = drop.r2 t_t = drop.theta_t; t_b = drop.theta_b center_r = (r1 - r2, delta_b) center_l = (-(r1 - r2), delta_b) v_r = gen_arc(center_r, r2, -(t_b - pi / 2), (t_t - pi / 2)) v_l = gen_arc(center_l, r2, pi - (t_t - pi / 2), pi + (t_b - pi / 2)) p_b = v_r[0]; p_t = v_r[-1] verts = numpy.concatenate((v_r, v_l, (p_b, p_b))) # vertices for patch codes = [Path.MOVETO] + [Path.LINETO] * (resolution * 2) \ + [Path.CLOSEPOLY] # codes for path drop_patch = PathPatch(Path(verts, codes), facecolor="#ccdfff") drop_patch.set_alpha(0.5) circle = Circle(center_r, radius=r2, ls="--", fill=False, linewidth=0.5, edgecolor="#ffb4a5") arr1 = FancyArrowPatch((0, delta_b), (r1, delta_b), mutation_scale=10, linewidth=0, facecolor="#9b9b9b") arr2 = FancyArrowPatch(center_r, p_t, mutation_scale=10, linewidth=0, facecolor="#ffa047") pressure = drop.get_delta_stress() pr = (r1, delta_b) print(pr, p_t) return drop_patch, circle, arr1, arr2, \ pressure, pr, p_t
def _render_on_subplot(self, subplot): """ Render this Bezier path in a subplot. This is the key function that defines how this Bezier path graphics primitive is rendered in matplotlib's library. TESTS:: sage: bezier_path([[(0,1),(.5,0),(1,1)]]) Graphics object consisting of 1 graphics primitive :: sage: bezier_path([[(0,1),(.5,0),(1,1),(-3,5)]]) Graphics object consisting of 1 graphics primitive """ from matplotlib.patches import PathPatch from matplotlib.path import Path from sage.plot.misc import get_matplotlib_linestyle options = dict(self.options()) del options['alpha'] del options['thickness'] del options['rgbcolor'] del options['zorder'] del options['fill'] del options['linestyle'] bpath = Path(self.vertices, self.codes) bpatch = PathPatch(bpath, **options) options = self.options() bpatch.set_linewidth(float(options['thickness'])) bpatch.set_fill(options['fill']) bpatch.set_zorder(options['zorder']) a = float(options['alpha']) bpatch.set_alpha(a) c = to_mpl_color(options['rgbcolor']) bpatch.set_edgecolor(c) bpatch.set_facecolor(c) bpatch.set_linestyle( get_matplotlib_linestyle(options['linestyle'], return_type='long')) subplot.add_patch(bpatch)
def _render_on_subplot(self, subplot): """ Render this Bezier path in a subplot. This is the key function that defines how this Bezier path graphics primitive is rendered in matplotlib's library. TESTS:: sage: bezier_path([[(0,1),(.5,0),(1,1)]]) Graphics object consisting of 1 graphics primitive :: sage: bezier_path([[(0,1),(.5,0),(1,1),(-3,5)]]) Graphics object consisting of 1 graphics primitive """ from matplotlib.patches import PathPatch from matplotlib.path import Path from sage.plot.misc import get_matplotlib_linestyle options = dict(self.options()) del options['alpha'] del options['thickness'] del options['rgbcolor'] del options['zorder'] del options['fill'] del options['linestyle'] bpath = Path(self.vertices, self.codes) bpatch = PathPatch(bpath, **options) options = self.options() bpatch.set_linewidth(float(options['thickness'])) bpatch.set_fill(options['fill']) bpatch.set_zorder(options['zorder']) a = float(options['alpha']) bpatch.set_alpha(a) c = to_mpl_color(options['rgbcolor']) bpatch.set_edgecolor(c) bpatch.set_facecolor(c) bpatch.set_linestyle(get_matplotlib_linestyle(options['linestyle'], return_type='long')) subplot.add_patch(bpatch)
v1 = gen_arc((1, 0), 1, numpy.radians(-45), numpy.radians(45)) v2 = gen_arc((-1, 0), 1, numpy.radians(135), numpy.radians(225)) verts = numpy.concatenate((v1, v2, (v1[0], v1[0]))) codes = [1] + [2] * (63 + 64 + 1) + [79] # code2.setflags(write=True) # code2[0] = 2 # codes = numpy.concatenate((code1, code2)) # verts = numpy.concatenate((v1, v2)) # print(v1, code1) # print(v2, code2) # print(verts, codes) patch = PathPatch(Path(verts, codes)) patch.set_alpha(0.5) circle = Circle((1, 0), radius=1, ls="--", fill=False, edgecolor="red") arr1 = FancyArrowPatch((0, 0), (2, 0), mutation_scale=30, linewidth=0, facecolor="gray") arr2 = FancyArrowPatch((1, 0), v1[-1], mutation_scale=30, linewidth=0, facecolor="gray") # collect = PatchCollection([patch, circle], alpha=0.2) # ax.add_patch(arc1)