def plot_graph(self, file_name: str='graph.png', label_nodes: bool=True, label_edges: bool=True):
import matplotlib.pyplot as plt
# pos = nx.spring_layout(self.graph)
pos = nx.shell_layout(self.graph, dim=1024, scale=0.5)
# pos = nx.random_layout(self.graph, dim=1024, scale=0.5)
if label_edges:
edge_labels = {
(edge[0], edge[1]): edge[2]['object'] for edge in self.graph.edges(data=True)
}
nx.draw_networkx_edge_labels(self.graph, pos, edge_labels, font_size=5)
if label_nodes:
labels = {node[0]: node[1] for node in self.graph.nodes(data=True)}
nx.draw_networkx_labels(self.graph, pos, labels, font_size=5, alpha=0.8)
# nx.draw(self.graph, with_labels=True, arrows=True, node_size=80)
nx.draw_spectral(self.graph, with_labels=True, arrows=True, node_size=80)
plt.savefig(file_name, dpi=1024)
评论列表
文章目录