Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show a legend in plotly (python) sankey?

I want to plot some sankey diagrams representing mass flows of different substances for a report. I want to put a legend to differentiate the substances but I don't know how to do it. I've tried showlegend without success

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 = ['household','industry','waste'],
      color = "blue"
    ),
    link = dict(
      source = [0,1,0,1], # indices correspond to labels, eg A1, A2, A2, B1, ...
      target = [2,2,2,2],
      value = [7190,2074,4483,74.50],
      label = ['aluminium','aluminium','copper','copper'],
      color = ['#d7d6d6','#d7d6d6','#f3cf07','#f3cf07']
  ))])
fig.update_layout(showlegend=True)
fig.show() 

enter image description here

like image 491
Nabla Avatar asked Oct 29 '25 12:10

Nabla


1 Answers

It seems that Plotly Sankey traces don't actual support legends, despite the docs currently seeming to indicate they do.

I've created a corresponding issue on the Plotly repo; looks like the resolution may be improving the docs to make this clearer.

like image 195
nedned Avatar answered Nov 01 '25 01:11

nedned



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!