Graph-tool: draw text around the edges

for my thesis, I need to draw some probabilistic graphs of control flow. that is, control flow charts with probabilities depicted at the edges.

I found a graphical tool that seems very useful, as it can use deep copies of existing graphs, and my graphs are very similar.

So my question is, is it possible to draw edge properties (or some lines) on / next to the edges? If this is impossible or very difficult, is there a tool that is better to use in this case?

Edit: I need directional edges that can create loops between two nodes and have different values. Is there any opportunity for this? So I see both meanings? Currently I see a directed graph with a bi-directional edge, but there is only one value on it.

So, for example, in networkx (in the Hooked link) it will look like this:

G = nx.MultiDiGraph() G.add_edge(0,1) G.add_edge(1,0) labels = {(0,1):'foo', (1,0):'bar'} 

So, both "foo" and "bar" are visible, and you can see in which direction they are connected.

But as networkx does this, I get 1 bi-directional edge with 1 label.

+7
source share
2 answers
 import networkx as nx # Sample graph G = nx.Graph() G.add_edge(0,1) G.add_edge(1,2) G.add_edge(2,3) G.add_edge(1,3) labels = {(0,1):'foo', (2,3):'bar'} pos=nx.spring_layout(G) nx.draw(G, pos) nx.draw_networkx_edge_labels(G,pos,edge_labels=labels,font_size=30) import pylab as plt plt.show() 

enter image description here

EDIT: If you need red-labeled multigraphs, I don't think you can do this completely within networkx . However, you can do most of this in python and render and link with another program:

 import networkx as nx G = nx.MultiDiGraph() G.add_edge(0,1, label='A') G.add_edge(1,0, label='B') G.add_edge(2,3, label='foo') G.add_edge(3,1, label='bar') nx.write_dot(G, 'my_graph.dot') 

I use graphviz to turn this into an image. On my Unix machine, I run this from the command line

 dot my_graph.dot -T png > output.png 

Which gives the desired result you are looking for. Please note that graphviz has many ways to customize the look . The above example just gives:

enter image description here

+2
source

You can draw text near the edges using the graph-tool using the "edge_text" option of the graph_draw () function:

 from graph_tool.all import * g = Graph() g.add_vertex(4) label = g.new_edge_property("string") e = g.add_edge(0, 1) label[e] = "A" e = g.add_edge(2, 3) label[e] = "foo" e = g.add_edge(3, 1) label[e] = "bar" e = g.add_edge(0, 3) label[e] = "gnat" graph_draw(g, edge_text=label, edge_font_size=40, edge_text_distance=20, edge_marker_size=40, output="output.png") 

enter image description here

+8
source

All Articles