2

I'm trying to make a network figure. It is basically a subnetwork of 1st and 2nd neighbors from a particular node w/ corresponding colors. I've included my functions to create the 1st and 2nd neighborhood with colors along with creating the subgraph from these lists.

I THINK that the color part is working but I can't really tell since the layout is so small. I've tried expanding the spring_layout(G) w/ How to increase node spacing for networkx.spring_layout but it didn't seem to work.

How can I create a network figure where I can ACTUALLY visualize the nodes and their connections of this magnitude? (~ 1000 nodes) I was going to add with_labels but didn't include that in the image for simplicity.

My main goal is to color the root node "c" (cyan), the 1st neighbors "b" (blue), and the 2nd neighbors "g" (green) with a figure large enough for me to read labels and see connections.

I believe that I have provided enough code, please let me know if I need to add more and I will edit.

import networkx as nx
from collections import defaultdict

def neighborhood(G,node_list):
    #G is the main nx.Graph() object that has ALL the nodes (~7000 nodes)
    D_node_neighborhood = defaultdict(list)
    for node in node_list:
        #Color 1st neighbors blue
        for n1 in G.neighbors(node):
            D_node_neighborhood[node].append((n1,"b"))
            #Color 2nd neighbors green
            for n2 in G.neighbors(n1):
                D_node_neighborhood[node].append((n2,"g"))
    return(D_node_neighborhood)

def subnetwork(G,D_node_neighborhood,root_color = "c"):
    D_node_subgraph = {}
    for node,nghbr_color in D_node_neighborhood.items():
        neighbors = [entry[0] for entry in nghbr_color]
        colors = [entry[1] for entry in nghbr_color]
        H = G.subgraph(neighbors + [node]) #Add root note to neighbors list and create subgraph
        D_node_subgraph[node] = (H,colors + [root_color]) #Do the same with the colors
    return(D_node_subgraph)

D_node_neighborhood.keys()[0] #Grab first one, this object is a list of tuples ("node-name","color-of-node") around 1000
G,colors = D_node_subgraph[node] #Separate them out
nx.draw(G,node_color=colors,node_size=10,alpha=0.8) #Draw the graph w/ corresponding colors

enter image description here

Community
  • 1
  • 1
O.rka
  • 29,847
  • 68
  • 194
  • 309

1 Answers1

0

If I understand your question correctly you want to have a larger figure.

You can get the figure and increase its size after the actual drawing.

nx.draw(G)
fig = plt.gcf()
fig.set_size_inches((10, 10))
plt.show()

To explore your graph I suggest a tool better suited for the task: Gephi. You can visualize your network and apply different layout consecutively, you can even move nodes by hand.

To go from Networkx to Gephi simply export the graph in graphml format like so:

nx.write_graphml(G, 'your_graph.graphml')
Kirell
  • 9,228
  • 4
  • 46
  • 61