制作简单、漂亮的sankey图表(beta版)
pySankeyBeta的Python项目详细描述
Pysankey
使用matplotlib创建仅从左向右流动的简单 Sankey diagrams。
要求
需要python tk(对于python 2.7)或python3tk(对于python3.x),您可以 使用以下命令安装其他要求:
pip install -r requirements.txt
示例
带水果.txt:
true | predicted | |
---|---|---|
0 | blueberry | orange |
1 | lime | orange |
2 | blueberry | lime |
3 | apple | orange |
... | ... | ... |
996 | lime | orange |
997 | blueberry | orange |
998 | orange | banana |
999 | apple | lime |
1000行×2列
您可以使用以下代码生成sankey的关系图:
importpandasaspdfrompysankeyimportsankeypd.options.display.max_rows=8df=pd.read_csv('pysankey/fruits.txt',sep=' ',names=['true','predicted'])colorDict={'apple':'#f71b1b','blueberry':'#1b7ef7','banana':'#f3f71b','lime':'#12e23f','orange':'#f78c1b','kiwi':'#9BD937'}sankey(df['true'],df['predicted'],aspect=20,colorDict=colorDict,leftLabels=['banana','orange','blueberry','apple','lime'],rightLabels=['orange','banana','blueberry','apple','lime','kiwi'],fontsize=12,figureName="fruit")# Result is in "fruit.png"
您还可以使用重量:
,customer,good,revenue
0,John,fruit,5.5
1,Mike,meat,11.0
2,Betty,drinks,7.0
3,Ben,fruit,4.0
4,Betty,bread,2.0
5,John,bread,2.5
6,John,drinks,8.0
7,Ben,bread,2.0
8,Mike,bread,3.5
9,John,meat,13.0
importpandasaspdfrompysankeyimportsankeydf=pd.read_csv('pysankey/customers-goods.csv',sep=',',names=['id','customer','good','revenue'])weight=df['revenue'].values[1:].astype(float)sankey(left=df['customer'].values[1:],right=df['good'].values[1:],rightWeight=weight,leftWeight=weight,aspect=20,fontsize=20,figureName="customer-good")# Result is in "customer-good.png"
软件包开发
皮棉
pylint pysankey
测试
python -m unittest
覆盖范围
coverage run -m unittest
coverage html
# Open htmlcov/index.html in a navigator