Mercurial > repos > shellac > sam_consensus_v3
comparison env/share/doc/networkx-2.5/examples/subclass/plot_printgraph.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
| author | shellac |
|---|---|
| date | Mon, 22 Mar 2021 18:12:50 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:4f3585e2f14b |
|---|---|
| 1 """ | |
| 2 =========== | |
| 3 Print Graph | |
| 4 =========== | |
| 5 | |
| 6 Example subclass of the Graph class. | |
| 7 """ | |
| 8 | |
| 9 import matplotlib.pyplot as plt | |
| 10 import networkx as nx | |
| 11 from networkx import Graph | |
| 12 | |
| 13 | |
| 14 class PrintGraph(Graph): | |
| 15 """ | |
| 16 Example subclass of the Graph class. | |
| 17 | |
| 18 Prints activity log to file or standard output. | |
| 19 """ | |
| 20 | |
| 21 def __init__(self, data=None, name="", file=None, **attr): | |
| 22 Graph.__init__(self, data=data, name=name, **attr) | |
| 23 if file is None: | |
| 24 import sys | |
| 25 | |
| 26 self.fh = sys.stdout | |
| 27 else: | |
| 28 self.fh = open(file, "w") | |
| 29 | |
| 30 def add_node(self, n, attr_dict=None, **attr): | |
| 31 Graph.add_node(self, n, attr_dict=attr_dict, **attr) | |
| 32 self.fh.write(f"Add node: {n}\n") | |
| 33 | |
| 34 def add_nodes_from(self, nodes, **attr): | |
| 35 for n in nodes: | |
| 36 self.add_node(n, **attr) | |
| 37 | |
| 38 def remove_node(self, n): | |
| 39 Graph.remove_node(self, n) | |
| 40 self.fh.write(f"Remove node: {n}\n") | |
| 41 | |
| 42 def remove_nodes_from(self, nodes): | |
| 43 for n in nodes: | |
| 44 self.remove_node(n) | |
| 45 | |
| 46 def add_edge(self, u, v, attr_dict=None, **attr): | |
| 47 Graph.add_edge(self, u, v, attr_dict=attr_dict, **attr) | |
| 48 self.fh.write(f"Add edge: {u}-{v}\n") | |
| 49 | |
| 50 def add_edges_from(self, ebunch, attr_dict=None, **attr): | |
| 51 for e in ebunch: | |
| 52 u, v = e[0:2] | |
| 53 self.add_edge(u, v, attr_dict=attr_dict, **attr) | |
| 54 | |
| 55 def remove_edge(self, u, v): | |
| 56 Graph.remove_edge(self, u, v) | |
| 57 self.fh.write(f"Remove edge: {u}-{v}\n") | |
| 58 | |
| 59 def remove_edges_from(self, ebunch): | |
| 60 for e in ebunch: | |
| 61 u, v = e[0:2] | |
| 62 self.remove_edge(u, v) | |
| 63 | |
| 64 def clear(self): | |
| 65 Graph.clear(self) | |
| 66 self.fh.write("Clear graph\n") | |
| 67 | |
| 68 | |
| 69 G = PrintGraph() | |
| 70 G.add_node("foo") | |
| 71 G.add_nodes_from("bar", weight=8) | |
| 72 G.remove_node("b") | |
| 73 G.remove_nodes_from("ar") | |
| 74 print("Nodes in G: ", G.nodes(data=True)) | |
| 75 G.add_edge(0, 1, weight=10) | |
| 76 print("Edges in G: ", G.edges(data=True)) | |
| 77 G.remove_edge(0, 1) | |
| 78 G.add_edges_from(zip(range(0, 3), range(1, 4)), weight=10) | |
| 79 print("Edges in G: ", G.edges(data=True)) | |
| 80 G.remove_edges_from(zip(range(0, 3), range(1, 4))) | |
| 81 print("Edges in G: ", G.edges(data=True)) | |
| 82 | |
| 83 G = PrintGraph() | |
| 84 nx.add_path(G, range(10)) | |
| 85 nx.add_star(G, range(9, 13)) | |
| 86 nx.draw(G) | |
| 87 plt.show() |
