plotly / plotly.py

The interactive graphing library for Python :sparkles: This project now includes Plotly Express!
https://plotly.com/python/
MIT License
15.82k stars 2.52k forks source link

Sankey plot #4122

Open MatteoSerafino opened 1 year ago

MatteoSerafino commented 1 year ago

Is there a way to display the values of the link as a fixed label?

At the moment, I can only visualize them when I go over them with the mouse, but I need the in the pdf version of the image.

AaronStiff commented 1 year ago

Hi @MatteoSerafino. Could you include a reproducible example of your code? That will help us to understand your question. Thanks!

MatteoSerafino commented 1 year ago

Hello @AaronStiff , sure.

`import plotly.graph_objects as go

nodes = ['A', 'B', 'C', 'D', 'E'] link_source = [0, 1, 2, 2, 3, 3] link_target = [2, 3, 4, 3, 4, 2] link_value = [8, 4, 2, 2, 2, 2] link_label = ['10%', '20%', '30%', '40%', '50%', '60%'] percentage_values = [10, 20, 30, 40, 50, 60] fixed_values = ['fixed1', 'fixed2', 'fixed3', 'fixed4', 'fixed5', 'fixed6']

trace = go.Sankey( node=dict(label=nodes), link=dict( source=link_source, target=link_target, value=link_value, customdata=list(zip(fixed_values, percentage_values)), hovertemplate='Link value: %{value}
Fixed value: %{customdata[0]}
Percentage: %{customdata[1]}%' ), textfont=dict(size=12), # Set the font size for the text field )

layout = go.Layout( hoverlabel=dict( bgcolor='white', font_size=12, font_family='Arial', bordercolor='gray', namelength=-1, align='left' # Use 'left' to align the hover label to the left of the mouse ) )

fig = go.Figure(data=[trace], layout=layout) fig.write_image('Figures/Na.pdf', engine="kaleido")

fig.show() `

This is the code I am using. It generates the file Prova. However, the values of the link are not present in the image.