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

 更新时间:2022年05月11日 16:01:30   作者:Cyril_KI  
这篇文章主要为大家介绍了Python利用networkx画图处理绘制Les Misérables悲惨世界里的人物关系图,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

数据集介绍

《悲惨世界》中的人物关系图,图中共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()

以上就是Python利用networkx画图绘制Les Misérables人物关系的详细内容,更多关于Python networkx画图绘制的资料请关注脚本之家其它相关文章!

相关文章

  • Python使用Altair创建交互式数据可视化的操作指南

    Python使用Altair创建交互式数据可视化的操作指南

    Altair 是一个基于 Vega-Lite 的 Python 数据可视化库,它旨在简化数据可视化的创建过程,尤其适用于统计图表的生成,Altair 强调声明式编码方式,通过简单的语法,用户能够快速创建复杂的交互式图表,本文将介绍 Altair 的基础用法、常见图表类型,需要的朋友可以参考下
    2024-12-12
  • Python简单读写Xls格式文档的方法示例

    Python简单读写Xls格式文档的方法示例

    这篇文章主要介绍了Python简单读写Xls格式文档的方法,结合实例形式分析了Python中xlrd和xlwt模块的安装及针对xls格式文档的相关读写操作实现技巧,需要的朋友可以参考下
    2018-08-08
  • 在python3.9下如何安装scrapy的方法

    在python3.9下如何安装scrapy的方法

    这篇文章主要介绍了在python3.9下如何安装scrapy的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • Python模块学习 filecmp 文件比较

    Python模块学习 filecmp 文件比较

    filecmp模块用于比较文件及文件夹的内容,它是一个轻量级的工具,使用非常简单。python标准库还提供了difflib模块用于比较文件的内容。关于difflib模块,且听下回分解
    2012-08-08
  • python format 格式化输出方法

    python format 格式化输出方法

    今天小编就为大家分享一篇python format 格式化输出方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • python 截取XML中bndbox的坐标中的图像,另存为jpg的实例

    python 截取XML中bndbox的坐标中的图像,另存为jpg的实例

    这篇文章主要介绍了python 截取XML中bndbox的坐标中的图像,另存为jpg的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03
  • python人工智能深度学习算法优化

    python人工智能深度学习算法优化

    这篇文章主要为大家介绍了python人工智能深度学习关于算法优化详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2021-11-11
  • Pytorch使用Visdom进行数据可视化的示例代码

    Pytorch使用Visdom进行数据可视化的示例代码

    pytorch Visdom可视化,是一个灵活的工具,用于创建,组织和共享实时丰富数据的可视化,这个博客简要介绍一下在使用Pytorch进行数据可视化的一些内容,感兴趣的朋友可以参考下
    2023-12-12
  • 用Python删除本地目录下某一时间点之前创建的所有文件的实例

    用Python删除本地目录下某一时间点之前创建的所有文件的实例

    下面小编就为大家分享一篇用Python删除本地目录下某一时间点之前创建的所有文件的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2017-12-12
  • python集合删除多种方法详解

    python集合删除多种方法详解

    这篇文章主要介绍了python集合删除多种方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-02-02

最新评论