欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python利用networkx畫圖繪制Les?Misérables人物關系

 更新時間:2022年05月11日 16:01:30   作者:Cyril_KI  
這篇文章主要為大家介紹了Python利用networkx畫圖處理繪制Les?Misérables悲慘世界里的人物關系圖,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

數據集介紹

《悲慘世界》中的人物關系圖,圖中共77個節(jié)點、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},
}

從中可以得知:該圖是一個無向圖,節(jié)點表示《悲慘世界》中的人物,兩個節(jié)點之間的邊表示這兩個人物出現在書的同一章,邊的權重表示兩個人物(節(jié)點)出現在同一章中的頻率。

真正的數據在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é)點的初始化很容易解決,我們主要解決邊的初始化:先將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自帶的數據集

忙活了半天發(fā)現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實現粒子群算法的示例

    這篇文章主要介紹了Python實現粒子群算法的示例,幫助大家更好的理解和使用Python,感興趣的朋友可以了解下
    2021-02-02
  • python使用Berkeley DB數據庫實例

    python使用Berkeley DB數據庫實例

    這篇文章主要介紹了python使用Berkeley DB數據庫的方法,以實例形式講述了完整的操作過程,并總結了具體的操作步驟,非常具有實用性,需要的朋友可以參考下
    2014-09-09
  • python打包成so文件過程解析

    python打包成so文件過程解析

    這篇文章主要介紹了python打包成so文件過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-09-09
  • Python繪制詞云圖之可視化神器pyecharts

    Python繪制詞云圖之可視化神器pyecharts

    這篇文章主要介紹了Python繪制詞云圖之可視化神器pyecharts,文章圍繞主題展開詳細的相關內容,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-07-07
  • pandas組內排序,并在每個分組內按序打上序號的操作

    pandas組內排序,并在每個分組內按序打上序號的操作

    這篇文章主要介紹了pandas組內排序,并在每個分組內按序打上序號的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • Python 使用元類type創(chuàng)建類對象常見應用詳解

    Python 使用元類type創(chuàng)建類對象常見應用詳解

    這篇文章主要介紹了Python 使用元類type創(chuàng)建類對象,結合實例形式詳細分析了Python元類的概念、功能及元類type創(chuàng)建類對象的常見應用技巧,需要的朋友可以參考下
    2019-10-10
  • python數據處理 根據顏色對圖片進行分類的方法

    python數據處理 根據顏色對圖片進行分類的方法

    今天小編就為大家分享一篇python數據處理 根據顏色對圖片進行分類的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • python 列表遞歸求和、計數、求最大元素的實例

    python 列表遞歸求和、計數、求最大元素的實例

    今天小編就為大家分享一篇python 列表遞歸求和、計數、求最大元素的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • python中子類繼承父類的__init__方法實例

    python中子類繼承父類的__init__方法實例

    這篇文章主要給大家詳細介紹了python中子類如何繼承父類的__init__方法,文中給出了詳細的示例代碼,相信對大家的理解和學習具有一定參考價值,有需要的朋友們下面來跟著小編一起學習學習吧。
    2016-12-12
  • Python中線程threading.Thread的使用詳解

    Python中線程threading.Thread的使用詳解

    python的thread模塊是比較底層的模塊,python的threading模塊是對thread做了一些包裝的,可以更加方便的被使用。本文將為大家詳細介紹一下python中的線程threading.Thread()的使用,需要的可以參考一下
    2022-07-07

最新評論