Python利用networkx画图绘制Les Misérables人物关系
作者:Cyril_KI 发布时间:2021-03-31 07:41:54
标签:python,networkx,画图处理
数据集介绍
《悲惨世界》中的人物关系图,图中共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


猜你喜欢
- 代码案例import pluggy# HookspecMarker 和 HookimplMarker 实质上是一个装饰器带参数的装饰器类,作
- JavaScript是前端开发的主要语言,我们可以通过编写JavaScript程序来判断浏览器的类型及版本。JavaScript判断浏览器类
- 有时你提交过代码之后,发现一个地方改错了,你下次提交时不想保留上一次的记录;或者你上一次的commit message的描述有误,这时候你可
- web框架是什么?web开发框架是一组工具,同时也提供了非常多的资源,供软件开发人员构建和管理网站、提供web服务、编写web应用程序。它是
- 本文实例为大家分享了vue实现小球滑动交叉效果的具体代码,供大家参考,具体内容如下废话不多说 直接上代码!<template>
- bootstrap中有alert组件,如果点击关闭按钮后该组件会被删除而不是被隐藏,想再显示怎么办呢?bootstrap-alert.js源
- 从大规模数据集中寻找物品间的隐含关系被称作关联分析或关联规则学习。过程分为两步:1.提取频繁项集。2.从频繁项集中抽取出关联规则。 频繁项集
- 前言此次的目标是爬取指定城市的天气预报信息,然后再用Python发送邮件到指定的邮箱。下面话不多说了,来一起看看详细的实现过程吧一、爬取天气
- 用div+css制作页面,想实现左右两部分固定宽度,而中间部分不固定,并随着屏幕分辨率的的变化而自动伸缩。大家可知道应该如何实现? &nbs
- 一、绘制折线图使用plot()绘制折线图常用的参数:x:表示x轴的数据y:表示y轴的数据fmt:表示快速设置条样式的格式字符串。label:
- 值得学习的地方:1.选择合法索引的方式2.数组转图像显示import numpy as npfrom PIL import Image#in
- 下面的示例看看这三个函数的具体的区别,其中var_dump和var_export比较少用,但他们两者又很相似。所以可以看看:<?php
- Pandas如何将带有字符串元素的列拆分为多个列。使用以下字符串的方法。str.split():用定界符分割str.extract():按正
- 在js中this的指向对于新手来说一定是个难题,但是如果你真正理解了的话,也就没什么问题啦,下面就来讲讲this吧。JS中,this的值取决
- 关于什么是并发模型,我在这里引用 Go 语言联合创造者 Rob Pike 的一段话:并发是指一次处理多件事。并行是指一次做多件事。二者不同,
- 有一道题: 比较两个列表范围,如果包含的话,返回TRUE,否则FALSE。 详细题目如下:Create a function, this f
- 下面通过实例代码给大家分享Python切片操作去除字符串首尾的空格的方法,具体内容如下所示:#利用切片操作,实现一个trim()函数,去除字
- 前言说到如何用Python执行线性回归,大部分人会立刻想到用sklearn的linear_model,但事实是,Python至少有8种执行线
- 正态分布应用最广泛的连续概率分布,其特征是“钟”形曲线。这种分布的概率密度函数为:其中,μ为均值,σ为标准差。求正态分布曲线下面积有3σ原则
- 在html 5增加了新元素header、footer,测试过发现IE不能解析html 5新增的元素。代码如下:<!DOCTYPE&nb