python 中的图形上有很多边

2024-03-15

我有以下脚本:

import pandas as pd
from igraph import *

df_p_c = pd.read_csv('data/edges.csv')

...

edges = list_edges
vertices = list(dict_case_to_number.keys())

g = Graph(edges=edges, directed=True)

plot(g, bbox=(6000, 6000))

I have 2300 edges with rare connection. This is my plot of it: all area And here are zooms of a few parts of it:

该图不可读,因为边缘之间的距离太小。如何使边缘之间的距离更大?只有来自同一“族”的边距离较小。

还有其他方法可以改善具有大量边缘的绘图吗? 我正在寻找任何可视化父子相关性的方法,它可能是另一个 python 数据包。


你似乎有很多小的、互不相连的组件。如果您想要一个信息丰富的图表,我认为您应该按大小对连接的组件进行排序和分组。此外,许多网络布局算法的基本假设是存在一个巨大的组件。因此,如果您想要合理的坐标,您通常需要单独计算每个组件的布局,然后相对于彼此排列组件。我会以这种方式重新绘制你的图表:

我已经使用该图编写了代码networkx因为这是我选择的模块。然而,用它来代替是很容易的networkx函数与igraph功能。您需要替换的两个函数是networkx.connected_component_subgraphs以及你想用的任何东西component_layout_func.

#!/usr/bin/env python

import numpy as np
import matplotlib.pyplot as plt
import networkx


def layout_many_components(graph,
                           component_layout_func=networkx.layout.spring_layout,
                           pad_x=1., pad_y=1.):
    """
    Arguments:
    ----------
    graph: networkx.Graph object
        The graph to plot.

    component_layout_func: function (default networkx.layout.spring_layout)
        Function used to layout individual components.
        You can parameterize the layout function by partially evaluating the
        function first. For example:

        from functools import partial
        my_layout_func = partial(networkx.layout.spring_layout, k=10.)
        pos = layout_many_components(graph, my_layout_func)

    pad_x, pad_y: float
        Padding between subgraphs in the x and y dimension.

    Returns:
    --------
    pos : dict node : (float x, float y)
        The layout of the graph.

    """

    components = _get_components_sorted_by_size(graph)
    component_sizes = [len(component) for component in components]
    bboxes = _get_component_bboxes(component_sizes, pad_x, pad_y)

    pos = dict()
    for component, bbox in zip(components, bboxes):
        component_pos = _layout_component(component, bbox, component_layout_func)
        pos.update(component_pos)

    return pos

    
def _get_components_sorted_by_size(g):
    subgraphs = list(networkx.connected_component_subgraphs(g))
    return sorted(subgraphs, key=len)


def _get_component_bboxes(component_sizes, pad_x=1., pad_y=1.):
    bboxes = []
    x, y = (0, 0)
    current_n = 1
    for n in component_sizes:
        width, height = _get_bbox_dimensions(n, power=0.8)

        if not n == current_n: # create a "new line"
            x = 0 # reset x
            y += height + pad_y # shift y up
            current_n = n

        bbox = x, y, width, height
        bboxes.append(bbox)
        x += width + pad_x # shift x down the line
    return bboxes


def _get_bbox_dimensions(n, power=0.5):
    # return (np.sqrt(n), np.sqrt(n))
    return (n**power, n**power)


def _layout_component(component, bbox, component_layout_func):
    pos = component_layout_func(component)
    rescaled_pos = _rescale_layout(pos, bbox)
    return rescaled_pos


def _rescale_layout(pos, bbox):

    min_x, min_y = np.min([v for v in pos.values()], axis=0)
    max_x, max_y = np.max([v for v in pos.values()], axis=0)

    if not min_x == max_x:
        delta_x = max_x - min_x
    else: # graph probably only has a single node
        delta_x = 1.

    if not min_y == max_y:
        delta_y = max_y - min_y
    else: # graph probably only has a single node
        delta_y = 1.

    new_min_x, new_min_y, new_delta_x, new_delta_y = bbox

    new_pos = dict()
    for node, (x, y) in pos.items():
        new_x = (x - min_x) / delta_x * new_delta_x + new_min_x
        new_y = (y - min_y) / delta_y * new_delta_y + new_min_y
        new_pos[node] = (new_x, new_y)

    return new_pos


def test():
    from itertools import combinations

    g = networkx.Graph()

    # add 100 unconnected nodes
    g.add_nodes_from(range(100))

    # add 50 2-node components
    g.add_edges_from([(ii, ii+1) for ii in range(100, 200, 2)])

    # add 33 3-node components
    for ii in range(200, 300, 3):
        g.add_edges_from([(ii, ii+1), (ii, ii+2), (ii+1, ii+2)])

    # add a couple of larger components
    n = 300
    for ii in np.random.randint(4, 30, size=10):
        g.add_edges_from(combinations(range(n, n+ii), 2))
        n += ii

    pos = layout_many_components(g, component_layout_func=networkx.layout.circular_layout)

    networkx.draw(g, pos, node_size=100)

    plt.show()


if __name__ == '__main__':

    test()

EDIT

如果你想让子图紧密排列,你需要安装矩形打包器(pip install rectangle-packer),并代入_get_component_bboxes有了这个版本:

import rpack 

def _get_component_bboxes(component_sizes, pad_x=1., pad_y=1.):
    dimensions = [_get_bbox_dimensions(n, power=0.8) for n in component_sizes]
    # rpack only works on integers; sizes should be in descending order
    dimensions = [(int(width + pad_x), int(height + pad_y)) for (width, height) in dimensions[::-1]]
    origins = rpack.pack(dimensions)
    bboxes = [(x, y, width-pad_x, height-pad_y) for (x,y), (width, height) in zip(origins, dimensions)]
    return bboxes[::-1]

Edit #2

我写了一个用于可视化网络的库,它被称为netgraph https://github.com/paulbrodersen/netgraph。它以上述方式自动处理具有多个组件的网络。它与 networkx 和 igraph Graph 对象完全兼容,因此应该可以轻松快速地制作漂亮的图的图(至少是这样的想法)。

import itertools
import matplotlib.pyplot as plt
import networkx as nx

# installation easiest via pip:
# pip install netgraph
from netgraph import Graph

# construct the graph as before:
g = nx.Graph()

# add 30 unconnected nodes
g.add_nodes_from(range(30))

# add 15 2-node components
g.add_edges_from([(ii, ii+1) for ii in range(30, 60, 2)])

# add 10 3-node components
for ii in range(60, 90, 3):
    g.add_edges_from([(ii, ii+1), (ii, ii+2), (ii+1, ii+2)])

# add a couple of larger components
n = 90
for ii in [10, 20, 40]:
    g.add_edges_from(itertools.combinations(range(n, n+ii), 2))
    n += ii

# if there are any disconnected components, netgraph automatically handles them separately
Graph(g, node_layout='circular', node_size=1, node_edge_width=0.1, edge_width=0.1, edge_color='black', edge_alpha=1.)

plt.show()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python 中的图形上有很多边 的相关文章

随机推荐