Python怎么利用networkx画图绘制Les Misérables人物关系(networkx,python,开发技术)

时间:2024-05-07 00:24:03 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

数据集介绍

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

数据集截图:

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

打开README文件:

LesMisérablesnetwork,partoftheKoblenzNetworkCollection===========================================================================ThisdirectorycontainstheTSVandrelatedfilesofthemoreno_lesmisnetwork:Thisundirectednetworkcontainsco-occurancesofcharactersinVictorHugo'snovel'LesMisérables'.Anoderepresentsacharacterandanedgebetweentwonodesshowsthatthesetwocharactersappearedinthesamechapterofthethebook.Theweightofeachlinkindicateshowoftensuchaco-appearanceoccured.Moreinformationaboutthenetworkisprovidedhere:http://konect.cc/networks/moreno_lesmisFiles:meta.moreno_lesmis--Metadataaboutthenetworkout.moreno_lesmis--Theadjacencymatrixofthenetworkinwhitespace-separatedvaluesformat,withoneedgeperlineThemeaningofthecolumnsinout.moreno_lesmisare:Firstcolumn:IDoffromnodeSecondcolumn:IDoftonodeThirdcolumn(ifpresent):weightormultiplicityofedgeFourthcolumn(ifpresent):timestampofedgesUnixtimeThirdcolumn:edgeweightUsethefollowingReferencesforcitation:@MISC{konect:2017:moreno_lesmis,title={LesMisérablesnetworkdataset--{KONECT}},month=oct,year={2017},url={http://konect.cc/networks/moreno_lesmis}}@book{konect:knuth2993, title={The{Stanford}{GraphBase}:APlatformforCombinatorialComputing}, author={Knuth,DonaldErvin}, volume={37}, year={1993}, publisher={Addison-WesleyReading},}@book{konect:knuth2993, title={The{Stanford}{GraphBase}:APlatformforCombinatorialComputing}, author={Knuth,DonaldErvin}, volume={37}, year={1993}, publisher={Addison-WesleyReading},}@inproceedings{konect, title={{KONECT}--{The}{Koblenz}{Network}{Collection}}, author={JérômeKunegis}, year={2013}, booktitle={Proc.Int.Conf.onWorldWideWebCompanion}, 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ômeKunegis}, year={2013}, booktitle={Proc.Int.Conf.onWorldWideWebCompanion}, 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([iforiinrange(1,78)])g.add_edges_from([(1,2,{'weight':1})])

节点的初始化很容易解决,我们主要解决边的初始化:先将dataframe转为列表,然后将其中每个元素转为元组。

df=pd.read_csv('out.csv')res=df.values.tolist()foriinrange(len(res)):res[i][2]=dict({'weight':res[i][2]})res=[tuple(x)forxinres]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([iforiinrange(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-*-importnetworkxasnximportmatplotlib.pyplotaspltimportpandasaspd#77254df=pd.read_csv('out.csv')res=df.values.tolist()foriinrange(len(res)):res[i][2]=dict({'weight':res[i][2]})res=[tuple(x)forxinres]print(res)#初始化图g=nx.Graph()g.add_nodes_from([iforiinrange(1,78)])g.add_edges_from(res)g=nx.les_miserables_graph()nx.draw(g,with_labels=True)plt.show()
 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Python怎么利用networkx画图绘制Les Misérables人物关系的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:C语言如何实现控制台打砖块小游戏下一篇:

10 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18