桑基图(Sankey diagram),即桑基能量分流图,也叫桑基能量平衡图。它是一种特定类型的流程图,图中延伸的分支的宽度对应数据流量的大小,通常应用于能源、材料成分、金融等数据的可视化分析。因1898年Matthew Henry Phineas Riall Sankey绘制的“蒸汽机的能源效率图”而闻名,此后便以其名字命名为“桑基图”。
安装社区命令sankey:
net install sankey, from(https://www.mjcrowther.co.uk/code/sankey)
更新社区命令sankey:
adoupdate sankey
演示数据:
use https://www.mjcrowther.co.uk/data/sankey_eg1 sankey start stop freq, colors(3 "green" 6 "red") linkcolor(lightgreen)
首次运行可能会报错:
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
ModuleNotFoundError: No module named ‘plotly’
(28 lines skipped)
(error occurred while loading sankey.ado)
r(7102);
提示Python的plotly模块未找到,需要手动安装,因为电脑安装的是Anaconda发行版的Python环境,在电脑中打开Anaconda Powershell Prompt菜单, 输入 安装plotly模块的代码:
conda install -c plotly plotly
安装过程中,需要输入y,以确认安装。
安装完成后,再次输入Stata命令:
sankey start stop freq, colors(3 "green" 6 "red") linkcolor(lightgreen)
会自动打开一个网址类似127.0.0.1:51542 的本机网页,网页里就是生成的桑基图。
说明:社区命令sankey基于Stata 16,且需要安装Python扩展(http://www.epiman.cn/thread-139202-1-1.html)。