[PYTHON] Even beginners can do it! An easy way to write a Sankey Diagram on Plotly

What is SankyDiagram

The Sankey diagram (English Sankey diagram) is a diagram that expresses the flow rate between processes. The thickness of the arrow indicates the amount of flow. In particular, it is used to represent the displacement of energy, supplies, expenses, etc. Source: [Wikipedia](https://en.wikipedia.org/wiki/%E3%82%B5%E3%83%B3%E3%82%AD%E3%83%BC_%E3%83%80%E3 % 82% A4% E3% 82% A2% E3% 82% B0% E3% 83% A9% E3% 83% A0)

Benefits of implementing with plotly (Plotly Express)

Implementation of Sankey Diagram on Plotly

import pandas as pd
import plotly.express as px

#DF generation
#Create a data frame so that one row of data represents a transition
df =pd.DataFrame([['Cat1', 'CatA-2', 'AI', 'Normal'],
                  ['Cat1', 'CatA-3', 'AI', 'Normal'],
                  ['Cat1', 'CatA-3', 'No', 'NG'],
                  ['Cat3', 'CatA-3', 'Random', 'Normal'],
                  ['Cat3', 'CatA-5', 'Random', 'NG'],
                  ['Cat3', 'CatA-1', 'Random', 'NG'],
                  ['Cat3', 'CatA-1', 'No', 'NG']],
                columns=['one','two','three',"output"])

#Categorical variables as dummy variables
#Error if the last column of the data frame is a string?
df["output"] = pd.get_dummies(df["output"])

fig = px.parallel_categories(df, 
                             dimensions=['one','two','three','output'],
                             color="output",
                             color_continuous_scale=px.colors.diverging.BrBG,
                             labels={'one':'FirstArea', 'two':'SecondArea', 'three':'ThirdArea','output':'output'}
                            )
fig.show()

newplot (69).png

Recommended Posts

Even beginners can do it! An easy way to write a Sankey Diagram on Plotly
A relatively easy way to insert a NEologd dictionary on Windows-System Dictionary
An easy way to view the time taken in Python and a smarter way to improve it
How easy is it to synthesize a drug on the market?
An easy way to re-execute a previously executed command in ipython
[Python] Create a linebot to write a name and age on an image
Even a new graduate engineer can do it! The trick to make a difference in synchronization by commenting out
I want to write an element to a file with numpy and check it.
[Python] Concatenate a List containing numbers and write it to an output file.
Searching for an efficient way to write a Dockerfile in Python with poetry
Easy way to use Python 2.7 on Cent OS 6
Easy way to plot HR diagram using astroquery
Easy way to load CPU / memory on Linux
An easy way to call Java from Python
Automatic browser operation that even beginners can do
An easy way to measure the processing speed of a disk recognized by Linux
An easy way to pad the number with zeros depending on the number of digits [Python]
You can do it in 3 minutes! How to make a moving QR code (GIF)!