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

查看:355
本文介绍了如何使用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,但是我需要交互使用它,并在几行中告诉它如何加载数据,然后调用render函数,该函数会将图形呈现为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天全站免登陆