diff options
author | Ross Barnowski <rossbar@berkeley.edu> | 2020-12-07 12:13:57 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-07 12:13:57 -0800 |
commit | 6108e42fa70fec2d1af0f177bf5518d6b60981d6 (patch) | |
tree | b40b362f97c65319b2e153dc8497bab67e137f0c /examples | |
parent | 87b92dea2429590c78108d7407f9329cd999a564 (diff) | |
download | networkx-6108e42fa70fec2d1af0f177bf5518d6b60981d6.tar.gz |
Add gallery example for drawing self-loops. (#4430)
Diffstat (limited to 'examples')
-rw-r--r-- | examples/drawing/plot_selfloops.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/examples/drawing/plot_selfloops.py b/examples/drawing/plot_selfloops.py new file mode 100644 index 00000000..11bcf50f --- /dev/null +++ b/examples/drawing/plot_selfloops.py @@ -0,0 +1,29 @@ +""" +========== +Self-loops +========== + +A self-loop is an edge that originates from and terminates the same node. +This example shows how to draw self-loops with `nx_pylab`. + +""" +import networkx as nx +import matplotlib.pyplot as plt + +# Create a graph and add a self-loop to node 0 +G = nx.complete_graph(3, create_using=nx.DiGraph) +G.add_edge(0, 0) +pos = nx.circular_layout(G) + +# As of version 2.6, self-loops are drawn by default with the same styling as +# other edges +nx.draw(G, pos, with_labels=True) + +# Add self-loops to the remaining nodes +edgelist = [(1, 1), (2, 2)] +G.add_edges_from(edgelist) + +# Draw the newly added self-loops with different formatting +nx.draw_networkx_edges(G, pos, edgelist=edgelist, arrowstyle="<|-", style="dashed") + +plt.show() |