Are you looking for visually-stunning tools to represent networks of knowledge, communication, social interactions, data organization, etc in your GUI App?
This post will get you to understand how to use the NetworkX Python Library using Python4Delphi (P4D) in the Delphi/C++ Builder application and perform some interesting Network Visualization tasks.
NetworkX is a Python package for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks.
At a glance, NetworkX provides us with:
- Data structures for graphs, digraphs, and multigraphs.
- Many standard graph algorithms.
- Network structure and analysis measures.
- Generators for classic graphs, random graphs, and synthetic networks.
- Nodes can be “anything” (e.g., text, images, XML records).
- Edges can hold arbitrary data (e.g., weights, time-series).
- Open source 3-clause BSD license.
- Well tested with over 90% code coverage.
- Additional benefits from Python include fast prototyping, easy to teach, and multi-platform
Demo: Draw Random Geometric Graph
This post will guide you on how to draw a Visually-Stunning Geometric Graph using Python’s NetworkX and then display it in the Delphi Windows GUI app.
First, open and run our Python GUI using project Demo1 from Python4Delphi with RAD Studio. Then insert the script into the lower Memo, click the Execute button, and get the result in the upper Memo. You can find the Demo1 source on GitHub. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this link.
Let’s perform a Random Geometric Graph demo of the NetworkX library. The following code is credited to NetworkX Developers (visit the source here):
import matplotlib.pyplot as plt
import networkx as nx
G = nx.random_geometric_graph(200, 0.125)
# position is stored as node attribute data for random_geometric_graph
pos = nx.get_node_attributes(G, "pos")
# find node near center (0.5,0.5)
dmin = 1
ncenter = 0
for n in pos:
x, y = pos[n]
d = (x - 0.5) ** 2 + (y - 0.5) ** 2
if d < dmin:
ncenter = n
dmin = d
# color by path length from node near center
p = dict(nx.single_source_shortest_path_length(G, ncenter))
nx.draw_networkx_edges(G, pos, nodelist=[ncenter], alpha=0.4)
Let’s see the result in Python GUI:
Congratulations, now you have learned how to draw a Visually-Stunning Geometric Graph using Python’s NetworkX and then display it in the Delphi Windows GUI app.
Check out the NetworkX library for Python and use it in your projects: https://pypi.org/project/networkx/ and
Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi: https://github.com/pyscripter/python4delphi