Created
July 17, 2018 23:27
-
-
Save laurencee9/81dd582d36f96ae958a5c41ab95c24d7 to your computer and use it in GitHub Desktop.
Plot a network using networkx and clip it into a circle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
Author
laurencee9
commented
Jul 17, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment