I am very new to networkx, apologies if this is very easy. But I am trying to visualize the evolution of a population over time using a networkx graph, how would that be accomplished?
The closest thing I have found in their docs, visually, is this multipartite layout: https://networkx.org/documentation/stable/auto_examples/drawing/plot_multipartite_graph.html#sphx-glr-auto-examples-drawing-plot-multipartite-graph-py
The graph of which looks like this: enter image description here
Code from their example:
subset_sizes = [5, 5, 4, 3, 2, 4, 4, 3]
subset_color = [
"gold",
"violet",
"violet",
"violet",
"violet",
"limegreen",
"limegreen",
"darkorange",
]
def multilayered_graph(*subset_sizes):
extents = nx.utils.pairwise(itertools.accumulate((0,) + subset_sizes))
layers = [range(start, end) for start, end in extents]
G = nx.Graph()
for (i, layer) in enumerate(layers):
G.add_nodes_from(layer, layer=i)
for layer1, layer2 in nx.utils.pairwise(layers):
G.add_edges_from(itertools.product(layer1, layer2))
return G
G = multilayered_graph(*subset_sizes)
color = [subset_color[data["layer"]] for v, data in G.nodes(data=True)]
pos = nx.multipartite_layout(G, subset_key="layer")
plt.figure(figsize=(8, 8))
nx.draw(G, pos, node_color=color, with_labels=False)
plt.axis("equal")
plt.show()
As an example of what I am looking for, assume in period 1 we have 3 id's:
[1,2,3]
In period 2, we have 4 id's:
[1,2,4,5]
Notice how we have 2 new entrants into the list , and one exit.
I am envisioning a multipartite graph like the one described in the docs, except where each layer's node is connected via one edge just to itself in the next layer, each layer representing a period in a timeseries. Each layers number of nodes, oriented vertically, might increase or decrease depending if more nodes are added than removed, but you might get a sense of how the population changes over time by visualizing it this way.
As a bonus... Each time a new node is added in a given period it is green (1st layer all green), and every time a node exits (is not present in layer+1) , it would be red, otherwise an empty circle/normal node (last layer all empty circles since we don't know layer+1).