visualize_tree-backup_1.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. def write_frame_png(self, filename="nix-tree.png"):
  2. """Dump the graph to a png file"""
  3. try:
  4. cmap = getattr(matplotlib.cm, self.config["color_map"])
  5. except AttributeError:
  6. raise util.TreeCLIError("Colormap {} does not exist".format(
  7. self.config["color_map"]))
  8. pos = {n: (n.x, n.y) for n in self.nodes}
  9. col_scale = 255.0/(self.depth+1.0)
  10. col = [(x.level+random.random()*self.config["color_scatter"])*col_scale
  11. for x in self.G.nodes()]
  12. col = [min([x,255]) for x in col]
  13. img_y_height=self.config["img_y_height_inches"]
  14. size_min = self.config["min_node_size"]
  15. size_max = self.config["max_node_size_over_min_node_size"] * size_min
  16. plt.figure(1, figsize=(img_y_height*self.config["aspect_ratio"],
  17. img_y_height))
  18. node_size = [min(size_min + (x.out_degree-1)*
  19. self.config["add_size_per_out_link"],
  20. size_max) if x.level > 0 else size_max for
  21. x in self.G.nodes()]
  22. # Draw edges
  23. nx.draw(self.G, pos, node_size=node_size, arrows=False,
  24. with_labels=self.config["show_labels"],
  25. edge_color=self.config["edge_color"],
  26. font_size=12*self.config["font_scale"],
  27. node_color=col, vmin=0, vmax=256,
  28. width=self.config["edge_width_scale"],
  29. alpha=self.config["edge_alpha"], nodelist=[])
  30. # Draw nodes
  31. nx.draw(self.G, pos, node_size=node_size, arrows=False,
  32. with_labels=self.config["show_labels"],
  33. font_size=12*self.config["font_scale"],
  34. node_color=col, vmin=0, vmax=255, edgelist=[],
  35. font_weight="light", cmap=cmap,
  36. font_color=self.config["font_color"])
  37. logger.info("Writing png file: {}".format(filename))
  38. plt.savefig(filename, dpi=self.config["dpi"])
  39. plt.close()