Sankey diagram
A diagramok egyik kedvence, amely lehet bármilyen anyag/információ, vagy bármiféle tárgy vagy közeg áramlását úgy akarjuk ábrázolni úgy, hogy az egymásból történő következések, és az egymáshoz viszonyított arányok és eloszlások egyértelműek legyenek.
Az ábrán a két különálló elemet csomópontoknak nevezzük, és kapcsolatoknak a két különféle elem között összeköttetést ezt hivatkozásnak is nevezünk.
Alapvetően a Matplotlib és Plotly csomagok használata szükséges
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.sankey import Sankey
fig = plt.figure(figsize = (15,8))
ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[],
title="Sankey5")
#sankey = Sankey(ax=ax, unit=None)
sankey = Sankey(ax=ax, scale=2, offset=1, unit='%')
sankey.add(flows=[1.0, -0.3, -0.1, -0.1, -0.5],
# labels=['Lajos-1', 'Lajos-2', 'Lajos-3', 'Lajos-4', 'lajos-5'],
label='Lajos',
orientations=[0, -1, 1, 1, 0])
sankey.add(flows=[0.5, 0.1, 0.1, -0.1, -0.1, -0.1, -0.1, -0.3], fc='#37c959',
# labels=['Bela-1', 'Bela-2', 'Bela-3', 'Bela-4', 'Bela-5'],
label='Bela',
orientations=[0, -1, -1, 1, 1, -1, -1, 0], prior=0, connect=(4, 0))
sankey.add(flows=[-0.1, 0.1],
label='Jeno',
orientations=[-1,-1], prior=1, connect=(2, 0))
diagrams = sankey.finish()
diagrams[-1].patch.set_hatch('/')
plt.legend(loc='lower right')
plt.show()
import matplotlib.pyplot as plt
from matplotlib.sankey import Sankey
fig = plt.figure(figsize = (15,8))
ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[],
title="Sankey5")
#sankey = Sankey(ax=ax, unit=None)
sankey = Sankey(ax=ax, scale=2, offset=1, unit='%')
sankey.add(flows=[1.0, -0.3, -0.1, -0.1, -0.5],
# labels=['Lajos-1', 'Lajos-2', 'Lajos-3', 'Lajos-4', 'lajos-5'],
label='Lajos',
orientations=[0, -1, 1, 1, 0])
sankey.add(flows=[0.5, 0.1, 0.1, -0.1, -0.1, -0.1, -0.1, -0.3], fc='#37c959',
# labels=['Bela-1', 'Bela-2', 'Bela-3', 'Bela-4', 'Bela-5'],
label='Bela',
orientations=[0, -1, -1, 1, 1, -1, -1, 0], prior=0, connect=(4, 0))
sankey.add(flows=[-0.1, 0.1],
label='Jeno',
orientations=[-1,-1], prior=1, connect=(2, 0))
diagrams = sankey.finish()
diagrams[-1].patch.set_hatch('/')
plt.legend(loc='lower right')
plt.show()
import matplotlib.pyplot as plt
from matplotlib.sankey import Sankey
Sankey(flows=[0.25, 0.15, 0.60, ## +1.00
-0.20, -0.15, -0.15, -0.40, -0.10], ## -1.00
labels=['IN-1', 'IN-2', 'IN-3', 'OUT-1', 'OUT-2', 'OUT-3', 'OUT-4', 'OUT-5'],
orientations=[1, 1, 1, -1, -1, -1, -1, 0]).finish()
plt.title("Sankey0")
from matplotlib.sankey import Sankey
Sankey(flows=[0.25, 0.15, 0.60, ## +1.00
-0.20, -0.15, -0.15, -0.40, -0.10], ## -1.00
labels=['IN-1', 'IN-2', 'IN-3', 'OUT-1', 'OUT-2', 'OUT-3', 'OUT-4', 'OUT-5'],
orientations=[1, 1, 1, -1, -1, -1, -1, 0]).finish()
plt.title("Sankey0")
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.sankey import Sankey
fig = plt.figure(figsize = (15,8))
ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[], title="Sankey6")
flows = [0.25, 0.15, 0.60, -0.10, -0.05, -0.25, -0.15, -0.10, -0.35]
sankey = Sankey(ax=ax, unit=None)
sankey.add(flows=flows, label='Lajos',
orientations=[-1, 1, 0, 1, 1, 1, -1, -1, 0])
sankey.add(flows=[-0.25, 0.15, 0.1], label='Bela',
orientations=[-1, -1, -1], prior=0, connect=(0, 0))
diagrams = sankey.finish()
diagrams[-1].patch.set_hatch('/')
plt.legend()
plt.show()
import plotly.graph_objects as go
import urllib, json
url = 'https://raw.githubusercontent.com/plotly/plotly.js/master/test/image/mocks/sankey_energy.json'
response = urllib.request.urlopen(url)
data = json.loads(response.read())
opacity = 0.4
data['data'][0]['node']['color'] = ['rgba(255,0,255, 0.8)' if color == "magenta" else color for color in data['data'][0]['node']['color']]
data['data'][0]['link']['color'] = [data['data'][0]['node']['color'][src].replace("0.8", str(opacity))
for src in data['data'][0]['link']['source']]
fig = go.Figure(data=[go.Sankey(
valueformat = ".0f",
valuesuffix = "TWh",
# Define nodes
node = dict(
pad = 15,
thickness = 15,
line = dict(color = "black", width = 0.5),
label = data['data'][0]['node']['label'],
color = data['data'][0]['node']['color']
),
# Add links
link = dict(
source = data['data'][0]['link']['source'],
target = data['data'][0]['link']['target'],
value = data['data'][0]['link']['value'],
label = data['data'][0]['link']['label'],
color = data['data'][0]['link']['color']
))])
fig.update_layout(title_text="Sankey 9",
font_size=10)
fig.show()
Video link : https://youtu.be/tVkpwIbRh0Q
import pandas as pd
import numpy as np
import plotly.graph_objects as go
links = [
{'source': 'IN-1', 'target': 'IN-22', 'value': 1},
{'source': 'IN-1', 'target': 'IN-21', 'value': 2},
##------------
{'source': 'IN-22', 'target': 'IN-33', 'value': .3},
{'source': 'IN-22', 'target': 'IN-34', 'value': .3},
{'source': 'IN-22', 'target': 'IN-35', 'value': .3},
##------------
{'source': 'IN-21', 'target': 'IN-31', 'value': 1.5},
{'source': 'IN-21', 'target': 'IN-32', 'value':.5}
]
df = pd.DataFrame(links)
nodes = np.unique(df[["source","target"]], axis=None)
nodes = pd.Series(index=nodes, data=range(len(nodes)))
go.Figure(
go.Sankey(
orientation = 'v', ## orientáció v: vertikális /h: horizontalis
node={"label": nodes.index},
link={
"source": nodes.loc[df["source"]],
"target": nodes.loc[df["target"]],
"value": df["value"],
"color" : ["gold", "gold", "silver","gold", "gold", "silver","silver"],
},
)
)
# imports
import pandas as pd
import plotly.graph_objects as go
# to make notebook work offline
from plotly.offline import iplot, init_notebook_mode
init_notebook_mode(connected=True)
fig = go.Figure(go.Sankey(
arrangement = "snap",
orientation = 'v', ## orientáció v: vertikális /h: horizontalis
node = {
"label": ["A", "B", "C", "D", "E", "F"],
"x": [0.45, 0.35, 0.2, 0.4, 0.2, 0.3],
"y": [0.3, 0.2, 0.5, 0.7, 0.3, 0.5],
'pad':10}, # 10 Pixels
link = {
"source": [0, 0, 1, 2, 5, 4, 3, 5],
"target": [5, 3, 4, 3, 0, 2, 2, 3],
"value" : [1, 2, 1, 1, 1, 1, 1, 2],
"color" : ["gold", "gold", "silver", "silver", "silver", "orange", "yellow", "magenta"]}))
iplot(fig)
import pandas as pd
import plotly.graph_objects as go
# to make notebook work offline
from plotly.offline import iplot, init_notebook_mode
init_notebook_mode(connected=True)
fig = go.Figure(go.Sankey(
arrangement = "snap",
orientation = 'v', ## orientáció v: vertikális /h: horizontalis
node = {
"label": ["A", "B", "C", "D", "E", "F"],
"x": [0.45, 0.35, 0.2, 0.4, 0.2, 0.3],
"y": [0.3, 0.2, 0.5, 0.7, 0.3, 0.5],
'pad':10}, # 10 Pixels
link = {
"source": [0, 0, 1, 2, 5, 4, 3, 5],
"target": [5, 3, 4, 3, 0, 2, 2, 3],
"value" : [1, 2, 1, 1, 1, 1, 1, 2],
"color" : ["gold", "gold", "silver", "silver", "silver", "orange", "yellow", "magenta"]}))
iplot(fig)
Link:
https://github.com/klajosw/python/blob/master/kl_py_sankey_matplotlib.ipynb
Megjegyzések
Megjegyzés küldése