Questions tagged [networkx]

NetworkX is a Python package for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks. Use this tag for questions about how to install or use the package, for clarification on any of its methods, or for help with algorithms written with it. Do not use this tag for questions related to troubleshooting computer network connectivity.

NetworkX is a Python package for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks. Use this tag for questions about how to install or use the package, for clarification on any of its methods, or for help with algorithms written with it.

Features:

  • Python language data structures for graphs, digraphs, and multigraphs.
  • Nodes can be "anything" (e.g. text, images, XML records)
  • Edges can hold arbitrary data (e.g. weights, time-series)
  • Generators for classic graphs, random graphs, and synthetic networks
  • Standard graph algorithms
  • Network structure and analysis measures
  • Basic graph drawing
  • Open source BSD license

Tutorials:

The Package Homepage contains the download, documentation, and examples that should be looked at BEFORE posting a question here.

6006 questions
157
votes
6 answers

how to draw directed graphs using networkx in python?

I have some nodes coming from a script that I want to map on to a graph. In the below, I want to use Arrow to go from A to D and probably have the edge colored too in (red or something). This is basically, like a path from A to D when all other…
brain storm
  • 30,124
  • 69
  • 225
  • 393
103
votes
5 answers

Is there a way to guarantee hierarchical output from NetworkX?

I'm trying to produce a flow diagram of a tree structure. I've been able to create representative graphs with networkx, but I need a way to show the tree structure when I output a plot. I'm using matplotlib.pylab to plot the graph. I need to show…
max
  • 2,346
  • 4
  • 26
  • 34
91
votes
5 answers

Storing and Accessing node attributes python networkx

I have a network of nodes created using python networkx. i want to store information in nodes such that i can access the information later based on the node label (the name of the node) and the field that in which the information has been stored…
user1295112
  • 1,013
  • 1
  • 8
  • 4
89
votes
2 answers

Plotting networkx graph with node labels defaulting to node name

NetworkX is powerful but I was trying to plot a graph which shows node labels by default and I was surprised how tedious this seemingly simple task could be for someone new to Networkx. There is an example which shows how to add labels to the…
Pranjal Mittal
  • 10,772
  • 18
  • 74
  • 99
79
votes
4 answers

How to set colors for nodes in NetworkX?

I created my graph, everything looks great so far, but I want to update color of my nodes after creation. My goal is to visualize DFS, I will first show the initial graph and then color nodes step by step as DFS solves the problem. If anyone is…
Gokhan Arik
  • 2,626
  • 2
  • 24
  • 50
70
votes
3 answers

Get all edges linked to a given node in a networkx graph

Just wondering if there is convenient networkx function that returns a list of edges connected to a given node (or nodes) (e.g. my_node_name) in a graph (e.g. G). I can do it this way: edlist=[] for ed in G.edges(): if 'my_node_name' in ed: …
Lee
  • 29,398
  • 28
  • 117
  • 170
68
votes
3 answers

Combine (join) networkx Graphs

Say I have two networkx graphs, G and H: G=nx.Graph() fromnodes=[0,1,1,1,1,1,2] tonodes=[1,2,3,4,5,6,7] for x,y in zip(fromnodes,tonodes): G.add_edge(x,y) H=nx.Graph() fromnodes=range(2,8) tonodes=range(8,14) for x,y in zip(fromnodes,tonodes): …
Lee
  • 29,398
  • 28
  • 117
  • 170
64
votes
3 answers

networkx - change color/width according to edge attributes - inconsistent result

I managed to produce the graph correctly, but with some more testing noted inconsistent result for the following two different line of codes: colors = [h.edge[i][j]['color'] for (i,j) in h.edges_iter()] widths = [h.edge[i][j]['width'] for (i,j) in…
timeislove
  • 1,075
  • 1
  • 9
  • 14
63
votes
5 answers

Improving Python NetworkX graph layout

I am having some problems in visualizing the graphs created with python-networkx, I want to able to reduce clutter and regulate the distance between the nodes (I have also tried spring_layout, it just lays out the nodes in an elliptical fashion).…
Sayan
  • 2,662
  • 10
  • 41
  • 56
55
votes
1 answer

Sharing Memory in Gunicorn?

I have a large read-only data structure (a graph loaded in networkx, though this shouldn't be important) that I use in my web service. The webservice is built in Flask and then served through Gunicorn. Turns out that for every gunicorn worker I spin…
Eli
  • 36,793
  • 40
  • 144
  • 207
54
votes
3 answers

How to increase node spacing for networkx.spring_layout

Drawing a clique graph with import networkx as nx .... nx.draw(G, layout=nx.spring_layout(G)) produces the following picture: Obviously, the spacing between the nodes (e.g., the edge length) needs to be increased. I've googled this and found this…
clstaudt
  • 21,436
  • 45
  • 156
  • 239
53
votes
3 answers

How can I specify an exact output size for my networkx graph?

The above is the output of my current graph. However, I have yet to manage what I am trying to achieve. I need to output my graph in a larger size so that each node/edge can be viewed with ease. I've tried nx.draw(G, node_size=size), but that only…
user428370
52
votes
2 answers

Add edge-weights to plot output in networkx

I am doing some graph theory in python using the networkx package. I would like to add the weights of the edges of my graph to the plot output. How can I do this? For example How would I modify the following code to get the desired output? import…
smilingbuddha
  • 14,334
  • 33
  • 112
  • 189
50
votes
8 answers

Can one get hierarchical graphs from networkx with python 3?

I am trying to display a tree graph of my class hierarchy using networkx. I have it all graphed correctly, and it displays fine. But as a circular graph with crossing edges, it is a pure hierarchy, and it seems I ought to be able to display it as a…
NickDanger66
  • 503
  • 1
  • 5
  • 7
49
votes
2 answers

Draw different color for nodes in networkx based on their node value

I have a large graph of nodes and directed edges. Furthermore, I have an additional list of values assigned to each node. I now want to change the color of each node according to their node value. So e.g., drawing nodes with a very high value red…
fsociety
  • 1,791
  • 4
  • 22
  • 32
1
2 3
99 100