def create_sankey(self, v): #绘制桑基图 sankey = Sankey(self.TITLE_TEXT, self.TITLE_SUBTEXT,width=1200, height=600) category1,category2,category3,category4,category5,category6=self.ATTR[0],self.ATTR[1],self.ATTR[2],self.ATTR[3],self.ATTR[4],self.ATTR[5] nodes = [ {'name': 'Java软件开发'}, {'name': '前端技术'}, {'name': '移动端技术'}, {'name': '自然语言处理'}, {'name': 'Python数据分析'}, {'name': '数据可视化'}, {'name': '量化交易'},{'name': '算法工程'},{'name': '算法优化'}, {'name': '机器学习开发'},{'name': '运维工程'},{'name': '云计算开发'}, {'name': '软件UI设计'},{'name': '计算机视觉'},{'name': '产品经理'},{'name': '深度学习'} ] links = [ {'source': 'Java软件开发', 'target': '前端技术', 'value': 5}, {'source': '前端技术', 'target': '移动端技术', 'value': 12}, {'source': 'Python数据分析', 'target': '自然语言处理', 'value': 10}, {'source': 'Python数据分析', 'target': '数据可视化', 'value': 8}, {'source': '自然语言处理', 'target': '算法工程', 'value': 15}, {'source': '算法工程', 'target': '算法优化', 'value': 15}, {'source': '自然语言处理', 'target': '算法优化', 'value': 30}, {'source': '量化交易', 'target': '算法优化', 'value': 10}, {'source': '自然语言处理', 'target': '移动端技术', 'value': 14}, {'source': '机器学习开发', 'target': '自然语言处理', 'value': 14}, {'source': '机器学习开发', 'target': '算法工程', 'value': 20}, {'source': '软件UI设计', 'target': '移动端技术', 'value': 15}, {'source': '深度学习', 'target': '计算机视觉', 'value': 12}, {'source': '计算机视觉', 'target': '算法优化', 'value': 10}, {'source': '运维工程', 'target': '云计算开发', 'value': 15}, {'source': '前端技术', 'target': '云计算开发', 'value': 20}, {'source': '云计算开发', 'target': '产品经理', 'value': 3}, {'source': '移动端技术', 'target': '产品经理', 'value': 10}, {'source': '算法优化', 'target': '产品经理', 'value': 7}, {'source': '计算机视觉', 'target': '产品经理', 'value': 6}, {'source': '数据可视化', 'target': '产品经理', 'value': 5}, ] sankey.add( "技能树", nodes, links, line_opacity=0.2, line_curve=0.5, line_color="source", is_label_show=True, label_pos="right", ) snippet = TRANSLATOR.translate(sankey.options) options = snippet.as_snippet() return options
def test_sankey_default(): nodes = [ {"name": "category1"}, {"name": "category2"}, {"name": "category3"}, {"name": "category4"}, {"name": "category5"}, {"name": "category6"}, ] links = [ {"source": "category1", "target": "category2", "value": 10}, {"source": "category2", "target": "category3", "value": 15}, {"source": "category3", "target": "category4", "value": 20}, {"source": "category5", "target": "category6", "value": 25}, ] sankey = Sankey("桑基图示例", width=1200, height=600) sankey.add( "sankey", nodes, links, line_opacity=0.2, line_curve=0.5, line_color="source", is_label_show=True, label_pos="right", ) sankey.render() with codecs.open( os.path.join("fixtures", "energy.json"), "r", encoding="utf-8" ) as f: j = json.load(f) sankey = Sankey("桑基图示例", width=1200, height=600) sankey.add( "sankey", nodes=j["nodes"], links=j["links"], line_opacity=0.2, line_curve=0.5, line_color="source", is_label_show=True, label_pos="right", ) sankey.render()
def cross_sankey(df, attr1, attr2, func=len, value=None): if value==None: df2=df.groupby([attr1, attr2])[attr1].agg([func]) else: df2=df.groupby([attr1, attr2])[value].agg([func]) df2=df2.reset_index() nodes = [] for j in [attr1, attr2]: for i in set(df[j]): nodes.append({'name': str(i)}) links = [] for i in range(df2.shape[0]): if value==None: links.append({ 'source':str(df2.loc[i,attr1]), 'target':str(df2.loc[i,attr2]), 'value':df2.loc[i,'len'], }) else: links.append({ 'source':str(df2.loc[i,attr1]), 'target':str(df2.loc[i,attr2]), 'value':df2.loc[i,func.__name__], }) sankey = Sankey("桑基图示例", width=1200, height=600) sankey.add("sankey", nodes, links, line_opacity=0.8, line_curve=0.5, line_color='source', is_label_show=True, label_pos='right') sankey.render()
def sankey_charts(title, nodes, links): style = Style( width=1200, height=800, background_color='#f0f0f0', ) chart = Sankey(title, **style.init_style) chart.add("", nodes, links, line_opacity=0.2, line_curve=0.5, line_color='source', is_label_show=True, label_pos='right') chart.options['toolbox']['show'] = False return chart.render_embed()
def test_sankey_default(): nodes = [ { 'name': 'category1' }, { 'name': 'category2' }, { 'name': 'category3' }, { 'name': 'category4' }, { 'name': 'category5' }, { 'name': 'category6' }, ] links = [{ 'source': 'category1', 'target': 'category2', 'value': 10 }, { 'source': 'category2', 'target': 'category3', 'value': 15 }, { 'source': 'category3', 'target': 'category4', 'value': 20 }, { 'source': 'category5', 'target': 'category6', 'value': 25 }] sankey = Sankey("桑基图示例", width=1200, height=600) sankey.add("sankey", nodes, links, line_opacity=0.2, line_curve=0.5, line_color='source', is_label_show=True, label_pos='right') sankey.render()
def test_sankey_official_data(): import json if PY2: import codecs with codecs.open(os.path.join("..", "json", "energy.json"), "rb") as f: j = json.load(f) else: with open(os.path.join("..", "json", "energy.json"), "r", encoding="utf-8") as f: j = json.load(f) sankey = Sankey("桑基图示例", width=1200, height=600) sankey.add("sankey", nodes=j['nodes'], links=j['links'], line_opacity=0.2, line_curve=0.5, line_color='source', is_label_show=True, label_pos='right') sankey.render()
def test_sankey_default(): nodes = [ { "name": "category1" }, { "name": "category2" }, { "name": "category3" }, { "name": "category4" }, { "name": "category5" }, { "name": "category6" }, ] links = [ { "source": "category1", "target": "category2", "value": 10 }, { "source": "category2", "target": "category3", "value": 15 }, { "source": "category3", "target": "category4", "value": 20 }, { "source": "category5", "target": "category6", "value": 25 }, ] sankey = Sankey("桑基图示例", width=1200, height=600) sankey.add( "sankey", nodes, links, line_opacity=0.2, line_curve=0.5, line_color="source", is_label_show=True, label_pos="right", ) sankey.render() with codecs.open(os.path.join("fixtures", "energy.json"), "r", encoding="utf-8") as f: j = json.load(f) sankey = Sankey("桑基图示例", width=1200, height=600) sankey.add( "sankey", nodes=j["nodes"], links=j["links"], line_opacity=0.2, line_curve=0.5, line_color="source", is_label_show=True, label_pos="right", ) sankey.render()
nodes.append({'name': attr1 + '_' + str(i)}) for i in labels_2: nodes.append({'name': attr2 + '_' + str(i)}) links = [] for i in range(dfv.shape[0]): links.append({ 'source': attr1 + '_' + str(dfv.loc[i, attr1]), 'target': attr2 + '_' + str(dfv.loc[i, attr2]), 'value': dfv.loc[i, 'len'], }) print(nodes) print(links) title = 'Sankey relation between imdb score and profits' sankey = Sankey(title, title_pos='center', width=1200, height=600) sankey.add("sankey", nodes, links, line_opacity=0.7, line_curve=0.5, line_color='source', is_legend_show=False, is_label_show=True, label_pos='right', is_random=True) sankey.render(title + '.html') ##################################################################################### attr = 'title_year' bins = [1900]
def test_sankey(): # sankey_0 nodes = [ { 'name': 'category1' }, { 'name': 'category2' }, { 'name': 'category3' }, { 'name': 'category4' }, { 'name': 'category5' }, { 'name': 'category6' }, ] links = [{ 'source': 'category1', 'target': 'category2', 'value': 10 }, { 'source': 'category2', 'target': 'category3', 'value': 15 }, { 'source': 'category3', 'target': 'category4', 'value': 20 }, { 'source': 'category5', 'target': 'category6', 'value': 25 }] sankey = Sankey("桑基图示例", width=1200, height=600) sankey.add("sankey", nodes, links, line_opacity=0.2, line_curve=0.5, line_color='source', is_label_show=True, label_pos='right') sankey.render() # sankey_1 import json if PY2: import codecs with codecs.open(os.path.join("..", "json", "energy.json"), "rb") as f: j = json.load(f) else: with open(os.path.join("..", "json", "energy.json"), "r", encoding="utf-8") as f: j = json.load(f) sankey = Sankey("桑基图示例", width=1200, height=600) sankey.add("sankey", nodes=j['nodes'], links=j['links'], line_opacity=0.2, line_curve=0.5, line_color='source', is_label_show=True, label_pos='right') sankey.render()
'source': 'Others 1.070%', 'target': 'Public Administration 0.039%', 'value': 0.039082391 }, { 'source': 'Others 1.070%', 'target': 'Other 0.000%', 'value': 5.41204E-08 }] from pyecharts import Sankey def label_formatter(params): return params.value sankey = Sankey("", width=1600, height=900) # sankey.use_theme('dark') sankey.add( '', nodes, links, line_opacity=0.6, label_text_size=16, line_curve=0.6, line_color="target", line_type='dash', is_label_show=True, label_pos="right", sankey_node_gap=20, ) sankey.render()
{'name': 'business', 'value': 10}, {'name': 'officer', 'value': 8}, {'name': 'team1', 'value': 20},{'name': 'team2', 'value': 10}, {'name': 'team3', 'value': 10},{'name': 'team4', 'value': 5}, ] links = [ {'source': 'engineer', 'target': 'team1', 'value': 10}, {'source': 'scientist', 'target': 'team1', 'value': 5}, {'source': 'business', 'target': 'team1', 'value': 3}, {'source': 'designer', 'target': 'team1', 'value': 2}, {'source': 'scientist', 'target': 'team2', 'value': 8}, {'source': 'officier', 'target': 'team2', 'value': 2}, {'source': 'officier', 'target': 'team3', 'value': 3}, {'source': 'officier', 'target': 'team4', 'value': 4}, ] sankey = Sankey("桑基图示例", width=1200, height=600) sankey.add("sankey", nodes, links, line_opacity=0.2, line_curve=0.5, line_color='source', is_label_show=True, label_pos='right') sankey.render() def cross_sankey(df, attr1, attr2, func=len, value=None): if value==None: df2=df.groupby([attr1, attr2])[attr1].agg([func]) else: df2=df.groupby([attr1, attr2])[value].agg([func]) df2=df2.reset_index() nodes = [] for j in [attr1, attr2]: for i in set(df[j]):
def test_sankey_default(): nodes = [ { 'name': 'category1' }, { 'name': 'category2' }, { 'name': 'category3' }, { 'name': 'category4' }, { 'name': 'category5' }, { 'name': 'category6' }, ] links = [{ 'source': 'category1', 'target': 'category2', 'value': 10 }, { 'source': 'category2', 'target': 'category3', 'value': 15 }, { 'source': 'category3', 'target': 'category4', 'value': 20 }, { 'source': 'category5', 'target': 'category6', 'value': 25 }] sankey = Sankey("桑基图示例", width=1200, height=600) sankey.add("sankey", nodes, links, line_opacity=0.2, line_curve=0.5, line_color='source', is_label_show=True, label_pos='right') sankey.render() with codecs.open(os.path.join("fixtures", "energy.json"), "r", encoding='utf-8') as f: j = json.load(f) sankey = Sankey("桑基图示例", width=1200, height=600) sankey.add("sankey", nodes=j['nodes'], links=j['links'], line_opacity=0.2, line_curve=0.5, line_color='source', is_label_show=True, label_pos='right') sankey.render()
def create_charts(): page = Page() style = Style(width=WIDTH, height=HEIGHT) nodes = [ { 'name': 'category1' }, { 'name': 'category2' }, { 'name': 'category3' }, { 'name': 'category4' }, { 'name': 'category5' }, { 'name': 'category6' }, ] links = [{ 'source': 'category1', 'target': 'category2', 'value': 10 }, { 'source': 'category2', 'target': 'category3', 'value': 15 }, { 'source': 'category3', 'target': 'category4', 'value': 20 }, { 'source': 'category5', 'target': 'category6', 'value': 25 }] chart = Sankey("桑基图-默认", **style.init_style) chart.add("sankey", nodes, links, line_opacity=0.2, line_curve=0.5, line_color='source', is_label_show=True, label_pos='right') page.add(chart) chart = Sankey("桑基图-自定义", **style.init_style) chart.add("sankey", nodes=ENERGY['nodes'], links=ENERGY['links'], line_opacity=0.2, line_curve=0.5, line_color='source', is_label_show=True, label_pos='right') page.add(chart) return page
def sankey_charts(): page = Page() chart_init = { "width": WIDTH, "height": HEIGHT, } nodes = [ { 'name': 'category1' }, { 'name': 'category2' }, { 'name': 'category3' }, { 'name': 'category4' }, { 'name': 'category5' }, { 'name': 'category6' }, ] links = [{ 'source': 'category1', 'target': 'category2', 'value': 10 }, { 'source': 'category2', 'target': 'category3', 'value': 15 }, { 'source': 'category3', 'target': 'category4', 'value': 20 }, { 'source': 'category5', 'target': 'category6', 'value': 25 }] chart = Sankey("桑基图-默认", **chart_init) chart.add("sankey", nodes, links, line_opacity=0.2, line_curve=0.5, line_color='source', is_label_show=True, label_pos='right') page.add(chart) chart = Sankey("桑基图-自定义", **chart_init) chart.add("sankey", nodes=ENERGY['nodes'], links=ENERGY['links'], line_opacity=0.2, line_curve=0.5, line_color='source', is_label_show=True, label_pos='right') page.add(chart) return page