程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Some implementations of Python mulberry graph

編輯:Python

Preface

Recent work needs , Doing user path analysis , I hereby write this article to record
The mainstream mulberry map on the Internet is probably as shown in the figure below , Our article also restores the following effects

Installation steps

Realize mulberry diagram , I borrowed python Of pyecharts Components
pyecharts It's actually Echarts And python Docking

  1. install python, I won't go into that
  2. install pyecharts
pip3 install pyecharts
  1. install openpyxl
pip3 install openpyxl

Attach the official document :https://gallery.pyecharts.org/#/README

Implementation code


import pandas as pd from pyecharts.charts import Sankey from pyecharts import options as opts import os from pyecharts.charts import Pie,Bar,Map,Page,Funnel def get_data(df): nodes =[] for i in range(2): vales=df.iloc[:,i].unique() for value in vales: dic={
}
dic['name']=value nodes.append(dic) nodes1 = [] for id in nodes: if id not in nodes1: nodes1.append(id) # print(nodes1) links=[] for i in df.values: dic={
}
dic['source']=i[0]
dic['target']=i[1]
dic['value']=i[2]
links.append(dic)
return nodes1,links
# print(links)
def get_tu(tablename,df):
nodes1,links = get_data(df)
sankey = (
Sankey(init_opts=opts.InitOpts(width="2000px", height="800px"))
.add(
tablename,
nodes1,
links,
pos_top="10%",
node_width = 30, # The width of each sangee rectangle
node_gap= 12, # The interval between any two rectangular nodes in each column of the sanggi graph .
is_draggable = True,
layout_iterations = 5,
# focus_node_adjacency=True,
itemstyle_opts=opts.ItemStyleOpts(border_width=2, border_color="#aaa"),
linestyle_opt=opts.LineStyleOpts(opacity=0.8, curve=0.5, color='source'),
label_opts=opts.LabelOpts(position='right'),
)
.set_global_opts(title_opts=opts.TitleOpts(title=" User path analysis "))
)
return sankey
file ='/Users/superyue/Desktop/tu/user2.xlsx'
df_0826 = pd.read_excel(file,sheet_name='result')
sk1 = get_tu('tets',df_0826)
sk1.render("/Users/superyue/Desktop/tu/test.html")

The effect is as follows :

summary

If data is thrown in , The program also runs successfully , But the effect picture can't come out
Then it must be source and target Corresponding excel Column duplicate
as well as source perhaps target Try not to appear INT Data of type ( Of course, it can also be modified through code )


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved