campus_topo_gen.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:ride 作者: KyleBenson 项目源码 文件源码
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!"
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号