def draw_text(txt, x, y, w, h, gca): v_max_scale = 0.7 h_max_scale = 0.8 rotate = (abs(w) < abs(h)) fp = FontProperties(family="arial") fsize = w if rotate else h tp = TextPath((0, 0), txt, size=fsize, prop=fp) bw, bh = w, h (__, __, tw, th) = tp.get_extents().bounds if rotate: tp = tp.transformed(Affine2D().rotate_deg(90).translate(th, 0)) bw, bh = bh, bw if tw < h_max_scale * bw: scale = v_max_scale * bh / th else: scale = h_max_scale * bw / tw tp = tp.transformed(Affine2D().scale(scale)) (__, __, tw, th) = tp.get_extents().bounds tp = tp.transformed(Affine2D().translate(x + (w - tw) / 2, y + (h - th) / 2)) txt = PathPatch(tp, linewidth=0.3, facecolor="0", edgecolor="0") gca.add_patch(txt)
def addLetter(char, x, y, height, color): text = TextPath((0, 0), char, size=1, prop=fontprop) bbox = text.get_extents() (tx0, ty0), tw, th = bbox.min, bbox.width, bbox.height trans = Affine2D.identity() \ .translate(-tx0, -ty0) \ .scale(0.9/tw, height/th) \ .translate(x+0.5, y) t = trans.transform_path(text) pylab.gca().add_patch(PathPatch(t, fc=color, ec='none'))
def addletter(let, x, y, height, color, alpha=1): text = TextPath((0, 0), let, size=1, prop=fontprop) bbox = text.get_extents() tx0, ty0 = bbox.min tw, th = bbox.width, bbox.height trans = Affine2D.identity() \ .translate(-tx0,-ty0) \ .scale(0.9/tw,height/th) \ .translate(x,y) t = trans.transform_path(text) pylab.gca().add_patch(PathPatch(t, fc=color, ec='none', alpha=alpha))
def text_on_2D(ax, s, xy=(0.5, 0.95), xlim=None, ylim=None, size=None, angle=0.0, usetex=False, center=True, aspect_ratio=1.0, **kwargs): #FIXME expand and add to text_on_the_wall #xlim = ax.get_xlim() #ylim = ax.get_ylim() #aspect_ratio = (ylim[1] - ylim[0])/(ylim[1] - ylim[0]) #size = 0.8 #center = True #angle = 0.0 #usetex=False #s = r"$\delta=0.00$" if xlim == None: xlim = ax.get_xlim() if ylim == None: ylim = ax.get_ylim() x = xlim[0] + (xlim[1] - xlim[0]) * xy[0] y = ylim[0] + (ylim[1] - ylim[0]) * xy[1] xy1 = (x, y) text_path = TextPath((0, 0), s, size=size, usetex=usetex) #Scale by aspect ratio scale_path = Affine2D().scale(1.0, sy=aspect_ratio) text_path = scale_path.transform_path(text_path) #Get bbox to center text bbox = text_path.get_extents() bbox_points = bbox.get_points() _b = bbox_points.sum(axis=0) / 2 if center == True: trans = Affine2D().rotate(angle).translate(xy1[0] - _b[0], xy1[1] - _b[1]) else: trans = Affine2D().rotate(angle).translate(xy1[0], xy1[1] - _b[1]) tp = trans.transform_path(text_path) p1 = PathPatch(tp, **kwargs) ax.add_patch(p1)
def text_on_the_wall(ax, xyz, s, zdir="z", size=None, angle=0, usetex=False, center=True, aspect_ratio=1.0, **kwargs): x, y, z = xyz if zdir == "y": xy1, z1 = (x, z), y elif zdir == "x": xy1, z1 = (y, z), x else: xy1, z1 = (x, y), z text_path = TextPath((0, 0), s, size=size, usetex=usetex) #Scale by aspect ratio scale_path = Affine2D().scale(1.0, sy=aspect_ratio) text_path = scale_path.transform_path(text_path) #Get bbox to center text bbox = text_path.get_extents() bbox_points = bbox.get_points() _b = bbox_points.sum(axis=0) / 2 if center == True: trans = Affine2D().rotate(angle).translate(xy1[0] - _b[0], xy1[1] - _b[1]) else: trans = Affine2D().rotate(angle).translate(xy1[0], xy1[1] - _b[1]) tp = trans.transform_path(text_path) p1 = PathPatch(tp, **kwargs) ax.add_patch(p1) art3d.pathpatch_2d_to_3d(p1, z=z1, zdir=zdir)