天天看點

桑基圖繪制

桑基圖還是很簡單的,直接調用pyecharts就行,套用我的代碼即可實作。

注意:1.節點名稱唯一。

2.關系用的節點必須是已經建立的。

首先上效果圖:

桑基圖繪制

代碼如下:

#coding='utf-8'
from pyecharts import options as opts
from pyecharts.charts import Sankey
#問題是節點名稱不能一樣
#所有關鍵詞和主題詞是節點,然後在同一年份的目标指向是主題詞;主題詞間又根據時間進行指向
#更新:四個關鍵詞為一行,一個主題詞為一行,間隔一行為年
nodes=[   # 所有節點名稱
    #關鍵詞,使用字尾無空格代表15-16,字尾一個空格代表17-18,字尾兩個空格代表19-20
    {"name": "圖書館"},{"name": "數字"},{"name": "資源"},{"name": "網際網路"},
    {"name": "大資料"},{"name": "突發事件"},{"name": "政府"},{"name": "媒體"},
    {"name": "文獻整理"},{"name": "數字化"},{"name": "口述資料"},{"name": "古籍"},
    {"name": "數字資源"},{"name": "版權"},{"name": "作品"},{"name": "語義"}, 
    {"name": "知識"},{"name": "資訊熵"},{"name": "産業鍊"},{"name": "關聯資料"},
    {"name": "閱讀推廣"},{"name": "農民工"},{"name": "公民"},{"name": "滿意度"},
    #15-16
    {"name": "圖書館 "},{"name": "閱讀 "},{"name": "資訊服務 "},{"name": "公共 "},
    {"name": "知識 "},{"name": "科學 "},{"name": "大資料 "},{"name": "專利 "},
    {"name": "文獻 "},{"name": "文化遺産 "},{"name": "醫藥 "},{"name": "數字化 "},
    {"name": "文獻整理 "},{"name": "出土 "},{"name": "藏文 "},{"name": "佛教 "},
    {"name": "科技館 "},{"name": "科學素養 "},{"name": "檔案 "},{"name": "保障體系 "},  
    {"name": "數字人文 "},{"name": "扶貧 "},{"name": "西北 "},{"name": "貧困 "},    
    #17-18
    {"name": "檔案  "},{"name": "公共衛生  "},{"name": "線上健康社群  "},{"name": "大資料  "},
    {"name": "文獻整理  "},{"name": "出土  "},{"name": "史料  "},{"name": "民間  "},
    {"name": "圖書館  "},{"name": "公共圖書館  "},{"name": "閱讀推廣  "},{"name": "知識服務  "},
    {"name": "資料庫  "},{"name": "知識流動  "},{"name": "科技成果  "},{"name": "轉化  "},
    {"name": "情報  "},{"name": "知識圖譜  "},{"name": "突發事件  "},{"name": "危機  "},
    {"name": "學術論文  "},{"name": "科研人員  "},{"name": "學術交流  "},{"name": "知識  "},
    #19-20 over
    #主題詞 start 
    {"name": "圖書館建設"},
    {"name": "大資料輿情"},
    {"name": "文獻數字化"},
    {"name": "知識産權"},
    {"name": "資料科學"},
    {"name": "資訊服務"},
    #15-16over
    {"name": "圖書館建設 "},
    {"name": "大資料科研 "},
    {"name": "文獻數字化 "},
    {"name": "古籍文獻整理 "},
    {"name": "資訊素養教育 "},
    {"name": "資訊扶貧 "},
    #17-18over
    {"name": "大資料健康  "},
    {"name": "古籍文獻整理  "},
    {"name": "資訊服務  "},
    {"name": "資訊産業  "},
    {"name": "大資料輿情  "},
    {"name": "資料科學  "},
    #19-20 over
]

links = [  # 每一條鍊路的資料,包含:父節點source + 子節點target + 資料值value(根據權重*100不四舍五入)
    #一年的——兩年連結——新一年的
    #第一年的關鍵詞需要連結後兩年的
    #先将1年的關鍵詞連接配接到對應的主題詞,然後比對下一年對應的關鍵詞,接着關鍵詞繼續連結到對應的主題詞
    {"source": "圖書館", "target": "圖書館建設", "value": 3},   
    {"source": "數字", "target": "圖書館建設", "value": 1},
    {"source": "資源", "target": "圖書館建設", "value": 2},
    {"source": "網際網路", "target": "圖書館建設", "value": 1},
    {"source": "大資料", "target": "大資料輿情", "value": 2},   
    {"source": "突發事件", "target": "大資料輿情", "value": 2},
    {"source": "政府", "target": "大資料輿情", "value": 1},
    {"source": "媒體", "target": "大資料輿情", "value": 1},
    {"source": "文獻整理", "target": "文獻數字化", "value": 3},   
    {"source": "數字化", "target": "文獻數字化", "value": 2},
    {"source": "口述資料", "target": "文獻數字化", "value": 1},
    {"source": "古籍", "target": "文獻數字化", "value": 2},
    {"source": "數字資源", "target": "知識産權", "value": 1},   
    {"source": "版權", "target": "知識産權", "value": 2},
    {"source": "作品", "target": "知識産權", "value": 1},
    {"source": "語義", "target": "知識産權", "value": 1},
    {"source": "知識", "target": "資料科學", "value": 2},   
    {"source": "資訊熵", "target": "資料科學", "value": 1},
    {"source": "産業鍊", "target": "資料科學", "value": 1},
    {"source": "關聯資料", "target": "資料科學", "value": 1},
    {"source": "閱讀推廣", "target": "資訊服務", "value": 2},   
    {"source": "農民工", "target": "資訊服務", "value": 1},
    {"source": "公民", "target": "資訊服務", "value": 2},
    {"source": "滿意度", "target": "資訊服務", "value": 1},#15-16關鍵詞連結到主題詞
    #15-16主題詞連接配接到17-18關鍵詞,同時需要15-16主題詞連結到19-20關鍵詞
    {"source": "圖書館建設", "target": "圖書館 ", "value": 3},   
    {"source": "圖書館建設", "target": "圖書館  ", "value": 6},
    {"source": "大資料輿情", "target": "大資料 ", "value": 4},
    {"source": "大資料輿情", "target": "大資料  ", "value": 3},
    {"source": "大資料輿情", "target": "突發事件  ", "value": 2},
    {"source": "文獻數字化", "target": "文獻整理 ", "value": 4},
    {"source": "文獻數字化", "target": "數字化 ", "value": 1},   
    {"source": "文獻數字化", "target": "文獻整理  ", "value": 1},    
    {"source": "資料科學", "target": "知識 ", "value": 3},
    {"source": "資料科學", "target": "知識  ", "value": 3},
    {"source": "資訊服務", "target": "閱讀推廣  ", "value": 2},
    #開始将17-18年關鍵詞連結到當年主題詞
    {"source": "圖書館 ", "target": "圖書館建設 ", "value": 6},
    {"source": "閱讀 ", "target": "圖書館建設 ", "value": 2},   
    {"source": "資訊服務 ", "target": "圖書館建設 ", "value": 1},
    {"source": "公共 ", "target": "圖書館建設 ", "value": 1},
    {"source": "知識 ", "target": "大資料科研 ", "value": 3},
    {"source": "科學 ", "target": "大資料科研 ", "value": 2},   
    {"source": "大資料 ", "target": "大資料科研 ", "value": 4},
    {"source": "專利 ", "target": "大資料科研 ", "value": 1},
    {"source": "文獻 ", "target": "文獻數字化 ", "value": 4},
    {"source": "文化遺産 ", "target": "文獻數字化 ", "value": 1},   
    {"source": "醫藥 ", "target": "文獻數字化 ", "value": 1},
    {"source": "數字化 ", "target": "文獻數字化 ", "value": 2},
    {"source": "文獻整理 ", "target": "古籍文獻整理 ", "value": 4},
    {"source": "出土 ", "target": "古籍文獻整理 ", "value": 1},   
    {"source": "藏文 ", "target": "古籍文獻整理 ", "value": 1},
    {"source": "佛教 ", "target": "古籍文獻整理 ", "value": 1},
    {"source": "科技館 ", "target": "資訊素養教育 ", "value": 2},
    {"source": "科學素養 ", "target": "資訊素養教育 ", "value": 3},   
    {"source": "檔案 ", "target": "資訊素養教育 ", "value": 1},
    {"source": "保障體系 ", "target": "資訊素養教育 ", "value": 1},
    {"source": "數字人文 ", "target": "資訊扶貧 ", "value": 1},
    {"source": "扶貧 ", "target": "資訊扶貧 ", "value": 2},   
    {"source": "西北 ", "target": "資訊扶貧 ", "value": 1},
    {"source": "貧困 ", "target": "資訊扶貧 ", "value": 1},
    #17-18主題詞連接配接到19-20關鍵詞
    {"source": "圖書館建設 ", "target": "圖書館  ", "value": 3},
    {"source": "大資料科研 ", "target": "大資料  ", "value": 3},   
    {"source": "大資料科研 ", "target": "知識  ", "value": 1},  
    {"source": "古籍文獻整理 ", "target": "文獻整理  ", "value": 4},
    {"source": "古籍文獻整理 ", "target": "出土  ", "value": 1},
    {"source": "資訊素養教育 ", "target": "檔案  ", "value": 1},
    #開始将19-20年關鍵詞連結到當年主題詞
    {"source": "檔案  ", "target": "大資料健康  ", "value": 3},   
    {"source": "公共衛生  ", "target": "大資料健康  ", "value": 2},
    {"source": "線上健康社群  ", "target": "大資料健康  ", "value": 1},
    {"source": "大資料  ", "target": "大資料健康  ", "value": 3},
    {"source": "文獻整理  ", "target": "古籍文獻整理  ", "value": 4},
    {"source": "出土  ", "target": "古籍文獻整理  ", "value": 1},   
    {"source": "史料  ", "target": "古籍文獻整理  ", "value": 1},
    {"source": "民間  ", "target": "古籍文獻整理  ", "value": 2},
    {"source": "圖書館  ", "target": "資訊服務  ", "value": 3},
    {"source": "公共圖書館  ", "target": "資訊服務  ", "value": 2},   
    {"source": "閱讀推廣  ", "target": "資訊服務  ", "value": 2},
    {"source": "知識服務  ", "target": "資訊服務  ", "value": 1},
    {"source": "資料庫  ", "target": "資訊産業  ", "value": 4},
    {"source": "知識流動  ", "target": "資訊産業  ", "value": 2},   
    {"source": "科技成果  ", "target": "資訊産業  ", "value": 2},
    {"source": "轉化  ", "target": "資訊産業  ", "value": 1},
    {"source": "情報  ", "target": "大資料輿情  ", "value": 5},
    {"source": "知識圖譜  ", "target": "大資料輿情  ", "value": 3},
    {"source": "突發事件  ", "target": "大資料輿情  ", "value": 3},   
    {"source": "危機  ", "target": "大資料輿情  ", "value": 1},
    {"source": "學術論文  ", "target": "資料科學  ", "value": 3},
    {"source": "科研人員  ", "target": "資料科學  ", "value": 3},
    {"source": "學術交流  ", "target": "資料科學  ", "value": 1},   
    {"source": "知識  ", "target": "資料科學  ", "value": 1},
]

c = (
    Sankey()
    .add(
        "",
        nodes,
        links,
        linestyle_opt=opts.LineStyleOpts(opacity=0.2, curve=0.5, color="source"),
        label_opts=opts.LabelOpts(position="right"),
    )
    .set_global_opts(title_opts=opts.TitleOpts(title="主題演化"))
    
)
c.render("主題演化.html")    #生成HTML檔案