how to print graph in newtorkx in python

To print a graph in Networkx, you can use one of the several methods provided by the matplotlib library. Below is an example code snippet to print a graph:

main.py
import networkx as nx
import matplotlib.pyplot as plt

# create a graph
G = nx.Graph()
G.add_edge(1, 2)
G.add_edge(2, 3)

# plot the graph
nx.draw(G, with_labels=True, font_weight='bold')
plt.show()
199 chars
12 lines

Here, nx.draw is used to plot the graph G. The with_labels=True option is used to print the node labels. Finally, plt.show() is used to display the graph. You can customize the graph plot with various other options provided by matplotlib.

related categories

gistlibby LogSnag