简体   繁体   中英

How to graph nodes on a grid in networkx

Just started learning Network Science and I'm a novice in Python so I've been having a hard time figuring this out even after reading a good bit of the networkx documentation. I need to compare the distance between all the nodes and generate an edge in the event the distance is less than d.

1) How to I compare node 1 to nodes (2...99) and then compare node 2 to nodes (3...99), etc. If there's a better way to do it than O(n^2) please show me.

2) How can I use the x,y coordinates stored in node_loc{} to graph each node to a coordinate plane?

import random, math
import matplotlib.pyplot as plt
import numpy as np
import networkx as nx
import pylab

# Calc distance given (x1,x2,y1,y2)
def distance(x1,x2,y1,y2):
    return math.sqrt(((x2-x1)**2)+((y2-y1)**2))

# Generate coordinate value
def coord_val():
    # node needs x and y coordinates (floats) from 0->100
    return random.uniform(0.0,100.0)

def main():
    # The distance that applies to link generation
    d = 20

    # Make a graph and name it
    g = nx.Graph(name = "100x100 Field Random Network")

    # Generate 100 nodes
    for num in range(0,100):

        # generate a dict with the node's location
        node_loc = {'x': coord_val(), 'y': coord_val()}

        # Add node with x,y dict
        g.add_node(num,node_loc)

    # Check node n against node n+1
    for n,d in g.nodes(data=True):
        if n == 99:
            break

        # I don't think this loop is correct
        for rl in g.nodes(data=True):
            # Don't go out of bounds on the loop
            if n == 99:
                break

            # grab coordinates from nodes
            y1=g.node[n]['y']
            x1=g.node[n]['x']
            y2=g.node[n+1]['y']
            x2=g.node[n+1]['x']

            # Check the distance, if < d, generate edge
            if distance(x1,x2,y1,y2) < d:
                # add edge
                g.add_edge(n,n+1)

    # plot
    # draw_random draws it on a plane, but randomly :(
    nx.draw_random(g,node_size=50)

    plt.show()

if __name__ == '__main__':
    main()

NetworkX has nx.grid_2d_graph , a Graph generator , that returns the 2d grid graph of mxn nodes, each being connected to its nearest neighbors. By default its labels will be the coordinates of the grid.

If we were interested in having the nodes as coordinates , we could rotate the positions so the origin is at the top left corner. Otherwise you can just leave it as is. Here's an example:

import networkx as nx
from matplotlib import pyplot as plt

G = nx.grid_2d_graph(3,3)

plt.figure(figsize=(6,6))
pos = {(x,y):(y,-x) for x,y in G.nodes()}
nx.draw(G, pos=pos, 
        node_color='lightgreen', 
        with_labels=True,
        node_size=600)

在此处输入图片说明

Note that nx.grid_2d_graph will generate grid graphs with up to an arbitrarily large m and n , an by positioning the labels you can also plot the grid of coordinates just as above:

在此处输入图片说明

看看 networkx 随机几何图形生成器,以实现您正在寻找的图形https://github.com/networkx/networkx/blob/master/networkx/generators/geometric.py#L79有一个例子显示输出以及如何在此处绘制它https://networkx.github.io/documentation/stable/tutorial.html#drawing-graphs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM