Package: pySankey2
pySankey2Uses matplotlib to create simpleSankey diagramsflowing from left to right.A fork of a fork ofpySankey.RequirementsRequires python-tk (for python 2.7) or python3-tk (for python 3.x) install withapt-getor your package manager.You can install the other requirements with:pipinstall-rrequirements.txtExamplesWith fruits.txt :truepredicted0blueberryorange1limeorange2blueberrylime3appleorange.........996limeorange997blueberryorange998orangebanana999applelime1000 rows × 2 columnsYou can generate a sankey's diagram with this code (colorDictis optional):importpandasaspdimportmatplotlib.pyplotaspltfrompysankeyimportsankeydf=pd.read_csv('pysankey/tests/fruits.txt',sep=' ',names=['true','predicted'])colorDict={'apple':'#f71b1b','blueberry':'#1b7ef7','banana':'#f3f71b','lime':'#12e23f','orange':'#f78c1b','kiwi':'#9BD937'}ax=sankey(df['true'],df['predicted'],aspect=20,colorDict=colorDict,leftLabels=['banana','orange','blueberry','apple','lime'],rightLabels=['orange','banana','blueberry','apple','lime','kiwi'],fontsize=12)plt.show()# to displayplt.savefig('fruit.png',bbox_inches='tight')# to saveWith customer-goods.csv :,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.0You could also weight:importpandasaspdimportmatplotlib.pyplotaspltfrompysankeyimportsankeydf=pd.read_csv('pysankey/tests/customers-goods.csv',sep=',',names=['id','customer','good','revenue'])weight=df['revenue'].values[1:].astype(float)ax=sankey(left=df['customer'].values[1:],right=df['good'].values[1:],rightWeight=weight,leftWeight=weight,aspect=20,fontsize=20)plt.show()# to displayplt.savefig('customers-goods.png',bbox_inches='tight')# to saveSimilar to seaborn, you can pass a matplotlibAxestosankeyfunction with the keywordax=:importpandasaspdimportmatplotlib.pyplotaspltfrompysankeyimportsankeydf=pd.read_csv('pysankey/tests/fruits.txt',sep=' ',names=['true','predicted'])colorDict={'apple':'#f71b1b','blueberry':'#1b7ef7','banana':'#f3f71b','lime':'#12e23f','orange':'#f78c1b'}ax1=plt.axes()ax1=sankey(df['true'],df['predicted'],aspect=20,colorDict=colorDict,fontsize=12,ax=ax1)plt.show()Important informationsUse offigureName,closePlotandfigSizeinsankey()has been removed.
This is done so matplotlib is used more transparently as this [issue] suggested (https://github.com/anazalea/pySankey/issues/26#issue-429312025) on the original github repo.Now,sankey()does less of the customization and let the user do it to their liking by returning a matplotlibAxesobject, which mean the user also has access to theFigureto customise.
Then they can choose what to do with it - showing it, saving it with much more flexibility.Recommended changes to your code from pySankeyTo save a figure, aftersankey(), one can simply do:plt.savefig("<figureName>.png",bbox_inches="tight",dpi=150)To display the diagram, simply doplt.show()aftersankey().You can modify the sankey size by changing the one from the matplotlib figure.plt.gcf().set_size_inches(figSize)It is possible to modify the diagram font looks, for example, add the following lines before callingsankey():plt.rc("text",usetex=False)plt.rc("font",family="serif")Package developmentLintpylint pysankeyTestingpython -m unittestCoveragecoverage run -m unittest
coverage html
# Open htmlcov/index.html in a navigator