Python基於pyecharts實現關聯圖繪製
阿新 • • 發佈:2020-03-28
生活中有很多需要用到關聯圖的地方,至少我認為的是這樣的圖:https://www.echartsjs.com/examples/zh/editor.html?c=graph-npm
我是在使用Word2Vec計算關聯詞的餘弦距離之後,想要更好的展示出來的時候,遇到的這種情況,就做了下拓展。
畫圖的步驟主要分為:
1. 將距離資料(或者相關資料)讀入;
2. 按照一定的格式和引數將資料儲存為json字串;
3. 根據json串,繪製關聯圖。
具體而言,主要是:
<1>. 首先有一批資料,如圖所示:
<2>. 匯入所需要的包
import json
import pandas as pdimport random
import copy
<3>. 產生顏色隨機值的函式
# 隨機顏色 def randomcolor_func(): color_char = ['1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'] color_code = "" for i in range(6): color_code += color_char[random.randint(0,14)] # randint包括前後節點0和14 return "#"+color_code
<4>. 生成隨機座標
# 隨機座標 #生成隨機數,浮點型別 def generate_position(n): # n = 10 for i in range(n): x = round(random.uniform(-2000,2000),5) #一定範圍內的隨機數,範圍可變 y = round(random.uniform(-2000,5) #控制隨機數的精度round(數值,精度) return x,y
<5>. 生成json格式的節點資料
def create_json(data,weights): # 自定義節點 address_dict = {"nodes":[],"edges":[]} node_dict = { "color": "","label": "","attributes": {},"y": None,"x": None,"id": "","size": None } edge_dict = { "sourceID": "","targetID": "","size": None } # 給節點賦值 for ii in range(len(data)): for jj in range(len(data.iloc[ii])): # node,"attributes"屬性可自行設定 node_dict[r"color"] = randomcolor_func() node_dict[r"label"] = data.iloc[ii,jj] x,y = generate_position(1) node_dict[r"y"] = y node_dict[r"x"] = x node_dict[r"id"] = data.iloc[ii,jj] node_dict[r"size"] = int(weights.loc[data.iloc[ii,jj]]) tmp_node = copy.deepcopy(node_dict) address_dict[r"nodes"].append(tmp_node) for ii in range(len(data)): for jj in range(1,len(data.iloc[ii])): # edge edge_dict[r"sourceID"] = data.iloc[ii,0] edge_dict[r"targetID"] = data.iloc[ii,jj] edge_dict[r"size"] = 2 tmp_edge = copy.deepcopy(edge_dict) address_dict["edges"].append(tmp_edge) return address_dict
<6>. 主函式生成json資料
if __name__ == '__main__': # read data data = pd.read_excel(r'test_josn_data.xlsx',0) weights = pd.DataFrame({"詞頻":[100,40,30,20,90,50,35,14,85,38,29,10]},index = ['球類','籃球','足球','羽毛球','美食','肯德基','火鍋','烤魚','飲料','可樂','紅茶','奶茶']) #建立索引權值列表 address_dict = create_json(data,weights) with open("write_json.json","w",encoding='utf-8') as f: # json.dump(dict_,f) # 寫為一行 json.dump(address_dict,f,indent=2,ensure_ascii=False) # 寫為多行
最後形成的json資料如下:
<7>. 繪製關聯圖,裡面的檔案讀取和儲存地址自行修改,write_json.json 就是上面儲存的json檔案
import pyecharts.options as opts from pyecharts.charts import Graph import json with open(r"D:\Python_workspace\spyder_space\test_各種功能\write_json.json",encoding='utf-8') as f: #設定以utf-8解碼模式讀取檔案,encoding引數必須設定,否則預設以gbk模式讀取檔案,當檔案中包含中文時,會報錯 data = json.load(f) #print(data) nodes = [ { "x": node["x"],"y": node["y"],"id": node["id"],"name": node["label"],"symbolSize": node["size"],"itemStyle": {"normal": {"color": node["color"]}},} for node in data["nodes"] ] edges = [{"source": edge["sourceID"],"target": edge["targetID"]} for edge in data["edges"]] ( Graph(init_opts=opts.InitOpts(width="1600px",height="800px")) .add( series_name="",nodes=nodes,links=edges,layout="none",is_roam=True,is_focusnode=True,label_opts=opts.LabelOpts(is_show=True),linestyle_opts=opts.LineStyleOpts(width=0.5,curve=0.3,opacity=0.7),) .set_global_opts(title_opts=opts.TitleOpts(title="熱詞對應的關聯詞")) .render("關聯詞圖.html") )
最後,就生成了最開始的那張圖。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。