Reputation: 1
I am attempting to visualize a specific type of graph, that has 3 different types of edge color, based on where the data for the edges came from. The problem is, that I want to animate this process, so in order to do that,I have to add one edge at a time, but I want to have the edge's color predetermined, so that I don't have to loop through my whole graph every time I want to update the graph drawing.
I solved this problem by creating a second graph with the same data as the first, outside of the looped animation function, with the same data, and colors associated to every value, to be used as a legend.
coGraph = nx.Graph()
coGraph.add_nodes_from(nodeArr)
coGraph.add_edges_from(inputD)
coGraph.add_edges_from(transC)
coGraph.add_edges_from(transFin)
colors = []
for edge in coGraph.edges():
print(str(edge))
if [edge[0], edge[1]]in inputD:
colors.append('black')
elif [edge[0], edge[1]] in transC:
colors.append('red')
elif [edge[0], edge[1]] in transFin:
colors.append('purple')
ani1 = matplotlib.animation.FuncAnimation(fig, updateDT, None, fargs = (transC, transFin,inGraph, pos, colors),
interval = 10, repeat = True, blit = False)
But for some reason, when I print the edges of said graph, or just the colors, it is completely out of the inputted order. inputD, transC, and transFin are all documents that contain lines of tuples. Is there any way for me to input the data into the temp graph, and have it retain its order?
Upvotes: 0
Views: 198
Reputation: 23827
The background data type for networkx is a dictionary. There is typically no guarantee of the order given by a dictionary. But in the most recent versions of Python, there is.
To address this, python has an OrderedDict
, and similarly now, networkx has OrderedGraph
(and other variants) The documentation is here.
Upvotes: 1