Skip to content

Instantly share code, notes, and snippets.

@KenoLeon
Last active June 2, 2022 01:22
Show Gist options
  • Save KenoLeon/6eafbc657dd4b8c9c6685c0315e7582c to your computer and use it in GitHub Desktop.
Save KenoLeon/6eafbc657dd4b8c9c6685c0315e7582c to your computer and use it in GitHub Desktop.
simple graph in networkx
import networkx as nx
import matplotlib.pyplot as plt
# Basic Example for networkx
# Init A Graph in networkx
G = nx.Graph()
# Add Single Node:
G.add_node(1)
# Add multiple nodes:
G.add_nodes_from([2, 3, 4, 5])
# Add Edges:
G.add_edge(1, 2)
# Add multiple edges:
# G.add_edges_from([(1, 3), (2, 4)])
# NODES
print("EDGES:", G.edges(), '# EDGES:', G.number_of_edges())
print("NODES:", G.nodes(), '# NODES:', G.number_of_nodes())
# Plot graph
fig = plt.figure(0)
fig.canvas.set_window_title('Networkx Graph')
nx.draw(G, with_labels=True)
plt.show()
# >>
# EDGES: [(1, 2)] # EDGES: 1
# NODES: [1, 2, 3, 4, 5] # NODES: 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment