Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

jg-you commented Jan 31, 2017

cibjtleweaew_q9

@Ewen2015

This comment has been minimized.

Copy link

Ewen2015 commented Nov 29, 2018

This is awesome!

@jg-you

This comment has been minimized.

Copy link
Owner 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

This comment has been minimized.

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
You can’t perform that action at this time.