Python怎么利用networkx画图绘制Les Misérables人物关系
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,这篇文章主要介绍"Python怎么利用networkx画图绘制Les Misérables人物关系"的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇"Python怎
千家信息网最后更新 2026年11月01日Python怎么利用networkx画图绘制Les Misérables人物关系
这篇文章主要介绍"Python怎么利用networkx画图绘制Les Misérables人物关系"的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇"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_lesmisFiles: 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 weightUse 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:knuth2993, title = {The {Stanford} {GraphBase}: A Platform for Combinatorial Computing}, author = {Knuth, Donald Ervin}, volume = {37}, year = {1993}, publisher = {Addison-Wesley Reading},}@book{konect:knuth2993, 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 nximport matplotlib.pyplot as pltimport pandas as pd# 77 254df = 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人物关系"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注行业资讯频道,小编每天都会为大家更新不同的知识点。
人物
数据
节点
悲惨
世界
两个
代码
悲惨世界
知识
文件
行业
无向
不同
实用
之间
从中
元素
内容
实用性
实际
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
喔噻互联网科技有限公司
互联网与未来科技
世界互联网领先科技成果
迪茉互联网科技
唐信互联网科技 大连
科技与互联网的利与弊
福州新华互联网科技学校 知乎
央行互联网金融科技公司
互联网科技创新服务
玉溪互联网科技怎么选
朱啸虎曾投资哪些互联网科技企业
互联网科技圈年度人物
科技互联网的核心竞争力
上海京穆互联网科技有限公司
富国互联网金融科技混合基金
卡尔迅互联网科技有限公司
35岁以上互联网科技行业
第三届互联网大会安防黑科技
华人运通互联网科技公司
互联网科技企业文化
广州万易互联网科技有限公司
苏州找大状互联网科技有限公司
互联网科技金融
互联网科技论文
壕鑫互联网络科技
互联网前沿科技
隆华科技工业互联网
福州新华互联网科技学校 知乎
互联网科技包装股份有限公司上市
互联网企业和网络科技有限公司