I have a networkx graph
g
And I want to draw up this visualization
http://mbostock.github.io/d3/talk/20111018/tree.html
What that means is somehow I have to convert my graph into flare.json
https://bitbucket.org/john2x/d3test/src/2ce4dd511244/d3/examples/data/flare.json
To convert this graph into a tree.. I will give a seed node which serves as root of this json and then grow the tree by adding edges to this tree as its children upto say 3 hops..
How do i do this?
If you have a tree you can use the networkx tree_data() function to write the data in JSON tree format for that flare.json example.
The example shown there is:
>>> from networkx.readwrite import json_graph
>>> G = nx.DiGraph([(1,2)])
>>> data = json_graph.tree_data(G,root=1)
To build a tree from your graph either bfs_tree() or dfs_tree() would work.
Or maybe you already know how you want to build a tree from your graph.
There is an example of how to use the d3.js library with NetworkX at https://networkx.github.io/documentation/stable/auto_examples/index.html#javascript
That uses the d3.js force layout code.
Related
I'm using networkx (v2.5) for a dependency-analysis problem, and visualizing the data via graphviz / pygraphviz (v1.7) on ubuntu 20.04. The contents of each node (label field) is a code block - so I'd like it LEFT justified. The problem is I can't seem to change the default (CENTER justified).
X/Y: - my specific need is to make a png from a networkx graph where the node text is left-justified - I believe Graphviz/pygraphviz is the best ~trivial way to do so - but any FOSS way to accomplish this would be fine.
I successfully generate a png as desired, via the following simplified code, but the text is all center-justified.
from networkx import DiGraph, nx_agraph
from networkx.drawing.nx_agraph import write_dot
# graph is created via networkx:
graph = DiGraph()
graph.add_edge("node1", "node2")
graph.nodes["node1"]["label"] = get_code_sniped("node1")
# ...
# and converted / output to dot & png via (internally) pygraphviz
write_dot(graph, "/tmp/foo.dot") # appears correctly output
a_graph = nx_agraph.to_agraph(graph)
a_graph.layout(prog="dot")
# attempt to add attrs per defs in
# https://www.graphviz.org/doc/info/attrs.html#d:labeljust
a_graph.graph_attr.update(labeljust="l") # <----- has no effect on output
a_graph.graph_attr.update(nojustify=True) # <-/
a_graph.draw("/tmp/foo.png") # <-- PNG outputs successfully,
# but all node text is CENTER justified
How can I modify the node text (specifically left-justifying it) in the PNG generated from my networkx graph?
it appears there is (undocumented, AFAICT) upstream/native graphviz behavior w.r.t. inheriting graph-level attributes.
setting a_graph.graph_attr.update(... is insufficient, as it is not inherited by child elements. In order to for instance set fontname, the following works:
for node in formattable_graph.iternodes():
node.attr["fontname"] = "Liberation Mono"
also, for text-justification, one can control this on a Per line basis by changing the line ending "\l" (Note: that is two bytes in python as it's not an actual escape char) and "\r" for left and right (respectively).
this will LEFT justify all lines
graph.nodes["node1"]["label"] = """my
node label
with newlines
""".replace("\n", "\l")
I'm currently trying to build a block model using the python package networkx. I found that the function networkx.quotient_graph can be used for this job:
g_block = nx.quotient_graph(G=g, partition=node_list, relabel=True)
In the next step, I want to export the generated block graph "g_block" to a file to import it afterwards in a visualization tool that supports for example graphml-files.
nx.write_graphml(g_block, 'test_block.graphml')
However, this leads to the error:
{KeyError}class 'networkx.classes.graphviews.SubDiGraph'
Can someone help?
Currently networkx (version 2.2) doesn't support nested graphs in a way you can easily export and visualize. Consider using graphviz for handling your nested graph and export it to a dot format.
For working with a networkx version of the graph, you can transform the pygraphviz to a networkx graph and vise versa by keeping a 'graph' property for nodes (which is semantically a subgraph), similarly to the result of quotient_graph.
Here is an example of transforming a small networkx graph to pygraphviz with subgraphs, and exporting it as a dot file:
import networkx as nx
import pygraphviz as pgv
G = nx.erdos_renyi_graph(6, 0.5, directed=False)
node_list = [set([0, 1, 2, 3]), set([4, 5])]
pgv_G = pgv.AGraph(directed=True)
pgv_G.add_edges_from(G.edges())
for i, sub_graph in enumerate(node_list):
pgv_G.add_subgraph(sub_graph, name=str(i))
print(pgv_G)
pgv_G.write("test_pgv.dot")
Note that netwrokx also allows writing and reading 'dot' format (see example), however since there is no built-in support for nested graphs it's not too helpful for this purpose.
The reason you can't write the quotient_graph is twofold:
In a quotient_graph each node has a 'graph' property, which is a SubDiGraph (or a SubGraph, if the original graph is undirected). A SubDiGraph is a ReadOnlyGraph which means it is not possible to write it using the standard networkx.readwrite utils.
Even if we convert the SubDiGraph to a DiGraph, not every graph file format allows to encode a 'graph' property. For example, graphml format supports primitive properties such as booleans, integers etc. Read more here.
One solution that works is to solve the first issue by overriding the 'graph' property with a DiGraph copy of the original SubDiGraph. The second issue can be simply solved by using another file format (e.g., pickle format can work). Read about all supported formats here.
Following is a working example:
g_block = nx.quotient_graph(G=G, partition=node_list, relabel=True)
def subdigraph_to_digraph(subdigraph):
G = nx.DiGraph()
G.add_nodes_from(subdigraph.nodes())
G.add_edges_from(subdigraph.edges())
return G
for node in g_block:
g_block.nodes[node]['graph'] = subdigraph_to_digraph(g_block.nodes[node]['graph'])
nx.write_gpickle(g_block, "test_block.pickle")
This allows to write and load the nested graph for using with netwrokx, however for the purpose of using the exported file in a visualization tool this is not too helpful.
I want to make a graph with random node positions but it seems that the "pos" attribute for nodes does nothing. Here is a minimal example:
import graphviz
import pylab
from graphviz import Digraph
g = Digraph('G', filename='ex.gv',format='pdf')
g.attr(size='7')
g.node('1',pos='1,2')
g.node('2',pos='2,3')
g.node('3',pos='0,0')
g.edge('1','2')
g.edge('1','3')
graphviz.Source(g)
Any ideas of how achieve that?
Thanks in advance.
Although not 100% clear in the docs, I think pos is not supported in the dot engine on input. The fdp or neato engines do support pos on input for setting the initial position, and if you end the coordinate specification with '!', the coordinates will not change and thus become the final node position.
Play with a live example at https://beta.observablehq.com/#magjac/placing-graphviz-nodes-in-fixed-positions
This standalone python script generates a pdf with the expected node positions:
#!/usr/bin/python
import graphviz
from graphviz import Digraph
g = Digraph('G', engine="neato", filename='ex.gv',format='pdf')
g.attr(size='7')
g.node('1',pos='1,2!')
g.node('2',pos='2,3!')
g.node('3',pos='0,0!')
g.edge('1','2')
g.edge('1','3')
g.render()
Since SO does not support pdf uploading, here's a png image generated with the same code except format='png':
Without the exclamation marks you get:
Without any pos attributes at all you get a similar (but not exactly the same) result:
I am trying to plot a tree from a Pandas dataframe but I don't know which is the correct data structure I must use and how can I solve it.
The dataset has 4 columns: source, destination, application and timemark.
For example, a row in the dataset could be:
192.168.1.1 | 192.168.1.200 | ping | 10:00AM
I would like to plot a tree graph generating a node for each of the sources, and showing the adjacency of each source with the destinations who has communicated with, and an adjacency of each destination with all the applications that this destination has used with the source and finally showing the adjacency of each (source, destination, application) leaf, with all the timemarks of the sessions that used this application between this destination and this source.
Could you please tell me how can I find a Python solution for this?
Thanks a lot!
You should look at NetworkX:
"NetworkX is a Python package for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks."
You can feed your dataset to populate a graph and then plot the graph.
For example:
import networkx as nx
import matplotlib.pyplot as plt
G=nx.Graph()
G.add_node('192.168.1.1')
G.add_node('192.168.1.100')
G.add_node('192.168.1.200')
G.add_edge('192.168.1.1', '192.168.1.100', object='10')
G.add_edge('192.168.1.1', '192.168.1.200', object='11')
nx.draw_networkx(G, pos = nx.shell_layout(G))
nx.draw_networkx_edge_labels(G, pos = nx.shell_layout(G))
plt.show()
would give you:
My task is to generate a graph from a dot file (using pydot) and then convert the same as networkx graph. The problem that I faced was that the attributes of Graph (as I have given in the .dot file) is not present in the networkx graph.
I also used read_dot() function which is again an error. My code is successfully working to visualize graphs but not its attribs.
My code is:
import pydot
import networkx as nx
(graph,) = pydot.graph_from_dot_file('1.dot')
G = nx.nx_pydot.from_pydot(graph)
nx.get_node_attributes(G,'1')
My output is {}
Pls help me to fix the problem
Thanks from Mathan :)