def layout(self): self.n2c = cartesian(self.root, scaled=self.scaled, yunit=1.0, smooth=self.smooth_xpos) self.labels = [ x.label for x in self.root ] for c in self.n2c.values(): c.x += self.xoff; c.y += self.yoff sv = sorted([ [c.y, c.x, n] for n, c in self.n2c.items() ]) self.coords = sv#numpy.array(sv)
def layout(self): """ This method calculates the coordinates of the nodes """ self.n2c = cartesian(self.root, scaled=self.scaled, yunit=1.0) for c in self.n2c.values(): c.x += self.xoff; c.y += self.yoff sv = sorted([[c.y, c.x, n] for n, c in self.n2c.items()]) for i in sv: i[2].yval = i[0] i[2].xval = i[1] self.coords = sv
def plot_tree(self, root, **kwargs): pyplot.ioff() self.root = root self.leaves = root.leaves() self.nleaves = len(self.leaves) self.leaf_hsep = 1.0/float(self.nleaves) if "branchlabels" in kwargs: self.branchlabels = kwargs["branchlabels"] if "leaflabels" in kwargs: self.leaflabels = kwargs["leaflabels"] for n in root.descendants(): if n.length is None: self.scaled=False; break n2c = layout.cartesian(root, scaled=self.scaled) self.n2c = n2c sv = sorted([ [c.y, c.x, n] for n, c in n2c.items() ]) self.coords = sv#numpy.array(sv) self.yaxis.set_visible(False) M = Path.MOVETO; L = Path.LINETO; S = Path.STOP verts = [] codes = [] self.node2label = {} for node, coords in n2c.items(): x = coords.x; y = coords.y if node.parent: pcoords = n2c[node.parent] px = pcoords.x; py = pcoords.y verts.append((x, y)); codes.append(M) verts.append((px, y)); codes.append(L) verts.append((px, py)); codes.append(L) if node.isleaf and node.label and self.leaflabels: txt = self.annotate( node.label, xy=(x, y), xytext=(self.leaf_offset, 0), textcoords="offset points", verticalalignment=self.leaf_valign, horizontalalignment=self.leaf_halign, fontsize=self.leaf_fontsize, clip_on=True, picker=True ) txt.set_visible(False) self.node2label[node] = txt if (not node.isleaf) and node.label and self.branchlabels: txt = self.annotate( node.label, xy=(x, y), xytext=(self.branch_offset,0), textcoords="offset points", verticalalignment=self.branch_valign, horizontalalignment=self.branch_halign, fontsize=self.branch_fontsize, bbox=dict(fc="lightyellow", ec="none", alpha=0.8), clip_on=True, picker=True ) ## txt.set_visible(False) self.node2label[node] = txt px, py = verts[-1] verts.append((px, py)); codes.append(M) verts.append((px, py)); codes.append(S) self.branchpath = Path(verts, codes) self.branchpatch = PathPatch(self.branchpath, fill=False) self.add_patch(self.branchpatch) self.highlight_support() self.mark_named() self.home() self.set_name(self.name) pyplot.ion() return self