plot_selfloops.py 753 B

1234567891011121314151617181920212223242526272829
  1. """
  2. ==========
  3. Self-loops
  4. ==========
  5. A self-loop is an edge that originates from and terminates the same node.
  6. This example shows how to draw self-loops with `nx_pylab`.
  7. """
  8. import networkx as nx
  9. import matplotlib.pyplot as plt
  10. # Create a graph and add a self-loop to node 0
  11. G = nx.complete_graph(3, create_using=nx.DiGraph)
  12. G.add_edge(0, 0)
  13. pos = nx.circular_layout(G)
  14. # As of version 2.6, self-loops are drawn by default with the same styling as
  15. # other edges
  16. nx.draw(G, pos, with_labels=True)
  17. # Add self-loops to the remaining nodes
  18. edgelist = [(1, 1), (2, 2)]
  19. G.add_edges_from(edgelist)
  20. # Draw the newly added self-loops with different formatting
  21. nx.draw_networkx_edges(G, pos, edgelist=edgelist, arrowstyle="<|-", style="dashed")
  22. plt.show()