def draw(self, label_nodes=False):
"""Draw the graph using matplotlib in a color-coordinated manner."""
try:
import matplotlib.pyplot as plt
print 'Node colors: red=core, blue=major-building, green=distribution, yellow=minor-building, cyan=server,' \
' magenta=host, black=floor-switch, white=rack-switch, white=cloud, green=gateway'
# TODO: ignore building internals?
colormap = {'c': 'r', 'b': 'b', 'd': 'g', 'm': 'y', 's': 'c', 'h': 'm', 'f': 'k', 'r': 'w', 'x': 'w', 'g': 'g'}
node_colors = [colormap[node[0]] for node in self.topo.nodes()]
# shell layout places nodes as a series of concentric circles
positions = nx.shell_layout(self.topo, [self.core_nodes,
# sort the building routers by degree in attempt to get ones connected to each other next to each other
sorted(self.major_building_routers, key=lambda n: nx.degree(self.topo, n)) + self.distribution_routers + self.server_nodes,
self.hosts + self.minor_building_routers])
# then do a spring layout, keeping the inner nodes fixed in positions
positions = nx.spring_layout(self.topo, pos=positions, fixed=self.core_nodes + self.server_nodes + self.major_building_routers + self.distribution_routers)
nx.draw(self.topo, node_color=node_colors, pos=positions, with_labels=label_nodes)
plt.show()
except ImportError:
print "ERROR: couldn't draw graph as matplotlib.pyplot couldn't be imported!"
评论列表
文章目录