Learner
Learner

Reputation: 39

drowing a 1d lattice graph in python networkx

I am tying to plot 1d lattice graph, but i face with below:

NetworkXPointlessConcept: the null graph has no paths, thus there is no averageshortest path length what is the problem of this code? thanks.

N = 1000
x = 0
for n in range(1, N, 10):
    lattice_1d_distance = list()
    d = 0
    lattice_1d = nx.grid_graph(range(1,n))
    
    d = nx.average_shortest_path_length(lattice_1d)
    lattice_1d_distance.append(d)
    x.append(n)
plt.plot(x, lattice_1d_distance)  
plt.show()

Upvotes: 1

Views: 691

Answers (1)

adrianop01
adrianop01

Reputation: 600

According to networkx documentation nx.grid_graph the input is a list of dimensions for nx.grid_graph

Example

print(list(range(1,4)))
nx.draw(nx.grid_graph(list(range(1,4))) # this is a two dimensional graph, as there is only 3 entries AND ONE ENTRY = 1

[1, 2, 3]

enter image description here

print(list(range(1,5)))
nx.draw(nx.grid_graph([1,2,3,4])) # this is a 3 dimensional graph, as there is only 4 entries AND ONE ENTRY = 1

[1, 2, 3, 4]

enter image description here

Therefore, lets say if you want to 1. plot the distance vs increment of number of dimensions for grid graphs but with constant size for each dimension, or you want to 2. plot the distance vs increment of size for each dimension for grid graphs but with constant number of dimensions:

import networkx as nx
import matplotlib.pyplot as plt

N = 10
x = []
lattice_1d_distance = []
for n in range(1, 10):
    d = 0
    lattice_1d = nx.grid_graph([2]*n) # plotting incrementing number of dimensions, but each dimension have same length.
    d = nx.average_shortest_path_length(lattice_1d)
    lattice_1d_distance.append(d)
    x.append(n)
plt.plot(x, lattice_1d_distance)  
plt.show()

enter image description here

N = 10
x = []
lattice_1d_distance = []
for n in range(1, 10):
    d = 0
    lattice_1d = nx.grid_graph([n,n]) # plotting 2 dimensional graphs, but each graph have incrementing length for each dimension.
    d = nx.average_shortest_path_length(lattice_1d)
    lattice_1d_distance.append(d)
    x.append(n)
plt.plot(x, lattice_1d_distance)  
plt.show()

enter image description here

Also, you need to pay attention to the declaration of list variables.

Upvotes: 2

Related Questions