Skip to content

Instantly share code, notes, and snippets.

@laurencee9
Created July 17, 2018 23:27
Show Gist options
  • Save laurencee9/81dd582d36f96ae958a5c41ab95c24d7 to your computer and use it in GitHub Desktop.
Save laurencee9/81dd582d36f96ae958a5c41ab95c24d7 to your computer and use it in GitHub Desktop.
Plot a network using networkx and clip it into a circle
import networkx as nx
import matplotlib.pyplot as plt
import matplotlib.patches as patches
# Generate network
N = 20
p=0.2
G = nx.erdos_renyi_graph(N,p)
# Figure
fig = plt.figure(figsize=(4,4))
ax = plt.gca()
# Draw network
pos = nx.spring_layout(G)
nx.draw(G, pos, ax=ax, node_size=90, edge_color="gray", node_color="steelblue" )
# Circle
origin = (0,0)
radius = 0.7
patch = patches.Circle(origin,
radius=radius,
transform=ax.transData,
fill=False,
edgecolor="black",
linewidth=2)
ax.add_patch(patch)
for c in ax.collections:
c.set_clip_path(patch)
r = 1.2
plt.xlim([-r,r])
plt.ylim([-r,r])
# Add text
plt.text(0,-radius*1.5,"Plot subtitle",
horizontalalignment='center',
fontsize=15)
@laurencee9
Copy link
Author

example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment