Python利用networkx画图绘制Les Misérables人物关系

作者:Cyril_KI 时间:2021-03-31 07:41:54 

数据集介绍

《悲惨世界》中的人物关系图,图中共77个节点、254条边。

数据集截图:

Python利用networkx画图绘制Les Misérables人物关系

打开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文件:

Python利用networkx画图绘制Les Misérables人物关系

数据处理

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()

Python利用networkx画图绘制Les Misérables人物关系

networkx自带的数据集

忙活了半天发现networkx有自带的数据集,其中就有悲惨世界的人物关系图:

g = nx.les_miserables_graph()
nx.draw(g, with_labels=True)
plt.show()

Python利用networkx画图绘制Les Misérables人物关系

完整代码

# -*- 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,画图处理
0
投稿

猜你喜欢

  • MySQL中使用binlog时格式该如何选择

    2024-01-19 20:55:30
  • JavaScript给数组添加元素的6个方法

    2024-04-30 08:46:47
  • 如何在Cloud Studio上执行Python代码?

    2023-12-23 02:12:15
  • 用python爬取租房网站信息的代码

    2022-04-30 07:03:38
  • Window10下python3.7 安装与卸载教程图解

    2021-02-15 16:23:54
  • 简介Django框架中可使用的各类缓存

    2022-01-16 13:02:39
  • 用python实现PDF解密打印文件

    2022-01-01 03:53:44
  • JavaScript实现相册弹窗功能(zepto.js)

    2024-04-19 10:42:49
  • 解决python运行启动报错问题

    2022-08-13 06:44:15
  • 最令人蛋疼的10种用户体验设计师

    2011-08-05 18:51:07
  • pycharm中leetcode插件使用图文详解

    2022-09-19 19:19:43
  • Python使用sorted对字典的key或value排序

    2023-12-12 06:36:53
  • PHP chr()函数讲解

    2023-06-05 04:03:58
  • python中复数的共轭复数知识点总结

    2023-09-18 14:11:07
  • 基于python实现高速视频传输程序

    2022-04-01 02:14:17
  • MySQL EXPLAIN执行计划解析

    2024-01-26 14:35:28
  • python儿童学游戏编程知识点总结

    2022-10-23 04:32:42
  • 玩转Python图像处理之二值图像腐蚀详解

    2022-09-01 16:23:24
  • msxml3.dll (0x80070005)拒绝访问 解决方法

    2010-03-11 21:26:00
  • MySQL中隐藏空间问题浅析

    2009-11-24 09:04:00
  • asp之家 网络编程 m.aspxhome.com