Python networkx 模块,graphviz_layout() 实例源码

我们从Python开源项目中,提取了以下2个代码示例,用于说明如何使用networkx.graphviz_layout()

项目:latenttrees    作者:kaltwang    | 项目源码 | 文件源码
def draw_axes(self, ax=None, id_highlight=None):
        # plt.ion()  # interactive mode on (doesn't work with pycharm)
        g = self.__nxgraph
        # new call format for networkx 1.11 (uses pygraphviz, which does not support Python 3!)
        # pos = nx.drawing.nx_agraph.graphviz_layout(g, prog='dot')
        # old call format for networkx 1.10
        # pos = nx.graphviz_layout(g, prog='dot')
        # new call format for networkx 1.11 (uses pydot)
        pos = nx.drawing.nx_pydot.graphviz_layout(g, prog='dot')

        if ax is None:
            ax = self.get_axes(self.id_axes)
        assert len(ax) == self.required_axes

        nx.draw(g, pos, ax=ax[0], hold=True, with_labels=True, arrows=True)
        # nx.draw_networkx(self.__graph)

        # plot the roots
        if id_highlight is None:
            id_highlight = self.__id_roots
        for r in id_highlight:
            ax[0].scatter(pos[r][0], pos[r][1], s=500)

        for id_node, node in self.nodes_iter(data=True):
            c = "{}".format(node.layer)
            ax[0].text(pos[id_node][0], pos[id_node][1], "\n" + c, va='top', ha='center', color='blue')

        # plt.show()  # this call blocks further execution until window is closed
项目:habran    作者:mkotov    | 项目源码 | 文件源码
def draw(G, karmas):
    def get_root(G):
        for x in G.nodes():
            if len(G.predecessors(x)) == 0:
                return x
        return None

    def gen_node_size(karma):
        if karma == "RO" or karma == "DA":
            return 1
        else:
            return max(1, math.sqrt(abs(float(karma))))

    def gen_node_sizes(karmas, nodes):
        return [20 * gen_node_size(karmas[n]) for n in nodes]

    def gen_node_color(karma):
        if karma == "RO":
            return "darkgray"
        elif karma == "DA":
            return "darkgray"
        elif float(karma) > 0:
            return "green"
        elif float(karma) < 0:
            return "red"
        else:
            return "blue"

    def gen_node_colors(karmas, nodes):
        return [gen_node_color(karmas[n]) for n in nodes]

    def gen_node_labels(karmas, G):
        ranks = []
        root = get_root(G)
        for n in G.nodes():
            r = len(G.successors(n)) * 10
            if karmas[n] != "RO" and karmas[n] != "DA":
             r += abs(float(karmas[n]))
            if n == root:
                r += 1000
            ranks.append([n, r])
        return dict((p[0], p[0]) for p in sorted(ranks, key=lambda p: p[1], reverse=True)[0:MAX_NUM_OF_LABELS])

    nx.draw(G, 
            nx.graphviz_layout(G, prog="neato", root=get_root(G)), 
            node_size=gen_node_sizes(karmas, G.nodes()), 
            node_color=gen_node_colors(karmas, G.nodes()), 
            arrows=True, 
            labels=gen_node_labels(karmas, G), 
            with_labels=True, 
            font_size="10", 
            edge_color="lightgrey", 
            linewidths=0)