Reputation: 35
I am trying to generate random Graphs, for that i create a list of Nodes with random X and Y coordinates
My Problem is that these Nodes seem to always align in a circle and i want them to be placed random in a square shaped area without a big empty space in the middle. I have tried to use random.random() and random.gauss() and other random.normalvariate(), but i always get a similar result. Do i have to use another random function to achieve this or is there a mistake how i assign the random coordinates? here are some example plots
import networkx as nx
import matplotlib.pyplot as plt
import random
import string
alphabet = list(string.ascii_uppercase)
c=3
r=10
Array = [[0] * c for i in range(r)]
#Fill array with random Coordinates and a Name
for n in range(len(Array) - 1):
Array[n][0] = alphabet.pop(0)
Array[n][1] = random.random()
Array[n][2] = random.random()
G = nx.Graph()
# Add Nodes To Graph
for [name, x, y] in Array:
G.add_node(name, pos=(x, y))
#Plot it
nx.draw(G, with_labels=True, node_size=200)
plt.show()
Upvotes: 0
Views: 52
Reputation: 68
just add change nx.draw(G, with_labels=True, node_size=200)
to nx.draw(G,pos=G.nodes('pos'), with_labels=True, node_size=200)
.
Upvotes: 1