[Python] Visualize Nodes and Their Connections as Clusters via networkx

Discussão em 'Python' iniciado por Stack, Abril 29, 2025.

  1. Stack

    Stack Membro Participativo

    I have a list of Connections between two nodes describing similarities of Entries in a Dataset.

    I'm thinking of vizualising the Entries and their connections to show that there are clusters of very similar entries.

    Each tuple stands for a pair of very similar nodes. I've chosen weight as 1 for all of them since it's required but I want all edges equally thick.

    I've started with networkx, problem is I don't really now how to cluster the similar nodes together in a useful manner.

    I have a List of the connections in a Dataframe:

    smallSample =
    [[0, 1492, 1],
    [12, 937, 1],
    [16, 989, 1],
    [18, 371, 1],
    [18, 1140, 1],
    [26, 398, 1],
    [26, 1061, 1],
    [30, 1823, 1],
    [33, 1637, 1],
    [54, 1047, 1],
    [63, 565, 1]]


    I Create a graph the following way:

    import networkx as nx
    import matplotlib.pyplot as plt
    G = nx.Graph()
    for index, row in CC.iterrows():
    G.add_edge(CC['source'].loc[index],CC['target'].loc[index], weight =1)
    pos = nx.spring_layout(G, seed=7)
    nx.draw_networkx_nodes(G, pos, node_size=5)
    nx.draw_networkx_edges(G, pos, edgelist=G.edges(), width=0.5)
    pos = nx.spring_layout(G, k=1, iterations=200)
    plt.figure(3, figsize=(2000,2000), dpi =2)


    With the small sample provided above the result looks like this:

    [​IMG]

    The result from my real df which consists of thousands of points:

    [​IMG]

    How can I Group the linked nodes together so that it is better visible how many of them are in each cluster? I dont want them to overlap so hard, its really not that easy to grasp how many of them are there specially in the big sample.

    Continue reading...

Compartilhe esta Página