您的位置:首页 > 娱乐 > 明星 > 网页设计与制作教程实验报告_内容营销策划方案_热门seo推广排名稳定_生猪价格今日猪价

网页设计与制作教程实验报告_内容营销策划方案_热门seo推广排名稳定_生猪价格今日猪价

2025/1/10 21:07:22 来源:https://blog.csdn.net/weixin_39293132/article/details/144852216  浏览:    关键词:网页设计与制作教程实验报告_内容营销策划方案_热门seo推广排名稳定_生猪价格今日猪价
网页设计与制作教程实验报告_内容营销策划方案_热门seo推广排名稳定_生猪价格今日猪价

流程图(三)利用python绘制桑基图

桑基图(Sankey diagram)简介

1

桑基图经常用于能源、金融行业,对材料、成本的流动进行可视化分析。现在很多互联网行业还使用桑基图做用户流动性分析,能很好地观察数据成分的变动大小及变动方向。

快速绘制

  1. 基于plotly

    import plotly.graph_objects as go
    import urllib, json# 导入数据
    url = 'https://raw.githubusercontent.com/plotly/plotly.js/master/test/image/mocks/sankey_energy.json'
    response = urllib.request.urlopen(url)
    data = json.loads(response.read())# 将所有的"magenta"颜色更改为rgba(255,0,255, 0.8),并将所有连接颜色更改为其对应的'source'节点颜色且透明度是0.4
    opacity = 0.4
    data['data'][0]['node']['color'] = ['rgba(255,0,255, 0.8)' if color == "magenta" else color for color in data['data'][0]['node']['color']]
    data['data'][0]['link']['color'] = [data['data'][0]['node']['color'][src].replace("0.8", str(opacity))for src in data['data'][0]['link']['source']]fig = go.Figure(data=[go.Sankey(valueformat = ".0f",valuesuffix = "TWh",# 定义节点node = dict(pad = 15,thickness = 15,line = dict(color = "black", width = 0.5),label =  data['data'][0]['node']['label'],color =  data['data'][0]['node']['color']),# 添加连接link = dict(source =  data['data'][0]['link']['source'],target =  data['data'][0]['link']['target'],value =  data['data'][0]['link']['value'],label =  data['data'][0]['link']['label'],color =  data['data'][0]['link']['color']
    ))])fig.update_layout(title_text="Energy forecast for 2050<br>Source: Department of Energy & Climate Change, Tom Counsell via <a href='https://bost.ocks.org/mike/sankey/'>Mike Bostock</a>",font_size=10)
    

    2

  2. 基于pyecharts

    import pyecharts.options as opts
    from pyecharts.charts import Sankey
    import urllib, json# 导入数据
    url = 'https://echarts.apache.org/examples/data/asset/data/energy.json'
    response = urllib.request.urlopen(url)
    data = json.loads(response.read())c = (Sankey().add(series_name="",nodes=data["nodes"],links=data["links"],itemstyle_opts=opts.ItemStyleOpts(border_width=1, border_color="#aaa"),linestyle_opt=opts.LineStyleOpts(color="source", curve=0.5, opacity=0.5),tooltip_opts=opts.TooltipOpts(trigger_on="mousemove"),).set_global_opts(title_opts=opts.TitleOpts(title=""))
    )c.render_notebook()
    

    3

  3. 基于pysankey

    import pandas as pd
    from pySankey.sankey import sankey# 基于source和target,数据可重复出现,出现次数越多,权重越大(即线越粗)
    url = "https://raw.githubusercontent.com/anazalea/pySankey/master/pysankey/fruits.txt"
    df = pd.read_csv(url, sep=" ", names=["true", "predicted"])colors = {"apple": "#f71b1b","blueberry": "#1b7ef7","banana": "#f3f71b","lime": "#12e23f","orange": "#f78c1b"
    }sankey(df["true"], df["predicted"], aspect=20, colorDict=colors, fontsize=12)
    

    4

    import pandas as pd
    from pySankey.sankey import sankey# 基于source和、target和value,数据可仅出现一次,value即权重
    url = "https://raw.githubusercontent.com/anazalea/pySankey/master/pysankey/customers-goods.csv"
    df = pd.read_csv(url, sep=",")sankey(left=df["customer"], right=df["good"], leftWeight= df["revenue"], rightWeight=df["revenue"], aspect=20, fontsize=20
    )
    

    5

    总结

    以上通过plotly、pyecharts和pysankey快速绘桑基图。

    共勉~

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com