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,
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With