Matplotlib and Networkx - drawing a self loop node

后端 未结 1 1249
故里飘歌
故里飘歌 2020-12-09 20:28

I have this function and I want to draw a self loop. How can I do that?
The edge exists but I think it\'s just a point in this exemple is (1,1) and I couldn\'t add the n

相关标签:
1条回答
  • 2020-12-09 21:23

    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

    0 讨论(0)
提交回复
热议问题