Skip to content

Instantly share code, notes, and snippets.

@jg-you
Last active October 30, 2022 00:04
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jg-you/144a35013acba010054a2cc4a93b07c7 to your computer and use it in GitHub Desktop.
Save jg-you/144a35013acba010054a2cc4a93b07c7 to your computer and use it in GitHub Desktop.
Beautiful networkx graph
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)
@jg-you
Copy link
Author

jg-you commented Jan 31, 2017

cibjtleweaew_q9

@Ewen2015
Copy link

This is awesome!

@jg-you
Copy link
Author

jg-you commented Jul 9, 2019

Note: This emulates the graphing style of The Nature Of Computation by Cristopher Moore and Stephan Mertens.

@Liyubov
Copy link

Liyubov commented Oct 6, 2019

Nice example!

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