Last active
February 3, 2025 09:09
-
-
Save jg-you/144a35013acba010054a2cc4a93b07c7 to your computer and use it in GitHub Desktop.
Beautiful networkx graph
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 copy | |
import networkx | |
import matplotlib.pyplot as plt | |
# Generate a graph. | |
# Here I chose an ER graph. | |
g = nx.erdos_renyi_graph(20, 0.3) | |
# Get positions. | |
# Here I use the spectral layout and add a little bit of noise. | |
pos = nx.layout.spectral_layout(g) | |
pos = nx.spring_layout(g, pos=pos, iterations=50) | |
# Create position copies for shadows, and shift shadows | |
pos_shadow = copy.deepcopy(pos) | |
shift_amount = 0.006 | |
for idx in pos_shadow: | |
pos_shadow[idx][0] += shift_amount | |
pos_shadow[idx][1] -= shift_amount | |
#~~~~~~~~~~~~ | |
# Draw graph | |
#~~~~~~~~~~~~ | |
fig = plt.figure(frameon=False) | |
ax = fig.add_axes([0, 0, 1, 1]) | |
ax.axis('off') | |
nx.draw_networkx_nodes(g, pos_shadow, node_color='k', alpha=0.5) | |
nx.draw_networkx_nodes(g, pos, node_color="#3182bd", linewidths=1) | |
nx.draw_networkx_edges(g, pos, width=1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice example!