简体   繁体   中英

how do I draw selfloops using Networkx and matplotlib in Python

I am using networkx and trying to find cycles in graphs. I wrote the following code:

import networkx as nx
import matplotlib.pyplot as plt
import pylab

tg = nx.Graph()

h_lat_dir = {1: [("A", "A"), ("B", "B"), ("A", "B")], 2: [("C", "D")],
    3: [("C", "F")], 4: [("F", "F")], 5: [("C", "C"), ("C", "E"), ("D", "E"), ("E", "E")],
    6: [("D", "D")]}

for wght, edgelist in h_lat_dir.iteritems():
    tg.add_edges_from(edgelist, weight=wght)

print nx.cycle_basis(tg)


nx.write_dot(tg, 'multi.dot')
nx.draw_graphviz(tg)
pylab.show()

the result is

[['A'], ['B'], ['C'], ['F'], ['E', 'D', 'C'], ['D'], ['E']]

and this drawing 在此处输入图片说明

Why can't I see the self_loops? (every vertex has one) Is it possible to draw them somehow?

Using the NetworkX interface to Graphviz (through pygraphviz or pydot):

import networkx as nx
import matplotlib.pyplot as plt

G = nx.Graph()
G.add_edges_from([(0,1), (0,2), (1,1), (1,2)])
nx.write_dot(G,'graph.dot')

Then run

dot -Tpng graph.dot > graph.png

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM