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

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

项目:nelpy    作者:nelpy    | 项目源码 | 文件源码
def draw_transmat_graph_inner(G, edge_threshold=0, lw=1, ec='0.2', node_size=15):

    num_states = G.number_of_nodes()

    edgewidth = [ d['weight'] for (u,v,d) in G.edges(data=True)]
    edgewidth = np.array(edgewidth)
    edgewidth[edgewidth<edge_threshold] = 0

    npos=circular_layout(G, scale=1, direction='CW')

    nx.draw_networkx_edges(G, npos, alpha=1.0, width=edgewidth*lw, edge_color=ec)

    nx.draw_networkx_nodes(G, npos, node_size=node_size, node_color='k',alpha=1.0)
    ax = plt.gca()
    ax.set_aspect('equal')

    return ax
项目:gym-kidney    作者:camoy    | 项目源码 | 文件源码
def _render(self, mode = "human", close = False):
        if close:
            return

        import matplotlib.pyplot as plt

        if self.tick == 0:
            plt.ion()

        G = self.G
        attrs = nx.get_node_attributes(G, "ndd")
        values = ["red" if attrs[v] else "blue" for v in G.nodes()]

        plt.clf()
        nx.draw(G,
            pos = nx.circular_layout(G),
            node_color = values)
        plt.pause(0.01)

        return []
项目:GraphTime    作者:GlooperLabs    | 项目源码 | 文件源码
def draw(self, layout='circular', figsize=None):
        """Draw all graphs that describe the DGM in a common figure

        Parameters
        ----------
        layout : str
            possible are 'circular', 'shell', 'spring'
        figsize : tuple(int)
            tuple of two integers denoting the mpl figsize

        Returns
        -------
        fig : figure
        """
        layouts = {
            'circular': nx.circular_layout,
            'shell': nx.shell_layout,
            'spring': nx.spring_layout
        }
        figsize = (10, 10) if figsize is None else figsize
        fig = plt.figure(figsize=figsize)
        rocls = np.ceil(np.sqrt(len(self.graphs)))
        for i, graph in enumerate(self.graphs):
            ax = fig.add_subplot(rocls, rocls, i+1)
            ax.set_title('Graph ' + str(i+1))
            ax.axis('off')
            ax.set_frame_on(False)
            g = graph.nxGraph
            weights = [abs(g.edge[i][j]['weight']) * 5 for i, j in g.edges()]
            nx.draw_networkx(g, pos=layouts[layout](g), ax=ax, edge_cmap=plt.get_cmap('Reds'),
                             width=2, edge_color=weights)
        return fig
项目:GraphTime    作者:GlooperLabs    | 项目源码 | 文件源码
def draw(self, layout='circular', figsize=None):
        """Draw graph in a matplotlib environment

        Parameters
        ----------
        layout : str
            possible are 'circular', 'shell', 'spring'
        figsize : tuple(int)
            tuple of two integers denoting the mpl figsize

        Returns
        -------
        fig : figure
        """
        layouts = {
            'circular': nx.circular_layout,
            'shell': nx.shell_layout,
            'spring': nx.spring_layout
        }
        figsize = (10, 10) if figsize is None else figsize
        fig = plt.figure(figsize=figsize)
        ax = fig.add_subplot(1, 1, 1)
        ax.axis('off')
        ax.set_frame_on(False)
        g = self.nxGraph
        weights = [abs(g.edge[i][j]['weight']) * 5 for i, j in g.edges()]
        nx.draw_networkx(g, pos=layouts[layout](g), ax=ax, edge_cmap=plt.get_cmap('Reds'),
                         width=2, edge_color=weights)
        return fig
项目:nelpy    作者:nelpy    | 项目源码 | 文件源码
def draw_transmat_graph(G, edge_threshold=0, lw=1, ec='0.2', node_size=15):

    num_states = G.number_of_nodes()

    edgewidth = [ d['weight'] for (u,v,d) in G.edges(data=True)]
    edgewidth = np.array(edgewidth)
    edgewidth[edgewidth<edge_threshold] = 0

    labels = {}
    labels[0] = '1'
    labels[1]= '2'
    labels[2]= '3'
    labels[num_states-1] = str(num_states)

    npos=circular_layout(G, scale=1, direction='CW')
    lpos=circular_layout(G, scale=1.15, direction='CW')

    nx.draw_networkx_edges(G, npos, alpha=0.8, width=edgewidth*lw, edge_color=ec)

    nx.draw_networkx_nodes(G, npos, node_size=node_size, node_color='k',alpha=0.8)
    ax = plt.gca()
    nx.draw_networkx_labels(G, lpos, labels, fontsize=18, ax=ax); # fontsize does not seem to work :/

    ax.set_aspect('equal')

    return ax
项目:nelpy    作者:nelpy    | 项目源码 | 文件源码
def double_circular_layout(Gi, scale=1, center=None, dim=2, direction='CCW'):
    inner=circular_layout(Gi, center=center, dim=dim, scale=scale, direction=direction)
    outer=circular_layout(Gi, center=center, dim=dim, scale=scale*1.3, direction=direction)

    num_states = Gi.number_of_nodes()

    npos = {}
    for k in outer.keys():
        npos[k+num_states] = outer[k]

    npos.update(inner)

    return npos
项目:single-ways    作者:smart-ways    | 项目源码 | 文件源码
def view_city(self):
        pos = nx.circular_layout(self.city)
        node_labels = {}
        for u in self.city.nodes():
            node_labels[u] = u
        nx.draw(self.city, pos)
        nx.draw_networkx_labels(self.city, pos, labels=node_labels)
        self.btnViewCity.setEnabled(True)
        plt.show()
项目:py-ipv8    作者:qstokkink    | 项目源码 | 文件源码
def draw(self, filename="network_view.png"):
        """
        Draw this graph to a file, for debugging.
        """
        import matplotlib.pyplot as plt
        plt.clf()
        pos = circular_layout(self.graph)
        draw(self.graph, pos, with_labels=False, arrows=False, hold=False,
             edge_color=[self.graph[u][v]['color'] for u,v in self.graph.edges()],
             node_color=['orange' if v in self._all_addresses else 'green' for v in self.graph.nodes()])
        plt.savefig(filename)
项目:bayesianpy    作者:morganics    | 项目源码 | 文件源码
def circular_layout(self, graph):
        return nx.circular_layout(graph, center=[0.5,0.5])
项目:symd    作者:xym-tool    | 项目源码 | 文件源码
def plot_module_dependency_graph(graph):
    """
    Plot a graph of specified yang modules. this function is used to plot
    both the full dependency graph of all yang modules in the DB, or a
    subgraph of dependencies for a specified module
    :param graph: Graph to be plotted
    :return: None
    """


    # fixed_pos = { 'ietf-interfaces':(0.01,0.01) }
    # fixed_nodes = fixed_pos.keys()
    # pos = nx.spring_layout(graph, iterations=200,
    #                        pos=fixed_pos, fixed=fixed_nodes)
    #pos = nx.circular_layout(graph)

    pos = nx.spring_layout(graph, iterations=2000)

    # Draw RFC nodes (yang modules) in red
    nx.draw_networkx_nodes(graph, pos=pos, nodelist=prune_graph_nodes(graph, RFC_TAG), node_size=200,
                           node_shape='s', node_color='red', alpha=0.5, linewidths=0.5)

    # Draw draft nodes (yang modules) in green
    nx.draw_networkx_nodes(graph, pos=pos, nodelist=prune_graph_nodes(graph, DRAFT_TAG), node_size=200,
                           node_shape='o', node_color='green', alpha=0.5, linewidths=0.5)

    # Draw unknown nodes (yang modules) in orange
    nx.draw_networkx_nodes(graph, pos=pos, nodelist=prune_graph_nodes(graph, UNKNOWN_TAG), node_size=200,
                           node_shape='^', node_color='orange', alpha=1.0, linewidths=0.5)

    # Draw edges in light gray (fairly transparent)
    nx.draw_networkx_edges(graph, pos=pos, alpha=0.25, linewidths=0.1, arrows=False)

    # Draw labels on nodes (modules)
    nx.draw_networkx_labels(graph, pos=pos, font_size=10, font_weight='bold', alpha=1.0)
项目:my_experiment    作者:Giuliao    | 项目源码 | 文件源码
def show_network(self):
        import time
        plt.figure(time.time())
        for v in self.G.nodes():
            self.G.node[v]['state'] = str(v)

        node_labels = nx.get_node_attributes(self.G, 'state')
        pos = nx.circular_layout(self.G)
        nx.draw_networkx_labels(self.G, pos, node_labels=node_labels)
        nx.draw(self.G, pos)
        plt.savefig('./assets/result2.png')
        # plt.show(block=False)
        plt.close()
项目:nelpy    作者:nelpy    | 项目源码 | 文件源码
def circular_layout(G, scale=1, center=None, dim=2, direction='CCW'):
    # dim=2 only
    """Position nodes on a circle.

    Parameters
    ----------
    G : NetworkX graph or list of nodes

    scale : float
        Scale factor for positions

    center : array-like or None
        Coordinate pair around which to center the layout.

    dim : int
        Dimension of layout, currently only dim=2 is supported

    Returns
    -------
    pos : dict
        A dictionary of positions keyed by node

    Examples
    --------
    >>> G = nx.path_graph(4)
    >>> pos = nx.circular_layout(G)

    Notes
    -----
    This algorithm currently only works in two dimensions and does not
    try to minimize edge crossings.

    """

    G, center = _process_params(G, center, dim)

    if len(G) == 0:
        pos = {}
    elif len(G) == 1:
        pos = {nx.utils.arbitrary_element(G): center}
    else:
        # Discard the extra angle since it matches 0 radians.
        theta = np.linspace(0, 1, len(G) + 1)[:-1] * 2 * np.pi
        theta = theta.astype(np.float32)
        if direction == 'CCW':
            pos = np.column_stack([np.cos(theta), np.sin(theta)])
        else:
            pos = np.column_stack([np.sin(theta), np.cos(theta)])
        pos = rescale_layout(pos, scale=scale) + center
        pos = dict(zip(G, pos))

    return pos
项目:symd    作者:xym-tool    | 项目源码 | 文件源码
def plot_module_dependency_graph(graph):
    """
    Plot a graph of specified yang modules. this function is used to plot
    both the full dependency graph of all yang modules in the DB, or a
    subgraph of dependencies for a specified module
    :param graph: Graph to be plotted
    :return: None
    """


    # fixed_pos = { 'ietf-interfaces':(0.01,0.01) }
    # fixed_nodes = fixed_pos.keys()
    # pos = nx.spring_layout(graph, iterations=200,
    #                        pos=fixed_pos, fixed=fixed_nodes)
    #pos = nx.circular_layout(graph)

    pos = nx.spring_layout(graph, iterations=2000)

    # Draw RFC nodes (yang modules) in red
    nx.draw_networkx_nodes(graph, pos=pos, nodelist=prune_graph_nodes(graph, RFC_TAG), node_size=200,
                           node_shape='s', node_color='red', alpha=0.5, linewidths=0.5)

    # Draw draft nodes (yang modules) in green
    nx.draw_networkx_nodes(graph, pos=pos, nodelist=prune_graph_nodes(graph, DRAFT_TAG), node_size=200,
                           node_shape='o', node_color='green', alpha=0.5, linewidths=0.5)

    # Draw unknown nodes (yang modules) in orange
    nx.draw_networkx_nodes(graph, pos=pos, nodelist=prune_graph_nodes(graph, UNKNOWN_TAG), node_size=200,
                           node_shape='^', node_color='orange', alpha=1.0, linewidths=0.5)

    # Draw edges in light gray (fairly transparent)
    nx.draw_networkx_edges(graph, pos=pos, alpha=0.25, linewidths=0.1, arrows=False)

    # Draw labels on nodes (modules)
    nx.draw_networkx_labels(graph, pos=pos, font_size=10, font_weight='bold', alpha=1.0)


##############################################################################
# symd - Show Yang Module Dependencies.
#
# A program to analyze and show dependencies between yang modules
##############################################################################