How to create a node relationship diagram using Python?

To draw a node relationship graph using Python, you can utilize Python’s graphic libraries such as NetworkX and Matplotlib.

Firstly, install the NetworkX and Matplotlib libraries. You can use the following command for installation:

pip install networkx
pip install matplotlib

Next, you can use Python to draw a node relationship graph by following the steps below:

  1. Import the necessary libraries.
import networkx as nx
import matplotlib.pyplot as plt
  1. Create an empty undirected graph.
G = nx.Graph()
  1. Add nodes to the graph.
G.add_node("A")
G.add_node("B")
G.add_node("C")
  1. Add an edge to the graph.
G.add_edge("A", "B")
G.add_edge("B", "C")
G.add_edge("C", "A")
  1. Create a node relationship diagram.
nx.draw(G, with_labels=True)
plt.show()

The complete code is shown below:

import networkx as nx
import matplotlib.pyplot as plt

G = nx.Graph()
G.add_node("A")
G.add_node("B")
G.add_node("C")
G.add_edge("A", "B")
G.add_edge("B", "C")
G.add_edge("C", "A")

nx.draw(G, with_labels=True)
plt.show()

After running the code, a window will pop up displaying the drawn node graph.

bannerAds