3

I'm using plotly.graph_objects to build a Sankey diagram, and I'd like the target nodes to be the same color as the links that flow into them. An example of the code structure I have so far is:

label = ['A', 'B', 'Z', 'Y', 'X']
source = [0, 0, 0, 1, 1, 1]
target = [2, 3, 4, 2, 3, 4]
value = [100, 200, 300, 
400, 500, 600]
link = dict(source = source, target = target, value = value, color = color) #colors have been defined for links in a different cell)
node = dict(label = label, pad=35, thickness = 10)
data = go.Sankey(link = link, node = node)
fig=go.Figure(data)
fig.show()

This gets pretty close to what I want, but I need to be able to color the target nodes so they match the links.

Thanks in advance,

hbstha123
  • 1,260
  • 11
  • 23
griffm
  • 29
  • 2
  • In your example, your targets have multiple links flowing into them. Do you want the targets to have mixed colors? – Derek O Sep 10 '21 at 23:39
  • No, the targets should be one solid color. I've have built the diagram so that the links flowing from multiple source nodes to one target will all be the same color. For example, the links connecting both Source A and B to target Y are green, so target Y should also be green. – griffm Sep 11 '21 at 01:35

1 Answers1

0

You can specify the colors for both source and target nodes, and then pass them inside fig.update_traces as follows:

#Specify color for nodes 0, 1, 2, 3 and 4 in both source and target nodes
color_for_nodes = ["red","green","blue","violet","maroon"]
fig.update_traces(node_color = color_for_nodes)

Note that the color_for_nodes is different than the color you pass inside dictionary for link. You get something as shown. I have not used the color for links here though, but you can specify it on your own:enter image description here

hbstha123
  • 1,260
  • 11
  • 23