How to draw oriented graphics using networkx in python?

I have several nodes from a script that I want to display on a graph. In the following, I want to use the arrow to go from A to D and maybe the color is also colored (red or something else). This is basically like a path from A to D when all the other nodes are present. you can imagine that each node, as a city, and traveling from A to D, requires directions (with arrows). This code below is plotting

import networkx as nx import numpy as np import matplotlib.pyplot as plt G = nx.Graph() G.add_edges_from( [('A', 'B'), ('A', 'C'), ('D', 'B'), ('E', 'C'), ('E', 'F'), ('B', 'H'), ('B', 'G'), ('B', 'F'), ('C', 'G')]) val_map = {'A': 1.0, 'D': 0.5714285714285714, 'H': 0.0} values = [val_map.get(node, 0.25) for node in G.nodes()] nx.draw(G, cmap = plt.get_cmap('jet'), node_color = values) plt.show() 

but I want something like the one shown in the image. enter image description hereenter image description here

The title arrows of the first image and the edges of red on the second image. thank

+46
python graph plot networkx
Nov 21 '13 at 22:43
source share
4 answers

Fully rounded example with arrows for red edges only:

 import networkx as nx import numpy as np import matplotlib.pyplot as plt G = nx.DiGraph() G.add_edges_from( [('A', 'B'), ('A', 'C'), ('D', 'B'), ('E', 'C'), ('E', 'F'), ('B', 'H'), ('B', 'G'), ('B', 'F'), ('C', 'G')]) val_map = {'A': 1.0, 'D': 0.5714285714285714, 'H': 0.0} values = [val_map.get(node, 0.25) for node in G.nodes()] # Specify the edges you want here red_edges = [('A', 'C'), ('E', 'C')] edge_colours = ['black' if not edge in red_edges else 'red' for edge in G.edges()] black_edges = [edge for edge in G.edges() if edge not in red_edges] # Need to create a layout when doing # separate calls to draw nodes and edges pos = nx.spring_layout(G) nx.draw_networkx_nodes(G, pos, cmap=plt.get_cmap('jet'), node_color = values, node_size = 500) nx.draw_networkx_labels(G, pos) nx.draw_networkx_edges(G, pos, edgelist=red_edges, edge_color='r', arrows=True) nx.draw_networkx_edges(G, pos, edgelist=black_edges, arrows=False) plt.show() 

Red edges

+42
Nov 21 '13 at 23:04
source share
β€” -

I just included this for completeness. I learned a lot from marius and mdml. Here are the weights. Sorry for the arrows. It seems that I'm not the only one who said that this will not help. I could not do this with the ipython laptop I had to go straight with python with, which was a problem getting my weights in the beginning.

 import networkx as nx import numpy as np import matplotlib.pyplot as plt import pylab G = nx.DiGraph() G.add_edges_from([('A', 'B'),('C','D'),('G','D')], weight=1) G.add_edges_from([('D','A'),('D','E'),('B','D'),('D','E')], weight=2) G.add_edges_from([('B','C'),('E','F')], weight=3) G.add_edges_from([('C','F')], weight=4) val_map = {'A': 1.0, 'D': 0.5714285714285714, 'H': 0.0} values = [val_map.get(node, 0.45) for node in G.nodes()] edge_labels=dict([((u,v,),d['weight']) for u,v,d in G.edges(data=True)]) red_edges = [('C','D'),('D','A')] edge_colors = ['black' if not edge in red_edges else 'red' for edge in G.edges()] pos=nx.spring_layout(G) nx.draw_networkx_edge_labels(G,pos,edge_labels=edge_labels) nx.draw(G,pos, node_color = values, node_size=1500,edge_color=edge_colors,edge_cmap=plt.cm.Reds) pylab.show() 

enter image description here

+34
Nov 22 '13 at 0:24
source share

You need to use a directed graph instead of a graph, i.e.

 G = nx.DiGraph() 

Then create a list of the colors of the edges you want to use and pass them to nx.draw (as @Marius shows).

Combining all this, I get the image below. The picture you are showing is not yet completely different (I don’t know where your scales come from), but much closer! If you want more control over what your output graph looks like (e.g. get arrows similar to arrows), I would check NetworkX with Graphviz .

enter image description here

+15
Nov 21 '13 at 23:08
source share
 import networkx as nx import matplotlib.pyplot as plt g = nx.DiGraph() g.add_nodes_from([1,2,3,4,5]) g.add_edge(1,2) g.add_edge(4,2) g.add_edge(3,5) g.add_edge(2,3) g.add_edge(5,4) nx.draw(g,with_labels=True) plt.draw() plt.show() 

It is simple how to draw a directed graph using python 3.x using networkx. simple representation and can be changed and colored, etc. See the created chart here .

Note. This is just an idea. Weighted edges can be added as

 g.add_edges_from([(1,2),(2,5)], weight=2) 

and therefore built again.

+3
May 22 '17 at 1:47 a.m.
source share



All Articles