如何使用 Python 可视化社交网络 [英] How do I visualize social networks with Python

查看:28
本文介绍了如何使用 Python 可视化社交网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要定义一个社交网络,对其进行分析和绘制.我既可以手工绘制,也可以手工分析(计算各种指标).但我不想重新发明轮子.

I need to define a social network, analyze it and draw it. I could both draw it by hand and analyze it (calculate various metrics) by hand. But I would not like to reinvent the wheel.

我曾尝试使用 matplotlib,但我需要以交互方式使用它,并在几行中告诉它如何加载数据,然后调用渲染函数,该函数会将图形渲染为 SVG.

I have tried to use matplotlib, but I need to use it interactively, and in a few lines tell it how to load the data, and then call a render function, that will render the graph as a SVG.

如何以上述方式可视化社交网络?

How can I visualize social networks in the described way?

推荐答案

networkx 是一个非常强大的和灵活的 Python 库,用于处理网络图.有向和无向连接可用于连接节点.网络可以通过添加节点然后连接它们的边来构建,或者简单地通过列出边对(将自动创建未定义的节点)来构建.创建后,节点(和边)可以使用任意标签进行注释.

networkx is a very powerful and flexible Python library for working with network graphs. Directed and undirected connections can be used to connect nodes. Networks can be constructed by adding nodes and then the edges that connect them, or simply by listing edge pairs (undefined nodes will be automatically created). Once created, nodes (and edges) can be annotated with arbitrary labels.

虽然 networkx 可用于可视化网络(请参阅文档),但您可能更喜欢使用网络可视化应用程序,例如 Gephi(可从 gephi.org).networkx 支持多种导入和导出格式.如果您使用 GraphML 等格式导出网络,导出的文件可以轻松加载到 Gephi 中并在那里可视化.

Although networkx can be used to visualise a network (see the documentation), you may prefer to use a network visualisation application such as Gephi (available from gephi.org). networkx supports a wide range of import and export formats. If you export a network using a format such as GraphML, the exported file can be easily loaded into Gephi and visualised there.

import networkx as nx
G=nx.Graph()
G.add_edges_from([(1,2),(1,3),(1,4),(3,4)])
G
>>> <networkx.classes.graph.Graph object at 0x128a930>
G.nodes(data=True)
>>> [(1, {}), (2, {}), (3, {}), (4, {})]
G.node[1]['attribute']='value'
G.nodes(data=True)
>>> [(1, {'attribute': 'value'}), (2, {}), (3, {}), (4, {})]
nx.write_graphml(G,'so.graphml')

这篇关于如何使用 Python 可视化社交网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆