Correct the position of a subset of nodes in NetworkX spring graphics

Using Networkx in Python, I try to imagine how different critics of the film are biased towards certain manufacturing companies. To show this on the graph, my idea is to fix the position of each production company node in a separate place in a circle, and then use the spring_layout algorithm to place the remaining film critic nodes, so that one can easily see how some critics are more interested in certain production companies.

My problem is that I canโ€™t fix the initial position of production node companies. Of course, I can correct their situation, but then this is just a random case, and I do not want this - I want them to be in a circle. I can calculate the position of all the nodes and then set the position of the production nodes of the company, but this exceeds the purpose of using the spring_layout algorithm, and I get something ridiculous:

enter image description here

Any ideas on how to do this correctly?

Currently my code is doing this:

def get_coordinates_in_circle(n): return_list = [] for i in range(n): theta = float(i)/n*2*3.141592654 x = np.cos(theta) y = np.sin(theta) return_list.append((x,y)) return return_list G_pc = nx.Graph() G_pc.add_edges_from(edges_2212) fixed_nodes = [] for n in G_pc.nodes(): if n in production_companies: fixed_nodes.append(n) pos = nx.spring_layout(G_pc,fixed=fixed_nodes) circular_positions = get_coordinates_in_circle(len(dps_2211)) i = 0 for p in pos.keys(): if p in production_companies: pos[p] = circular_positions[i] i += 1 colors = get_node_colors(G_pc, "gender") nx.draw_networkx_nodes(G_pc, pos, cmap=plt.get_cmap('jet'), node_color=colors, node_size=50, alpha=0.5) nx.draw_networkx_edges(G_pc,pos, alpha=0.01) plt.show() 
+6
source share
1 answer

To create a chart and set multiple positions:

 import networkx as nx G=nx.Graph() G.add_edges_from([(1,2),(2,3),(3,1),(1,4)]) #define G fixed_positions = {1:(0,0),2:(-1,2)}#dict with two of the positions set fixed_nodes = fixed_positions.keys() pos = nx.spring_layout(G,pos=fixed_positions, fixed = fixed_nodes) nx.draw_networkx(G,pos) 

enter image description here

Your problem is that you calculate the positions of all nodes before setting the positions of fixed nodes.

Move pos = nx.spring_layout(G_pc,fixed=fixed_nodes) after setting pos[p] for fixed nodes and change it to pos = nx.spring_layout(G_pc,pos=pos,fixed=fixed_nodes)

dict pos stores the coordinates of each node. You should quickly look through the documentation . In particular,

pos : dict or None optional (default = None). Starting positions for nodes as a dictionary with node as keys and values โ€‹โ€‹as a list or tuple. If "No" then use random starting positions.

fixed : list or None optional (default = none). Nodes should be fixed in the starting position. list or None optional (default = None)

You say to keep these nodes in the starting position, but you did not tell them what this initial position should be. Therefore, I would believe that this is a random assumption for this initial position and fixes it. However, when I check this, it looks like I encountered an error. It seems that if I say (my version) networkx to keep the nodes in [1,2] as fixed, but I donโ€™t talk about what their positions are, I get an error message (at the bottom of this answer). Therefore, I am surprised that your code is running.


For some other code enhancements using lists:

 def get_coordinates_in_circle(n): thetas = [2*np.pi*(float(i)/n) for i in range(n)] returnlist = [(np.cos(theta),np.sin(theta)) for theta in thetas] return return_list G_pc = nx.Graph() G_pc.add_edges_from(edges_2212) circular_positions = get_coordinates_in_circle(len(dps_2211)) #it not clear to me why you don't define circular_positions after #fixed_nodes with len(fixed_nodes) so that they are guaranteed to #be evenly spaced. fixed_nodes = [n for n in G_pc.nodes() if n in production_companies] pos = {} for i,p in enumerate(fixed_nodes): pos[p] = circular_positions[i] colors = get_node_colors(G_pc, "gender") pos = nx.spring_layout(G_pc,pos=pos, fixed=fixed_nodes) nx.draw_networkx_nodes(G_pc, pos, cmap=plt.get_cmap('jet'), node_color=colors, node_size=50, alpha=0.5) nx.draw_networkx_edges(G_pc,pos, alpha=0.01) plt.show() 

Here I see the error:

 import networkx as nx G=nx.Graph() G.add_edge(1,2) pos = nx.spring_layout(G, fixed=[1,2]) --------------------------------------------------------------------------- UnboundLocalError Traceback (most recent call last) <ipython-input-4-e9586af20cc2> in <module>() ----> 1 pos = nx.spring_layout(G, fixed=[1,2]) .../networkx/drawing/layout.pyc in fruchterman_reingold_layout(G, dim, k, pos, fixed, iterations, weight, scale) 253 # We must adjust k by domain size for layouts that are not near 1x1 254 nnodes,_ = A.shape --> 255 k=dom_size/np.sqrt(nnodes) 256 pos=_fruchterman_reingold(A,dim,k,pos_arr,fixed,iterations) 257 if fixed is None: UnboundLocalError: local variable 'dom_size' referenced before assignment 
+7
source

Source: https://habr.com/ru/post/986518/


All Articles