0

I am plotting sankey plots in Python using plotly, based on a sample script:

import plotly.graph_objects as go

fig = go.Figure(data=[go.Sankey(
    node = dict(
      pad = 15,
      thickness = 20,
      line = dict(color = "black", width = 0.5),
      label = ["A1", "A2", "B1", "B2", "C1", "C2"],
      customdata = ["Long name A1", "Long name A2", "Long name B1", "Long name B2",
                    "Long name C1", "Long name C2"],
      hovertemplate='Node %{customdata} has total value %{value}<extra></extra>',
      color = "blue"
    ),
    link = dict(
      source = [0, 1, 0, 2, 3, 3], # indices correspond to labels, eg A1, A2, A2, B1, ...
      target = [2, 3, 3, 4, 4, 5],
      value = [8, 4, 2, 8, 4, 2],
      customdata = ["q","r","s","t","u","v"],
      hovertemplate='Link from node %{source.customdata}<br />'+
        'to node%{target.customdata}<br />has value %{value}'+
        '<br />and data %{customdata}<extra></extra>',
  ))])

fig.update_layout(title_text="Basic Sankey Diagram", font_size=10)
fig.show()

I was wondering if there is a way to modify the way the links are plotted so that they could have different patterns to distinguish path 1 (A1 to B1 to C1) and 2 (A2 to B2 to C2)- Path 2 could be dotted, rather than solid. Is this possible?

Francesca
  • 63
  • 1
  • 7

1 Answers1

0
  • you can set the colors of the links
  • quite a bit of syntax sugar to achieve shade of grey is darker for lower values of last character of node label
import plotly.graph_objects as go

fig = go.Figure(
    data=[
        go.Sankey(
            node=dict(
                pad=15,
                thickness=20,
                line=dict(color="black", width=0.5),
                label=["A1", "A2", "B1", "B2", "C1", "C2"],
                customdata=[
                    "Long name A1",
                    "Long name A2",
                    "Long name B1",
                    "Long name B2",
                    "Long name C1",
                    "Long name C2",
                ],
                hovertemplate="Node %{customdata} has total value %{value}<extra></extra>",
                color="blue",
            ),
            link=dict(
                source=[
                    0,
                    1,
                    0,
                    2,
                    3,
                    3,
                ],  # indices correspond to labels, eg A1, A2, A2, B1, ...
                target=[2, 3, 3, 4, 4, 5],
                value=[8, 4, 2, 8, 4, 2],
                # color="red",
                customdata=["q", "r", "s", "t", "u", "v"],
                hovertemplate="Link from node %{source.customdata}<br />"
                + "to node%{target.customdata}<br />has value %{value}"
                + "<br />and data %{customdata}<extra></extra>",
            ),
        )
    ]
)

fig.update_layout(title_text="Basic Sankey Diagram", font_size=10)
fig.data[0].update(
    {
        "link": {
            "color": [
                f"rgba({c},{c},{c},.8)"
                for s, t in zip(
                    fig.data[0]["link"]["source"], fig.data[0]["link"]["target"]
                )
                if (
                    c := min(
                        int(fig.data[0]["node"]["label"][s][-1]),
                        int(fig.data[0]["node"]["label"][t][-1]),
                    )
                    * 30
                    + 150
                )
                is not None
            ]
        }
    }
)

fig

enter image description here

Rob Raymond
  • 29,118
  • 3
  • 14
  • 30