How to graph nodes on a grid in networkx
Question: 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()
    


Answer: Take a look at the networkx random geometric graph generator for an
implementation of a graph like you are looking for
<https://github.com/networkx/networkx/blob/master/networkx/generators/geometric.py#L36>
There is an example that shows the output and how to draw it here
<https://networkx.github.io/documentation/latest/examples/drawing/random_geometric_graph.html>

