楼主: Lee_iris
1149 0

[学习心得] stata绘制桑基图Sankey plots in Stata using Python [推广有奖]

已卖:1253份资源

教授

47%

还不是VIP/贵宾

-

威望
0
论坛币
1167 个
通用积分
1196.5565
学术水平
36 点
热心指数
43 点
信用等级
35 点
经验
14504 点
帖子
893
精华
0
在线时间
1517 小时
注册时间
2019-3-5
最后登录
2026-1-4

楼主
Lee_iris 在职认证  学生认证  发表于 2022-2-3 19:28:42 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
记录一下学习过程。

stata绘制桑基图需要调用Python,所以第一步是安装Python然后再在stata安装sankey命令包运行命令即可
一、安装Python
去官方网站https://www.python.org/downloads/windows/下载相应的Python安装包,安装

参考帖子:Python安装教程https://bbs.pinggu.org/forum.php ... 00&from^^uid=11374248


二、下载stata的sankey和python安装包
  1.         
  2. net install sankey, from([url]https://www.mjcrowther.co.uk/code/sankey[/url])

  3. ssc install python,replace
复制代码

参考网站:
https://www.mjcrowther.co.uk/software/sankey/

如果出现如下问题
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'

参考网站:
https://www.statalist.org/forums/forum/general-stata-discussion/general/1622989-sankey-visual-analysis
https://blog.stata.com/2020/09/01/stata-python-integration-part-3-how-to-install-python-packages/
如果在窗口出现错误ValueError: check_hostname requires server_hostname
参考网站:
https://www.jianshu.com/p/d4af07d5af27

三、注意
1.startvar stopvar不能是字符串,需要用encode(刚开始用了字符串,结果出不来)
2.安装命令
     
  1.         net install sankey, from([url]https://www.mjcrowther.co.uk/code/sankey[/url])
  2.         help sankey
  3.         ssc install python,replace
  4.         python query
  5.         python which numpy
  6.         shell
复制代码

然后在打开的窗口里输入(网址是清华镜像,下载速度会快一些):
        pip -V
        pip install -i https://pypi.tuna.tsinghua.edu.cn/simple numpy
        pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pandas
        pip install -i https://pypi.tuna.tsinghua.edu.cn/simple matplotlib
        pip install -i https://pypi.tuna.tsinghua.edu.cn/simple scikit-learn
        pip install -i https://pypi.tuna.tsinghua.edu.cn/simple plotly
再回到stata打开数据输入sankey的命令
  1. use [url]https://www.mjcrowther.co.uk/data/sankey_eg1[/url]
  2. sankey start stop freq, colors(3 "green" 6 "red") linkcolor(lightgreen)
复制代码

3.图形导出是网页版的,有时候加载很久也出不来,不确定是什么原因

4.节点不能太多,要不显示不出来5.startvar 和stopvar 为不同的值
参考:https://www.statalist.org/forums ... 1629010#post1629010

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:sankey python Stata Using Plots

已有 1 人评分经验 论坛币 收起 理由
dxystata + 100 + 10 鼓励积极发帖讨论

总评分: 经验 + 100  论坛币 + 10   查看全部评分

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-2-5 04:50