How to use the networkx.spring_layout function in networkx

To help you get started, we’ve selected a few networkx examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github reverland / scripts / python / renren.py View on Github external
#nx.draw(G)
    # Judge whether remove the isolated point from graph
    if remove_isolated is True:
        H = nx.empty_graph()
        for SG in nx.connected_component_subgraphs(G):
            if SG.number_of_nodes() > iso_level:
                H = nx.union(SG, H)
        G = H
    # Ajust graph for better presentation
    if different_size is True:
        L = nx.degree(G)
        G.dot_size = {}
        for k, v in L.items():
            G.dot_size[k] = v
        node_size = [G.dot_size[v] * 10 for v in G]
    pos = nx.spring_layout(G, iterations=50)
    nx.draw_networkx_edges(G, pos, alpha=0.2)
    nx.draw_networkx_nodes(G, pos, node_size=node_size, node_color='r', alpha=0.3)
    # Judge whether shows label
    if label_flag is True:
        nx.draw_networkx_labels(G, pos, alpha=0.5)
    #nx.draw_graphviz(G)
    plt.show()

    return G
github supsi-dacd-isaac / krangpower / krangpower / _krangsuit.py View on Github external
def peek(self):
        import matplotlib
        matplotlib.use('Qt5Agg')
        import matplotlib.pyplot as plt

        self._declare_buscoords()

        stpos = {x: y for x, y in self.bus_coords().items() if y is not None}
        if stpos == {}:
            posi = nx.spring_layout(self.graph())
        else:
            posi = nx.spring_layout(self.graph(), pos=stpos, fixed=stpos)

        nx.draw_networkx(self.graph(), pos=posi)
        plt.show()
github mrkwjc / ffnet / gui / animations.py View on Github external
def _calculate_pos(self, graph):
        if self.layout == 'layered':  # only DAGs
            try:
                pos = layered_layout(graph)
            except:
                display_error("Layered layout cannot be created!")
                pos  = None
        elif self.layout == 'spring':
            pos = nx.spring_layout(graph)
        elif self.layout == 'circular':
            pos = nx.circular_layout(graph)
        elif self.layout == 'random':
            pos = nx.random_layout(graph)
        else:
            pos = None  # which gives 'spring' layout...
        return pos
github Hurence / logisland / logisland-plugins / logisland-scripting-plugin / src / main / resources / nltk / parse / dependencygraph.py View on Github external
nonexecutive    JJ      15      NMOD
director        NN      12      PMOD
Nov.    NNP     9       VMOD
29      CD      16      NMOD
.       .       9       VMOD
""")
    tree = dg.tree()
    tree.pprint()
    if nx:
        # currently doesn't work
        import networkx
        from matplotlib import pylab

        g = dg.nx_graph()
        g.info()
        pos = networkx.spring_layout(g, dim=1)
        networkx.draw_networkx_nodes(g, pos, node_size=50)
        # networkx.draw_networkx_edges(g, pos, edge_color='k', width=8)
        networkx.draw_networkx_labels(g, pos, dg.nx_labels)
        pylab.xticks([])
        pylab.yticks([])
        pylab.savefig('tree.png')
        pylab.show()
github wgurecky / StarVine / starvine / vine / base_vine.py View on Github external
def plotVine(self, plotAll=True, savefig=None):
        """!
        @brief Plots the vine's graph structure.
        @param plotAll (optional) Plot the entire vine structure
        @param savefig (optional) filename of output image.
        """
        plt.figure(10, figsize=(6 + 0.3 * self.nLevels, 3 * self.nLevels))
        for i, treeL in enumerate(self.vine):
            plt.subplot(self.nLevels, 1, i + 1)
            plt.title("Tree Level: %d" % i)
            pos = nx.spring_layout(treeL.tree)
            nx.draw(treeL.tree, pos, with_labels=True, font_size=10, font_weight="bold")
            # specifiy edge labels explicitly
            edge_labels = dict([((u, v,), round(d['weight'], 2))
                                for u, v, d in treeL.tree.edges(data=True)])
            nx.draw_networkx_edge_labels(treeL.tree, pos, edge_labels=edge_labels)
        if savefig is not None:
            plt.savefig(savefig)
        plt.close(10)
github vallettea / koala / koala / ast / inout.py View on Github external
def plot_graph(self):
    import matplotlib.pyplot as plt

    pos=networkx.spring_layout(self.G,iterations=2000)
    #pos=networkx.spectral_layout(G)
    #pos = networkx.random_layout(G)
    networkx.draw_networkx_nodes(self.G, pos)
    networkx.draw_networkx_edges(self.G, pos, arrows=True)
    networkx.draw_networkx_labels(self.G, pos)
    plt.show()
github Humpheh / twied / scripts / examples / slp_demo.py View on Github external
true_users[u['user']['id']] = str(u['user']['screen_name'])
            colors[str(u['user']['screen_name'])] = 0

        if u['user']['id'] == user_test:
            target_user[u['user']['id']] = str(u['user']['screen_name'])

    plt.ion()

    G = nx.Graph()
    ego = defaultdict(dict)
    for c in net_connections:
        ego[c[0]][c[1]] = True
        ego[c[1]][c[0]] = True
        G.add_edge(net_users[str(c[0])]['user']['screen_name'], net_users[str(c[1])]['user']['screen_name'])

    pos = nx.spring_layout(G)

    max_iterations = 10

    for i in range(max_iterations):
        #plt.figure(1)

        values = [float(colors.get(node, max_iterations)) / float(max_iterations+5) for node in G.nodes()]
        plt.clf()
        nodes = nx.draw_networkx(G, pos, cmap=plt.get_cmap('jet'), node_color=values, vmin=0, vmax=1)
        plt.show()

        input(">")
        logging.info("Iteration %i" % i)

        # check if all users have been located
        if len(ground_truth) >= len(net_users):
github AB-CE / abce / abce / abcelogger.py View on Github external
network = nx.Graph(strict=True, directed=True)
        for node, attributes in nodes:
            network.add_node(node, shape=attributes['shape'])

        for edge in edges:
            network.add_edge(edge[0], edge[1])

        if self.savegml:
            nx.write_gml(network, self.directory +
                         '/network%05d.gml' % current_round)

        if self.savefig:
            try:
                if self.pos is None or not self.pos_fixed:
                    # positions for all nodes
                    self.pos = nx.spring_layout(network, pos=self.pos)
                plt.figure(1, figsize=self.figsize)
                nodeShapes = set((aShape[1]["shape"]
                                  for aShape in network.nodes(data=True)))
                for aShape in nodeShapes:
                    nodelist = [sNode[0]
                                for sNode in [x for x in network.nodes(data=True) if x[1]["shape"] == aShape]]
                    nx.draw_networkx_nodes(network,
                                           self.pos,
                                           node_shape=aShape,
                                           nodelist=nodelist,
                                           node_color=[colors[node]
                                                       for node in nodelist],
                                           alpha=self.alpha)
                nx.draw_networkx_edges(network, self.pos)
                plt.savefig(self.directory + '/network%05d.png' %
                            current_round, dpi=self.dpi)
github qrzeller / OpenCV-Video-Comparison / src / cpw_project / graph / display.py View on Github external
def community_by_louvain(G,with_labels):
    plt.figure(figsize=(25, 12), dpi=150)

    partition = community.best_partition(G)

    # drawing
    size = float(len(set(partition.values())))
    pos = nx.spring_layout(G)
    count = 0.
    for com in set(partition.values()):
        count = count + 1.
        list_nodes = [nodes for nodes in partition.keys()
                      if partition[nodes] == com]
        nx.draw_networkx_nodes(G, pos, list_nodes, node_size=60,
                               node_color=str(count / size),label=True)

    nx.draw_networkx_edges(G, pos, alpha=0.4)
    if with_labels:
        nx.draw_networkx_labels(G, pos)
    plt.show()
github pyphs / pyphs / pyphs / misc / plots / graphs.py View on Github external
def draw_nodes(graph, ax=None, layout=None, colors=None):
    if ax is None:
        ax = plt.axes(frameon=False)
    if colors is None:
        colors = [node_color(node) for node in graph.nodes()]
    if not hasattr(graph, 'positions'):
        if layout is None:
            layout = LAYOUT
        else:
            assert layout in ('circular', 'spring')
        if layout == 'spring':
            graph.positions = nx.spring_layout(graph, iterations=ITERATIONS)
        elif layout == 'circular':
            graph.positions = nx.circular_layout(graph)
    nx.draw_networkx_nodes(graph, graph.positions, ax=ax,
                           node_size=800, node_color=colors, lw=2)
    nx.draw_networkx_labels(graph, graph.positions, ax=ax)