python - 如何清楚地绘制大型网络

标签 python matplotlib networkx

我有一个 cora 引文网络数据,它有 2708 个节点和 5429 个边。 我尝试使用 networkx 绘制它

import scipy.sparse as sp
import numpy as np
import matplotlib.pyplot as plt

import networkx as nx

def encode_onehot(labels):
    classes = set(labels)
    classes_dict = {c: np.identity(len(classes))[i, :] for i, c in enumerate(classes)}
    labels_onehot = np.array(list(map(classes_dict.get, labels)), dtype=np.int32)
    return labels_onehot

def load_data(path="data/cora/", dataset="cora"):
    print('Loading {} dataset...'.format(dataset))

    idx_features_labels = np.genfromtxt("{}{}.content".format(path, dataset), dtype=np.dtype(str))
    features = sp.csr_matrix(idx_features_labels[:, 1:-1], dtype=np.float32)
    labels = encode_onehot(idx_features_labels[:, -1])

    # build graph
    idx = np.array(idx_features_labels[:, 0], dtype=np.int32)
    idx_map = {j: i for i, j in enumerate(idx)}
    edges_unordered = np.genfromtxt("{}{}.cites".format(path, dataset), dtype=np.int32)
    edges = np.array(list(map(idx_map.get, edges_unordered.flatten())),
                     dtype=np.int32).reshape(edges_unordered.shape)
    adj = sp.coo_matrix((np.ones(edges.shape[0]), (edges[:, 0], edges[:, 1])),
                        shape=(labels.shape[0], labels.shape[0]), dtype=np.float32)

    # build symmetric adjacency matrix
    adj = adj + adj.T.multiply(adj.T > adj) - adj.multiply(adj.T > adj)

    print('Dataset has {} nodes, {} edges, {} features.'.format(adj.shape[0], edges.shape[0], features.shape[1]))

    return features.todense(), adj, labels


X, A, y = load_data(dataset='cora')

G = nx.from_scipy_sparse_matrix(A)

nx.draw_networkx(G)
plt.show()

coranetworkplot

如您所见,这很难看到。有什么方法可以让它更容易看到吗? 任何建议都会被应用。

最佳答案

您可以使用 pyvis.network 进行交互式可视化。您还可以将图形导出到外部文件或在笔记本中内联绘制网络。

我通常使用此函数为自己绘制图表。

def draw_graph3(networkx_graph,notebook=True,output_filename='graph.html',show_buttons=False,only_physics_buttons=False):
        """
        This function accepts a networkx graph object,
        converts it to a pyvis network object preserving its node and edge attributes,
        and both returns and saves a dynamic network visualization.

        Valid node attributes include:
            "size", "value", "title", "x", "y", "label", "color".

            (For more info: https://pyvis.readthedocs.io/en/latest/documentation.html#pyvis.network.Network.add_node)

        Valid edge attributes include:
            "arrowStrikethrough", "hidden", "physics", "title", "value", "width"

            (For more info: https://pyvis.readthedocs.io/en/latest/documentation.html#pyvis.network.Network.add_edge)


        Args:
            networkx_graph: The graph to convert and display
            notebook: Display in Jupyter?
            output_filename: Where to save the converted network
            show_buttons: Show buttons in saved version of network?
            only_physics_buttons: Show only buttons controlling physics of network?
        """

        # import
        from pyvis import network as net

        # make a pyvis network
        pyvis_graph = net.Network(notebook=notebook)
        pyvis_graph.width = '1000px'
        # for each node and its attributes in the networkx graph
        for node,node_attrs in networkx_graph.nodes(data=True):
            pyvis_graph.add_node(node,**node_attrs)
    #         print(node,node_attrs)

        # for each edge and its attributes in the networkx graph
        for source,target,edge_attrs in networkx_graph.edges(data=True):
            # if value/width not specified directly, and weight is specified, set 'value' to 'weight'
            if not 'value' in edge_attrs and not 'width' in edge_attrs and 'weight' in edge_attrs:
                # place at key 'value' the weight of the edge
                edge_attrs['value']=edge_attrs['weight']
            # add the edge
            pyvis_graph.add_edge(source,target,**edge_attrs)

        # turn buttons on
        if show_buttons:
            if only_physics_buttons:
                pyvis_graph.show_buttons(filter_=['physics'])
            else:
                pyvis_graph.show_buttons()

        # return and also save
        return pyvis_graph.show(output_filename)

你可以通过调用这个函数来可视化图形

draw_graph3(your_graph,output_filename='graph_output.html', notebook=False)

关于python - 如何清楚地绘制大型网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59598019/

相关文章:

python - pickle 错误: AttributeError: Can't get attribute on <module '__main__' (built-in)>

python - 你如何在Python中使用IXR_Base64?

python - 使用 node2vec 创建嵌入

python - 在 Jupyter Windows 上使用池并行读取多个文件需要很长时间 :

python - django 中的 html css 示例

python - 在 matplotlib 中自定义 x 轴

python - 无法控制 df.plot() 上第二个 y 轴的比例

python - 使用热图将线图与双轴图上的 xticks 对齐

networkx - 彩色图同构?

python - 将具有属性和边的节点从 DataFrame 加载到 NetworkX