您好,登錄后才能下訂單哦!
這篇文章主要介紹“Python怎么利用networkx畫圖繪制Les Misérables人物關(guān)系”的相關(guān)知識,小編通過實(shí)際案例向大家展示操作過程,操作方法簡單快捷,實(shí)用性強(qiáng),希望這篇“Python怎么利用networkx畫圖繪制Les Misérables人物關(guān)系”文章能幫助大家解決問題。
《悲慘世界》中的人物關(guān)系圖,圖中共77個節(jié)點(diǎn)、254條邊。
數(shù)據(jù)集截圖:
打開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: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}, }
從中可以得知:該圖是一個無向圖,節(jié)點(diǎn)表示《悲慘世界》中的人物,兩個節(jié)點(diǎn)之間的邊表示這兩個人物出現(xiàn)在書的同一章,邊的權(quán)重表示兩個人物(節(jié)點(diǎn))出現(xiàn)在同一章中的頻率。
真正的數(shù)據(jù)在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})])
節(jié)點(diǎn)的初始化很容易解決,我們主要解決邊的初始化:先將dataframe轉(zhuǎn)為列表,然后將其中每個元素轉(zhuǎn)為元組。
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()
忙活了半天發(fā)現(xiàn)networkx有自帶的數(shù)據(jù)集,其中就有悲慘世界的人物關(guān)系圖:
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()
關(guān)于“Python怎么利用networkx畫圖繪制Les Misérables人物關(guān)系”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點(diǎn)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。