{"id":13556164,"url":"https://github.com/Pierre-Sassoulas/pySankey","last_synced_at":"2025-04-03T09:30:52.752Z","repository":{"id":39633017,"uuid":"105513747","full_name":"Pierre-Sassoulas/pySankey","owner":"Pierre-Sassoulas","description":"This is the maintened version of PySankey (pySankeyBeta on Pypi)","archived":false,"fork":true,"pushed_at":"2024-10-30T13:46:38.000Z","size":1431,"stargazers_count":39,"open_issues_count":4,"forks_count":8,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-06T09:23:57.632Z","etag":null,"topics":["hacktoberfest","sankey-diagram"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"anazalea/pySankey","license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Pierre-Sassoulas.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2017-10-02T08:39:02.000Z","updated_at":"2025-01-14T14:30:48.000Z","dependencies_parsed_at":"2023-02-08T09:00:28.782Z","dependency_job_id":"ef73f977-9e6c-4897-8f60-47c2c6af4103","html_url":"https://github.com/Pierre-Sassoulas/pySankey","commit_stats":{"total_commits":123,"total_committers":16,"mean_commits":7.6875,"dds":0.3902439024390244,"last_synced_commit":"f24585f819ada7c3e56fa70b745225e7b046d535"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pierre-Sassoulas%2FpySankey","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pierre-Sassoulas%2FpySankey/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pierre-Sassoulas%2FpySankey/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pierre-Sassoulas%2FpySankey/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Pierre-Sassoulas","download_url":"https://codeload.github.com/Pierre-Sassoulas/pySankey/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246976131,"owners_count":20863021,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hacktoberfest","sankey-diagram"],"created_at":"2024-08-01T12:03:40.518Z","updated_at":"2025-04-03T09:30:51.377Z","avatar_url":"https://github.com/Pierre-Sassoulas.png","language":"Jupyter Notebook","funding_links":[],"categories":["Jupyter Notebook","hacktoberfest"],"sub_categories":[],"readme":"# pySankey\n\nUses matplotlib to create simple \u003ca href=\"https://en.wikipedia.org/wiki/Sankey_diagram\"\u003e\nSankey diagrams\u003c/a\u003e flowing only from left to right.\n\n[![PyPI version](https://badge.fury.io/py/pySankeyBeta.svg)](https://badge.fury.io/py/pySankeyBeta)\n[![Build Status](https://github.com/Pierre-Sassoulas/pySankey/actions/workflows/ci.yaml/badge.svg)](https://github.com/Pierre-Sassoulas/pySankey/actions/workflows/ci.yaml)\n[![Coverage Status](https://coveralls.io/repos/github/Pierre-Sassoulas/pySankey/badge.svg?branch=master)](https://coveralls.io/github/Pierre-Sassoulas/pySankey?branch=master)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)\n[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)\n\n## Examples\n\n### Simple expected/predicted example with fruits.txt:\n\n`pysankey` contains a simple expected/predicted dataset called `fruits.txt` which looks\nlike the following:\n\n\u003cdiv\u003e\n\u003ctable border=\"1\" class=\"dataframe\"\u003e\n  \u003cthead\u003e\n    \u003ctr style=\"text-align: right;\"\u003e\n      \u003cth\u003e\u003c/th\u003e\n      \u003cth\u003etrue\u003c/th\u003e\n      \u003cth\u003epredicted\u003c/th\u003e\n    \u003c/tr\u003e\n  \u003c/thead\u003e\n  \u003ctbody\u003e\n    \u003ctr\u003e\n      \u003cth\u003e0\u003c/th\u003e\n      \u003ctd\u003eblueberry\u003c/td\u003e\n      \u003ctd\u003eorange\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003cth\u003e1\u003c/th\u003e\n      \u003ctd\u003elime\u003c/td\u003e\n      \u003ctd\u003eorange\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003cth\u003e2\u003c/th\u003e\n      \u003ctd\u003eblueberry\u003c/td\u003e\n      \u003ctd\u003elime\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003cth\u003e3\u003c/th\u003e\n      \u003ctd\u003eapple\u003c/td\u003e\n      \u003ctd\u003eorange\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003cth\u003e...\u003c/th\u003e\n      \u003ctd\u003e...\u003c/td\u003e\n      \u003ctd\u003e...\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003cth\u003e996\u003c/th\u003e\n      \u003ctd\u003elime\u003c/td\u003e\n      \u003ctd\u003eorange\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003cth\u003e997\u003c/th\u003e\n      \u003ctd\u003eblueberry\u003c/td\u003e\n      \u003ctd\u003eorange\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003cth\u003e998\u003c/th\u003e\n      \u003ctd\u003eorange\u003c/td\u003e\n      \u003ctd\u003ebanana\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003cth\u003e999\u003c/th\u003e\n      \u003ctd\u003eapple\u003c/td\u003e\n      \u003ctd\u003elime\u003c/td\u003e\n    \u003c/tr\u003e\n  \u003c/tbody\u003e\n\u003c/table\u003e\n\u003cp\u003e1000 rows × 2 columns\u003c/p\u003e\n\u003c/div\u003e\n\nYou can generate a sankey's diagram with this code:\n\n```python\nimport pandas as pd\nfrom pysankey import sankey\nimport matplotlib.pyplot as plt\n\n\ndf = pd.read_csv(\n    'fruits.txt',\n    sep=' ',\n    names=['true', 'predicted']\n)\n\ncolorDict = {\n    'apple':'#f71b1b',\n    'blueberry':'#1b7ef7',\n    'banana':'#f3f71b',\n    'lime':'#12e23f',\n    'orange':'#f78c1b',\n    'kiwi':'#9BD937'\n}\n\nlabels = list(colorDict.keys())\nleftLabels = [label for label in labels if label in df['true'].values]\nrightLabels = [label for label in labels if label in df['predicted'].values]\n\n# Create the sankey diagram\nax = sankey(\n    left=df['true'],\n    right=df['predicted'],\n    leftLabels=leftLabels,\n    rightLabels=rightLabels,\n    colorDict=colorDict,\n    aspect=20,\n    fontsize=12\n)\n\nplt.show() # to display\n```\n\n![Fruity Alchemy](.github/img/fruits.png)\n\n### Plotting preprocessed data using weights\n\nHowever, the data may not always be available in the format mentioned in the previous\nexample (for instance, if the dataset is too large). In such cases, the weights between\nthe true and predicted labels can be calculated in advance and used to create the Sankey\ndiagram. In this example, we will continue working with the data that was loaded in the\nprevious example:\n\n```python\n# Calculate the weights from the fruits dataframe\ndf = df.groupby([\"true\", \"predicted\"]).size().reset_index()\nweights = df[0].astype(float)\n\nax = sankey(\n    left=df['true'],\n    right=df['predicted'],\n    rightWeight=weights,\n    leftWeight=weights,\n    leftLabels=leftLabels,\n    rightLabels=rightLabels,\n    colorDict=colorDict,\n    aspect=20,\n    fontsize=12\n)\n\nplt.show() # to display\n```\n\n![Fruity Alchemy](.github/img/fruits_weighted.png)\n\n### pysankey function overview\n\n\u003e `sankey(left, right, leftWeight=None, rightWeight=None, colorDict=None, leftLabels=None, rightLabels=None, aspect=4, rightColor=False, fontsize=14, ax=None, color_gradient=False, alphaDict=None)`\n\u003e\n\u003e **left**, **right** : NumPy array of object labels on the left and right of the\n\u003e diagram\n\u003e\n\u003e **leftWeight**, **rightWeight** : Numpy arrays of the weights each strip\n\u003e\n\u003e **colorDict** : Dictionary of colors to use for each label\n\u003e\n\u003e **leftLabels**, **rightLabels** : order of the left and right labels in the diagram\n\u003e\n\u003e **aspect** : vertical extent of the diagram in units of horizontal extent\n\u003e\n\u003e **rightColor** : If true, each strip in the diagram will be be colored according to\n\u003e its left label\n\u003e\n\u003e **fontsize** : Fontsize to be used for the labels\n\u003e\n\u003e **ax** : matplotlib axes to plot on, otherwise uses current axes.\n\n## Important informations\n\nUse of `figureName`, `closePlot`, `figSize` in `sankey()` is deprecated and will be\nremove in a future version. This is done so matplotlib is used more transparently as\nthis [issue](https://github.com/anazalea/pySankey/issues/26#issue-429312025) on the\noriginal github repo suggested.\n\nNow, `sankey` does less of the customization and let the user do it to their liking by\nreturning a matplotlib `Axes` object, which mean the user also has access to the\n`Figure` to customise. Then they can choose what to do with it - showing it, saving it\nwith much more flexibility.\n\n### Recommended changes to your code\n\n- To save a figure, one can simply do:\n\n```python\n  plt.savefig(\"\u003cfigureName\u003e.png\", bbox_inches=\"tight\", dpi=150)\n```\n\n- The `closePlot` is not needed anymore because without `plt.show()` after `sankey()`,\n  no plot is displayed. You can still do `plt.close()` to be sure to not display this\n  plot if you display other plots afterwards.\n\n- You can modify the sankey size by changing the one from the matplotlib figure.\n  ```python\n    plt.gcf().set_size_inches(figSize)\n  ```\n\n## Package development\n\n    pip3 install -e \".[test]\"\n\n### Lint\n\n    pylint pysankey\n\n### Testing\n\n    python -m unittest\n\n### Coverage\n\n    coverage run -m unittest\n    coverage html\n    # Open htmlcov/index.html in a navigator\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FPierre-Sassoulas%2FpySankey","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FPierre-Sassoulas%2FpySankey","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FPierre-Sassoulas%2FpySankey/lists"}