Python networkx graph appears jumbled when drawn in matplotlib -


I am testing to create a network graph on "network x"; My problem is that when I try to plot these articles using "matplotlib", nodes, edges, and labeled hips appear. I want to connect labels to the right node, and I want to see the edges as they are adding nodes.

code

  import as NX try networkx: import as plt except Matplotlibkpyplot: G = nx.MultiGraph () strList = [ "Rick James "take," will smith "," Steve Miller "," rackem Willy "," little tunechi "," Ben Franklin "] strList2 = [" Jules Cesar "," Atticus Finch "," Al Capone "," Abe Lincoln "," Walt White "," Doctor Seuss "] i = 0 while i & lt; Lane (strList): g.add_edge (strList [i], strList2 [i]) i = i + 1 nx.draw_networkx_nodes (g, status = nx.spring_layout (g), nodelist = g.nodes ()) nx.draw_networkx_edges (g, status = nx.spring_layout (g), edgelist = g.edges ()) nx.draw_networkx_labels (g, status = nx.spring_layout (g)) # plt.savefig ( "testImage.png") plt.show ( )  

image

[1]

I want my connections to be like this:

  Rick James & lt; - & gt; Jules Kaiser Smith & lt; - & gt; Atkins Finch Steve Miller & lt; - & gt; > 

Spring layout is stoxtastic (random). An issue you are doing comes from the fact that you have been running this stochastic process from a different time - preparing a different layout for nodes, edges and labels. Where layout make this calculation the same time ,:

  position = nx.spring_layout (g) nx.draw_networkx_nodes (g, status = status, nodelist = g.nodes ()) NX. Draw_networkx_edges (g, pos = pos, edgelist = g.edges ()) nx.draw_networkx_labels (g, pos = pos)  

or the position of the nodes need not personal style / Edges / Labels:

  nx.draw_spring (g)  

I will not claim that this will give you a "good" layout because it Is not on my machine):

spring-layout

< P> One layout may be a better fit:

  nx.draw_circular (g)  

You can read about all the network x layouts, in which GrafWise (suggested by @ThomShobom).


Comments

Popular posts from this blog

Member with no value in F# -

java - Joda Time Interval Not returning what I expect -

c# - Showing a SelectedItem's Property -