Matplotlib and Networkx - drawing a self loop node

馋奶兔 提交于 2019-12-04 08:18:33

It seems that your approach is quite advanced a use of matplotlib, but I would still recommend using a specialized graph plotting library (as does the networkx documentation(. As graphs get bigger, more problems arise -- but problems that have already been solved in those libraries.

A "go-to" option is graphviz, which handles drawing multi-graphs reasonably well. You can write dot files from networkx graphs, and then use one of the graph drawing tools (e.g. dot, neato, etc).

Here is an example, building on graph attributes and multigraph edge attributes:

import networkx as nx
from networkx.drawing.nx_agraph import to_agraph 

# define the graph as per your question
G=nx.MultiDiGraph([(1,2),(1,1),(1,2),(2,3),(3,4),(2,4), 
    (1,2),(1,2),(1,2),(2,3),(3,4),(2,4)])

# add graphviz layout options (see https://stackoverflow.com/a/39662097)
G.graph['edge'] = {'arrowsize': '0.6', 'splines': 'curved'}
G.graph['graph'] = {'scale': '3'}

# adding attributes to edges in multigraphs is more complicated but see
# https://stackoverflow.com/a/26694158                    
G[1][1][0]['color']='red'

A = to_agraph(G) 
A.layout('dot')                                                                 
A.draw('multi.png')   

Note that you can also easily invoke the drawing from within an ipython shell: https://stackoverflow.com/a/14945560

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!