'Is there a way to format plotly Sankey Diagram display?
I am creating a Sankey diagram with plotly as follows:
import plotly.graph_objects as go
fig = go.Figure(data=[go.Sankey(
valueformat = ".0f",
valuesuffix = " %",
orientation = "h",
node = dict(
pad = 20,
thickness = 20,
line = dict(color = "red", width = 1),
label = ['Equity',
'Global Equity',
'Tier 1',
'A looooooooooong',
'Tier 2',
'B looooooooooong',
'C looooooooooong',
'Tier 3',
'D looooooooooong',
'E looooooooooong',
'F looooooooooong',
'G looooooooooong',
'H looooooooooong'],
color = ['aqua',
'aqua',
'yellow',
'orange',
'yellow',
'orange',
'orange',
'yellow',
'orange',
'orange',
'orange',
'orange',
'orange'],
),
link = dict(
source = [0, 2, 1, 4, 4, 2, 7, 7, 7, 7, 7, 4],
target = [1, 3, 2, 5, 6, 4, 8, 9, 10, 11, 12, 7],
value = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
color = ['aqua',
'yellow',
'aqua',
'yellow',
'yellow',
'aqua',
'yellow',
'yellow',
'yellow',
'yellow',
'yellow',
'aqua'],
hovertemplate='This link has total value %{value}<extra></extra>'
))])
fig.update_layout(title_text="Waterfall Diagram",
font_size=16,
plot_bgcolor='white',
paper_bgcolor='white')
fig.show()
Output looks like this:
Is there a way:
- to make sure links in color aqua are always below the yellow ones? to visually separate them better - I am not sure why current set up show them in that order
- to give more space between the links, spreading them out more? Especially I would need links and nodes not to overlap each other
- spread out the aqua links even further? I.e. visually dissociate them from the others
- to control where and how node labels are shown? I.e. to the right or below node, and also controlling the font for each node
Solution 1:[1]
For 1, 2, & 3, you can set node locations explicitly as follows:
import plotly.graph_objects as go
fig = go.Figure(go.Sankey(
arrangement = "snap",
node = {
"label": ["A", "B", "C", "D", "E", "F"],
"x": [0.2, 0.1, 0.5, 0.7, 0.3, 0.5], #these are fractions of the domain (0,1)
"y": [0.7, 0.5, 0.2, 0.4, 0.2, 0.3],
'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]}))
fig.show()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Arthur Morris |