Convert networkx graph to dot format in python - python

I am converting my networkx graph using the following code.
nx.drawing.nx_pydot.write_dot(G,path)
It creates a correct dot format which I can visualize later using graphviz interface. However, instead of adding multiple existing lines(Arcs, edges whatever you say), it creates a single (or two if there is an edge in the opposite direction). I just want to have all lines to be preserved in the dot format. How can I do that?

The creation of Networkx graph in the first place was not correct. I changed the line G=nx.DiGraph(directed=True) to G=nx.MultiDiGraph(directed=True). Now I do not have that problem.

Related

Adding sub nodes to networkx (with image)

I would like to create a networkx graph that looks more or less like this, but I haven't been able to find a way for it to display the way I need. The large nodes and edges display fine, but I haven't been able to find how to add the small nodes.
networkx.draw() has an optional argument node_size:
node_size (scalar or array, optional (default=300)) – Size of nodes. If an array is specified it must be the same length as nodelist.
If you want to draw nodes with various sizes, you should specify the array of sizes. You can also use some kind of list generator.
P.S. I don't recommend to use basic networkx drawing functional. There are many powerful visualization libraries better than networkx. Even in networkx docs you can find the same opinion. One can use Gephi, Graphviz (with various libraries) or Cytoscape for really HUGE graphs.

Generating Network graph in python

I am trying to generate a network graph for 200+ nodes and 300+ edges using networkx using python from a file. I was able to generate and plot the graph using matplotlib in my Juypter Notebook, but its not looking good and nodes are so tightly packed.
Is there any other python package help to generate network graph ??.
My aim is to generate graph for whole data set so that I can find dependency between nodes.
If nodes being too close together is the issue, try using the draw_spring()function of networkx.
https://networkx.github.io/documentation/networkx-2.0/reference/generated/networkx.drawing.nx_pylab.draw_spring.html#networkx.drawing.nx_pylab.draw_spring
It simulates what would happen if each edge were a spring and the network were picked up and spun around, seperating the nodes, especially the ones which are not connected via many paths.
If this does not work initially, you can use the parameters of the function that draw_spring() wraps, the spring_layout() function. Try adjusting the k value parameter. This should allow you to manipulate the rough distance between nodes.
https://networkx.github.io/documentation/networkx-2.0/reference/generated/networkx.drawing.layout.spring_layout.html#networkx.drawing.layout.spring_layout
Alternatively, there are additional draw functions you might find more useful than draw_spring(). You can find them in the draw section here: https://networkx.github.io/documentation/networkx-2.0/reference/drawing.html
I can recommend using Netwulf. Input a networkx.Graph object to netwulf.visualize, and launch the visualization in a new browser window. The result and data can be posted back to Python.
Disclaimer: I co-author Netwulf.

networkx: giving a wrong direction for an edge

I am trying to read some dataframe to a networkx graph like this:
bipartGraph = nx.Graph()
bipartGraph.add_edge(618254814, 14337833)
bipartGraph.add_edge(618254882, 12087274)
When I display the edges using the bipartGraph.edges() function, I get the below:
[(14337833, 618254814), (618254882, 12087274)]
So, the direction of the first edge is reversed. I am trying to build a bipartite graph from a dataframe which I need to reuse to build an another graph. Is there any specific property of networkx I am missing?
At least in networkx 1.11 if your graph is undirected, an edge that is added as (u,v) may be returned as either (v,u) or (u,v). [I think this may have been changed in version 2.0]. This is because the underlying data structure is a dict, and there is no guarantee that a dict returns values in any particular order: Why items order in a dictionary changed in Python?.
If you really want a direction on your edge, you should make your graph a DiGraph.

Removing a internal PropertyMap from a Graph in graph-tool

For the purpose to save a graph without a specific PropertyMap (added with g.vp.foo = vprop) we need to remove it from a Graph g.
The PropertyMap is already present after the loading of the Graph from file.
How can I either remove a PropertyMap from the loaded Graph? (Or alternatively is there a way to copy only the Graph into a new one, without the PropertyMaps?)
Duhhh, found the answer just some lines below from where I copied the example above:
del g.vertex_properties["foo"]

Fixed position hierarchical output from NetworkX without graphviz?

I'm trying to use Python to plot simple hierarchical tree. I'm using the networkx module. I declared a simple graph
G=networkx.DiGraph()
After adding nodes and edges into G, I tried using
nx.draw(G)
or
nx.draw_networkx(G)
to plot. The output plot hierarchy are all correct, but the position of the nodes appears all random on the graph. Even worse, each time I ran the script, the node position are different.
There is a solution provided in a similar question which requires graphviz package.
pos=nx.graphviz_layout(G,prog='dot')
nx.draw(G,pos,with_labels=False,arrows=False)
Unfortunately I'm not allowed to install graphviz. So I'm seeking alternative solution here.
From graphviz solution, it looks like it's only needed to compute position of the node. Is it correct to say that, as long as I specify a list of coordinates to the draw command, I would be able to plot node at the correct location?
Thanks
UPDATE (15 Apr 2015) Look at my answer here for code that I think will do what you're after.
So networkx doesn't make it particularly easy to use the graphviz layout if you don't have graphviz because it's a better idea to use graphviz's algorithm if you're trying to reproduce graphviz's layout.
But, if you're willing to put in the effort to calculate the position for each node this is straightforward (as you guessed). Create a dict saying where each node's position should be.
pos = {}
for node in G.nodes():
pos[node] = (xcoord,ycoord)
nx.draw(G,pos)
will do it where xcoord and ycoord are the coordinates you want to have.
The various plotting commands are described here

Categories

Resources