forked from anazalea/pySankey
-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updates example section in the Readme #48
Open
Trybnetic
wants to merge
12
commits into
Pierre-Sassoulas:main
Choose a base branch
from
Trybnetic:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8cb32d8
removes jupyter artifacts
Trybnetic 20bdd53
adds extended description of the fruits example and clarifies use for…
Trybnetic 46c2a97
updates status badge and gives ci workflow the name build to match wi…
Trybnetic 8e7d99d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4059d36
fixes too long line
Trybnetic fa8a5a7
Merge branch 'main' of github.com:Trybnetic/pySankey
Trybnetic 52f096a
fixes formatting
Trybnetic 4797b96
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] df097de
moved plot creation script to .github as it has nothing to do with th…
Trybnetic d29527a
simplifies data aggregation in the preprocessing example
Trybnetic 09354cc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1bd43b8
removes old png file from package folder (the ones still used can be …
Trybnetic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import matplotlib.pyplot as plt | ||
import pandas as pd | ||
from pysankey import sankey | ||
|
||
df = pd.read_csv("../pysankey/fruits.txt", sep=" ", names=["true", "predicted"]) | ||
|
||
colorDict = { | ||
"apple": "#f71b1b", | ||
"blueberry": "#1b7ef7", | ||
"banana": "#f3f71b", | ||
"lime": "#12e23f", | ||
"orange": "#f78c1b", | ||
"kiwi": "#9BD937", | ||
} | ||
|
||
labels = list(colorDict.keys()) | ||
leftLabels = [label for label in labels if label in df["true"].values] | ||
rightLabels = [label for label in labels if label in df["predicted"].values] | ||
|
||
ax = sankey( | ||
left=df["true"], | ||
right=df["predicted"], | ||
leftLabels=leftLabels, | ||
rightLabels=rightLabels, | ||
colorDict=colorDict, | ||
aspect=20, | ||
fontsize=12, | ||
) | ||
|
||
plt.savefig("img/fruits.png") | ||
plt.close() | ||
|
||
|
||
# This calculates how often the different combinations of "true" and | ||
# "predicted" co-occure | ||
df = df.groupby(["true", "predicted"]).size().reset_index() | ||
weights = df[0].astype(float) | ||
|
||
|
||
ax = sankey( | ||
left=df["true"], | ||
right=df["predicted"], | ||
rightWeight=weights, | ||
leftWeight=weights, | ||
leftLabels=leftLabels, | ||
rightLabels=rightLabels, | ||
colorDict=colorDict, | ||
aspect=20, | ||
fontsize=12, | ||
) | ||
|
||
|
||
plt.savefig("img/fruits_weighted.png") |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
name: build | ||
|
||
on: | ||
push: | ||
branches: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
# Jupyter | ||
.ipynb_checkpoints/ | ||
|
||
#Eclipse/pydev | ||
.project | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -4,14 +4,17 @@ Uses matplotlib to create simple <a href="https://en.wikipedia.org/wiki/Sankey_d | |||||||||||||||||||||
Sankey diagrams</a> flowing only from left to right. | ||||||||||||||||||||||
|
||||||||||||||||||||||
[![PyPI version](https://badge.fury.io/py/pySankeyBeta.svg)](https://badge.fury.io/py/pySankeyBeta) | ||||||||||||||||||||||
[![Build Status](https://travis-ci.org/Pierre-Sassoulas/pySankey.svg?branch=master)](https://travis-ci.org/Pierre-Sassoulas/pySankey) | ||||||||||||||||||||||
[![Build Status](https://github.com/Pierre-Sassoulas/pySankey/actions/workflows/ci.yaml/badge.svg)](https://github.com/Pierre-Sassoulas/pySankey/actions/workflows/ci.yaml) | ||||||||||||||||||||||
[![Coverage Status](https://coveralls.io/repos/github/Pierre-Sassoulas/pySankey/badge.svg?branch=master)](https://coveralls.io/github/Pierre-Sassoulas/pySankey?branch=master) | ||||||||||||||||||||||
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black) | ||||||||||||||||||||||
[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0) | ||||||||||||||||||||||
|
||||||||||||||||||||||
## Example | ||||||||||||||||||||||
## Examples | ||||||||||||||||||||||
|
||||||||||||||||||||||
With fruits.txt : | ||||||||||||||||||||||
### Simple expected/predicted example with fruits.txt: | ||||||||||||||||||||||
|
||||||||||||||||||||||
`pysankey` contains a simple expected/predicted dataset called `fruits.txt` which looks | ||||||||||||||||||||||
the following: | ||||||||||||||||||||||
|
||||||||||||||||||||||
<div> | ||||||||||||||||||||||
<table border="1" class="dataframe"> | ||||||||||||||||||||||
|
@@ -80,9 +83,13 @@ import pandas as pd | |||||||||||||||||||||
from pysankey import sankey | ||||||||||||||||||||||
import matplotlib.pyplot as plt | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
df = pd.read_csv( | ||||||||||||||||||||||
'pysankey/fruits.txt', sep=' ', names=['true', 'predicted'] | ||||||||||||||||||||||
'fruits.txt', | ||||||||||||||||||||||
sep=' ', | ||||||||||||||||||||||
names=['true', 'predicted'] | ||||||||||||||||||||||
) | ||||||||||||||||||||||
|
||||||||||||||||||||||
colorDict = { | ||||||||||||||||||||||
'apple':'#f71b1b', | ||||||||||||||||||||||
'blueberry':'#1b7ef7', | ||||||||||||||||||||||
|
@@ -92,83 +99,77 @@ colorDict = { | |||||||||||||||||||||
'kiwi':'#9BD937' | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
labels = list(colorDict.keys()) | ||||||||||||||||||||||
leftLabels = [label for label in labels if label in df['true'].values] | ||||||||||||||||||||||
rightLabels = [label for label in labels if label in df['predicted'].values] | ||||||||||||||||||||||
|
||||||||||||||||||||||
# Create the sankey diagram | ||||||||||||||||||||||
ax = sankey( | ||||||||||||||||||||||
df['true'], df['predicted'], aspect=20, colorDict=colorDict, | ||||||||||||||||||||||
leftLabels=['banana','orange','blueberry','apple','lime'], | ||||||||||||||||||||||
rightLabels=['orange','banana','blueberry','apple','lime','kiwi'], | ||||||||||||||||||||||
left=df['true'], | ||||||||||||||||||||||
right=df['predicted'], | ||||||||||||||||||||||
leftLabels=leftLabels, | ||||||||||||||||||||||
rightLabels=rightLabels, | ||||||||||||||||||||||
colorDict=colorDict, | ||||||||||||||||||||||
aspect=20, | ||||||||||||||||||||||
fontsize=12 | ||||||||||||||||||||||
) | ||||||||||||||||||||||
|
||||||||||||||||||||||
plt.show() # to display | ||||||||||||||||||||||
plt.savefig('fruit.png', bbox_inches='tight') # to save | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
|
||||||||||||||||||||||
![Fruity Alchemy](pysankey/fruit.png) | ||||||||||||||||||||||
![Fruity Alchemy](.github/img/fruits.png) | ||||||||||||||||||||||
|
||||||||||||||||||||||
You could also use weight: | ||||||||||||||||||||||
### Plotting preprocessed data using weights | ||||||||||||||||||||||
|
||||||||||||||||||||||
``` | ||||||||||||||||||||||
,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 | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
However, not always you have or can have the data available in the format mentioned in | ||||||||||||||||||||||
the previous example (e.g. if the dataset is too large). In this case, the weights | ||||||||||||||||||||||
between the true and predicted labels can also be calculated beforehand and used to | ||||||||||||||||||||||
create the sankey diagram. In this example, we continue to work with the data loaded | ||||||||||||||||||||||
already in the previous example: | ||||||||||||||||||||||
Comment on lines
+124
to
+128
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
```python | ||||||||||||||||||||||
import pandas as pd | ||||||||||||||||||||||
from pysankey import sankey | ||||||||||||||||||||||
import matplotlib.pyplot as plt | ||||||||||||||||||||||
|
||||||||||||||||||||||
df = pd.read_csv( | ||||||||||||||||||||||
'pysankey/customers-goods.csv', sep=',', | ||||||||||||||||||||||
names=['id', 'customer', 'good', 'revenue'] | ||||||||||||||||||||||
) | ||||||||||||||||||||||
weight = df['revenue'].values[1:].astype(float) | ||||||||||||||||||||||
# Calculate the weights from the fruits dataframe | ||||||||||||||||||||||
df = df.groupby(["true", "predicted"]).size().reset_index() | ||||||||||||||||||||||
weights = df[0].astype(float) | ||||||||||||||||||||||
|
||||||||||||||||||||||
ax = sankey( | ||||||||||||||||||||||
left=df['customer'].values[1:], right=df['good'].values[1:], | ||||||||||||||||||||||
rightWeight=weight, leftWeight=weight, aspect=20, fontsize=20 | ||||||||||||||||||||||
left=df['true'], | ||||||||||||||||||||||
right=df['predicted'], | ||||||||||||||||||||||
rightWeight=weights, | ||||||||||||||||||||||
leftWeight=weights, | ||||||||||||||||||||||
leftLabels=leftLabels, | ||||||||||||||||||||||
rightLabels=rightLabels, | ||||||||||||||||||||||
colorDict=colorDict, | ||||||||||||||||||||||
aspect=20, | ||||||||||||||||||||||
fontsize=12 | ||||||||||||||||||||||
) | ||||||||||||||||||||||
|
||||||||||||||||||||||
plt.show() # to display | ||||||||||||||||||||||
plt.savefig('customers-goods.png', bbox_inches='tight') # to save | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
|
||||||||||||||||||||||
![Customer goods](pysankey/customers-goods.png) | ||||||||||||||||||||||
|
||||||||||||||||||||||
Similar to seaborn, you can pass a matplotlib `Axes` to `sankey` function: | ||||||||||||||||||||||
|
||||||||||||||||||||||
```python | ||||||||||||||||||||||
import pandas as pd | ||||||||||||||||||||||
from pysankey import sankey | ||||||||||||||||||||||
import matplotlib.pyplot as plt | ||||||||||||||||||||||
|
||||||||||||||||||||||
df = pd.read_csv( | ||||||||||||||||||||||
'pysankey/fruits.txt', | ||||||||||||||||||||||
sep=' ', names=['true', 'predicted'] | ||||||||||||||||||||||
) | ||||||||||||||||||||||
colorDict = { | ||||||||||||||||||||||
'apple': '#f71b1b', | ||||||||||||||||||||||
'blueberry': '#1b7ef7', | ||||||||||||||||||||||
'banana': '#f3f71b', | ||||||||||||||||||||||
'lime': '#12e23f', | ||||||||||||||||||||||
'orange': '#f78c1b' | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
ax1 = plt.axes() | ||||||||||||||||||||||
|
||||||||||||||||||||||
sankey( | ||||||||||||||||||||||
df['true'], df['predicted'], aspect=20, colorDict=colorDict, | ||||||||||||||||||||||
fontsize=12, ax=ax1 | ||||||||||||||||||||||
) | ||||||||||||||||||||||
|
||||||||||||||||||||||
plt.show() | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
![Fruity Alchemy](.github/img/fruits_weighted.png) | ||||||||||||||||||||||
|
||||||||||||||||||||||
### pysankey function overview | ||||||||||||||||||||||
|
||||||||||||||||||||||
> `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)` | ||||||||||||||||||||||
> | ||||||||||||||||||||||
> **left**, **right** : NumPy array of object labels on the left and right of the | ||||||||||||||||||||||
> diagram | ||||||||||||||||||||||
> | ||||||||||||||||||||||
> **leftWeight**, **rightWeight** : Numpy arrays of the weights each strip | ||||||||||||||||||||||
> | ||||||||||||||||||||||
> **colorDict** : Dictionary of colors to use for each label | ||||||||||||||||||||||
> | ||||||||||||||||||||||
> **leftLabels**, **rightLabels** : order of the left and right labels in the diagram | ||||||||||||||||||||||
> | ||||||||||||||||||||||
> **aspect** : vertical extent of the diagram in units of horizontal extent | ||||||||||||||||||||||
> | ||||||||||||||||||||||
> **rightColor** : If true, each strip in the diagram will be be colored according to | ||||||||||||||||||||||
> its left label | ||||||||||||||||||||||
> | ||||||||||||||||||||||
> **fontsize** : Fontsize to be used for the labels | ||||||||||||||||||||||
> | ||||||||||||||||||||||
> **ax** : matplotlib axes to plot on, otherwise uses current axes. | ||||||||||||||||||||||
|
||||||||||||||||||||||
## Important informations | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.