Created
April 15, 2023 05:41
-
-
Save nktnlx/0429377a248cdc371ad8d2a0c12c315c to your computer and use it in GitHub Desktop.
graph representation with networkx code snippet
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 | |
# Create an empty graph | |
G = nx.Graph() | |
# Add edges to the graph | |
G.add_edge(1, 2) # edge between node 1 and node 2 | |
G.add_edge(1, 3) # edge between node 1 and node 3 | |
G.add_edge(2, 4) # edge between node 2 and node 4 | |
G.add_edge(3, 5) # edge between node 3 and node 5 | |
G.add_edge(4, 6) # edge between node 4 and node 6 | |
G.add_edge(4, 7) # edge between node 4 and node 7 | |
G.add_edge(4, 8) # edge between node 4 and node 8 | |
G.add_edge(4, 9) # edge between node 4 and node 9 | |
G.add_edge(5, 1) # edge between node 5 and node 1 | |
# Set the background color of the plot window to black | |
plt.gcf().set_facecolor('black') | |
# Set the positions of the nodes in the graph using spring layout algorithm | |
pos = nx.spring_layout(G) | |
# Draw nodes with green color | |
nx.draw_networkx_nodes(G, pos, node_color='green') | |
# Draw edges with green color | |
nx.draw_networkx_edges(G, pos, edge_color='green') | |
# Add labels to nodes with black font color | |
nx.draw_networkx_labels(G, pos, font_color='black') | |
# Turn off axis lines and labels | |
plt.axis('off') | |
# Set the size of the plot window to (6 inches x 2 inches) | |
plt.figure(figsize=(6,2)) | |
# Display the plot window | |
plt.show(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment