Python利用networkx画图绘制Les Misérables人物关系
作者:Cyril_KI 时间:2021-03-31 07:41:54
数据集介绍
《悲惨世界》中的人物关系图,图中共77个节点、254条边。
数据集截图:
打开README文件:
Les Misérables network, part of the Koblenz Network Collection
===========================================================================
This directory contains the TSV and related files of the moreno_lesmis network: This undirected network contains co-occurances of characters in Victor Hugo's novel 'Les Misérables'. A node represents a character and an edge between two nodes shows that these two characters appeared in the same chapter of the the book. The weight of each link indicates how often such a co-appearance occured.
More information about the network is provided here:
http://konect.cc/networks/moreno_lesmis
Files:
meta.moreno_lesmis -- Metadata about the network
out.moreno_lesmis -- The adjacency matrix of the network in whitespace-separated values format, with one edge per line
The meaning of the columns in out.moreno_lesmis are:
First column: ID of from node
Second column: ID of to node
Third column (if present): weight or multiplicity of edge
Fourth column (if present): timestamp of edges Unix time
Third column: edge weight
Use the following References for citation:
@MISC{konect:2017:moreno_lesmis,
title = {Les Misérables network dataset -- {KONECT}},
month = oct,
year = {2017},
url = {http://konect.cc/networks/moreno_lesmis}
}
@book{konect:knuth1993,
title = {The {Stanford} {GraphBase}: A Platform for Combinatorial Computing},
author = {Knuth, Donald Ervin},
volume = {37},
year = {1993},
publisher = {Addison-Wesley Reading},
}
@book{konect:knuth1993,
title = {The {Stanford} {GraphBase}: A Platform for Combinatorial Computing},
author = {Knuth, Donald Ervin},
volume = {37},
year = {1993},
publisher = {Addison-Wesley Reading},
}
@inproceedings{konect,
title = {{KONECT} -- {The} {Koblenz} {Network} {Collection}},
author = {Jérôme Kunegis},
year = {2013},
booktitle = {Proc. Int. Conf. on World Wide Web Companion},
pages = {1343--1350},
url = {http://dl.acm.org/citation.cfm?id=2488173},
url_presentation = {https://www.slideshare.net/kunegis/presentationwow},
url_web = {http://konect.cc/},
url_citations = {https://scholar.google.com/scholar?cites=7174338004474749050},
}
@inproceedings{konect,
title = {{KONECT} -- {The} {Koblenz} {Network} {Collection}},
author = {Jérôme Kunegis},
year = {2013},
booktitle = {Proc. Int. Conf. on World Wide Web Companion},
pages = {1343--1350},
url = {http://dl.acm.org/citation.cfm?id=2488173},
url_presentation = {https://www.slideshare.net/kunegis/presentationwow},
url_web = {http://konect.cc/},
url_citations = {https://scholar.google.com/scholar?cites=7174338004474749050},
}
从中可以得知:该图是一个无向图,节点表示《悲惨世界》中的人物,两个节点之间的边表示这两个人物出现在书的同一章,边的权重表示两个人物(节点)出现在同一章中的频率。
真正的数据在out.moreno_lesmis_lesmis中,打开并另存为csv文件:
数据处理
networkx中对无向图的初始化代码为:
g = nx.Graph()
g.add_nodes_from([i for i in range(1, 78)])
g.add_edges_from([(1, 2, {'weight': 1})])
节点的初始化很容易解决,我们主要解决边的初始化:先将dataframe转为列表,然后将其中每个元素转为元组。
df = pd.read_csv('out.csv')
res = df.values.tolist()
for i in range(len(res)):
res[i][2] = dict({'weight': res[i][2]})
res = [tuple(x) for x in res]
print(res)
res输出如下(部分):
[(1, 2, {'weight': 1}), (2, 3, {'weight': 8}), (2, 4, {'weight': 10}), (2, 5, {'weight': 1}), (2, 6, {'weight': 1}), (2, 7, {'weight': 1}), (2, 8, {'weight': 1})...]
因此图的初始化代码为:
g = nx.Graph()
g.add_nodes_from([i for i in range(1, 78)])
g.add_edges_from(res)
画图
nx.draw(g)
plt.show()
networkx自带的数据集
忙活了半天发现networkx有自带的数据集,其中就有悲惨世界的人物关系图:
g = nx.les_miserables_graph()
nx.draw(g, with_labels=True)
plt.show()
完整代码
# -*- coding: utf-8 -*-
import networkx as nx
import matplotlib.pyplot as plt
import pandas as pd
# 77 254
df = pd.read_csv('out.csv')
res = df.values.tolist()
for i in range(len(res)):
res[i][2] = dict({'weight': res[i][2]})
res = [tuple(x) for x in res]
print(res)
# 初始化图
g = nx.Graph()
g.add_nodes_from([i for i in range(1, 78)])
g.add_edges_from(res)
g = nx.les_miserables_graph()
nx.draw(g, with_labels=True)
plt.show()
来源:https://blog.csdn.net/Cyril_KI/article/details/121970723
标签:python,networkx,画图处理
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
一些关于asp 购物车的想法
2011-04-10 11:10:00
Python字节码与程序执行过程详解
2022-01-25 04:45:24
![](https://img.aspxhome.com/file/2023/0/64600_0s.png)
PHP开发实现微信退款功能示例
2023-06-30 09:10:25
SQL Server数据库管理常用SQL和T-SQL语句
2009-05-07 14:01:00
悟道WEB标准:统一思想,遵循标准
2009-10-11 16:38:00
一个Access数据库数据传递的实例方法
2008-11-28 16:24:00
用VB编写ActiveX DLL实现ASP编程
2008-10-21 21:28:00
Oracle 子程序参数模式,IN,OUT,NOCOPY
2009-10-23 18:08:00
解析arp病毒背后利用的Javascript技术
2007-08-08 09:55:00
无刷新dropdownlist并进行关联(js+xml)
2007-09-23 12:13:00
浅谈python迭代器
2023-07-21 21:56:47
好用的JS图片预加载类
2007-08-13 13:49:00
PHP使用laravel邮件服务发送邮件
2023-05-25 03:48:23
HTML5本地存储初探(二)
2010-03-07 15:47:00
![](https://img.aspxhome.com/file/UploadPic/20103/7/20100131-rmki9pjq85xy9nbt436gkdfb7i-94s.png)
15个梦幻的登录页面设计展示
2009-07-19 14:17:00
![](https://img.aspxhome.com/file/UploadPic/20097/19/battlenet-37s.jpg)
用asp程序读取网站的alexa世界排名
2008-11-23 20:43:00
解决iframe的frameborder在chrome/ff/ie下的差异
2023-08-09 00:23:24
不是原型继承那么简单!prototype的深度探索
2008-03-07 12:42:00
为你总结一些php信息函数
2023-10-28 09:46:59
关于ASP代码的加密
2007-10-15 12:30:00