数据集介绍
《悲惨世界》中的人物关系图,图中共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教程分享Python利用networkx画图绘制Les Misérables人物关系,都可以关注python教程分享栏目—猴子技术宅(www.ssfiction.com)
本文来自网络收集,不代表猴子技术宅立场,如涉及侵权请点击右边联系管理员删除。
如若转载,请注明出处:https://www.ssfiction.com/pythons/1092007.html