I'd like to draw a weighted multi-direction graph of the edge with the coordinates of the node specified using python.
Is there any good way?
I tried drawing using networkx and pyplot, but it didn't work because the arrows overlapped.
nodes = pd.DataFrame([[[1, 3]], [[0, 2]], [[3, 3]], [[5, 2]], [[2, 4]]], columns=['coords'])
nodes.index = ['A', 'B', 'C', 'D', 'E']
edges = pd.DataFrame([['A','B', 4], ['B','A', 20], ['C','B', 3], ['D','D' ,10], ['C','A', 7], ['E','A' ,2], ['D','C' ,15], ['C','E', 8], ['D','A', 2]], columns=['start', 'end', 'weight'])
G = nx.MultiDiGraph()
G.add_nodes_from(nodes.index)
for index, row in edges.iterrows():
G.add_edge(row.start, row.end, weight=row.weight)
nx.draw_networkx_labels(G, nodes.coords, font_size=16)
nx.draw_networkx_edge_labels(G, nodes.coords, weight=edges.weight)
nx.draw(G, nodes.coords)
plt.show()
If you don't want to overlap the edges (arrows), you should use graphviz.
You can shorten the position of the edge by weight (*), but you can also express the weight by label of the edge.
I haven't checked on jupyter-notebook, but I have written it on Anaconda's Spyder.
g With graphviz, it is difficult to place nodes and edges in the desired position, and the goodness of graphviz cannot be utilized.
Below is a sample code.
import graphviz
g=graphviz.Digraph(format='svg')
g.attr('node',shape='circle')
g.attr('node', style='filed')
g.attr('node',filcolor='orange')
g.attr('node', color='orange')
for node in nodes.index:
g.node (node, node)
for index, row in edges.iterrows():
g.attr('edge', weight=str(row.weight))
g.edge(row.start, row.end, label='{weight:'+str(row.weight)+'}')
print(g)
g.view()
When executing, please add it to the code of the person who asked the question.
*Please add the following to the beginning of the code of the person who asked the question to execute it.
import networkx as nx
import pandas aspd
Original image
Drawing with graphviz
623 Uncaught (inpromise) Error on Electron: An object could not be cloned
578 Understanding How to Configure Google API Key
922 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
613 GDB gets version error when attempting to debug with the Presense SDK (IDE)
© 2024 OneMinuteCode. All rights reserved.