Created
June 6, 2021 03:46
-
-
Save jinhangjiang/ab531f285d5e155202fe9da82e87a053 to your computer and use it in GitHub Desktop.
NetworkX Draw Graphs
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
def drawgraph(graph, density=False): | |
## insert the graph you just converted from the edge list | |
## the default for the density is False. If density = True, it will return the density of the graph | |
edges,weights = zip(*nx.get_edge_attributes(graph,'weight').items()) ## extract the edges and weights for later use | |
## k will affect the optimal distance between nodes. | |
## more types of layouts can be found at https://networkx.org/documentation/stable/reference/drawing.html | |
pos=nx.spring_layout(graph,k=0.05,seed=42) | |
nx.draw_networkx(graph, | |
pos, | |
with_labels=True, | |
node_size=400, | |
node_color="mistyrose", | |
edgelist=edges, | |
edge_color=weights, | |
edge_cmap=plt.cm.Blues_r, | |
style="solid", | |
width=1) | |
plt.subplots_adjust(left=2, bottom=3.2, right=6, top=6) | |
if density: | |
print("----------------------------------------") | |
print("Density:",nx.classes.function.density(graph)) | |
print("----------------------------------------") | |
return plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment