0

can i get the code for plotly sankey diagram to move individual names of both source and target nodes out of the image. eg. 'VU', 'CE' etc in the target nodes. the data is

data = [{'source': 'ANACANTHOBATIDAE', 'target': 'LC', 'value': 9},
    {'source': 'ARHYNCHOBATIDAE', 'target': 'CR', 'value': 2},
    {'source': 'ARHYNCHOBATIDAE', 'target': 'DD', 'value': 1},
    {'source': 'ARHYNCHOBATIDAE', 'target': 'EN', 'value': 3}...108 more rows]

the code is

import plotly.graph_objects as go

# Extract unique source and target nodes
nodes = list(set([entry['source'] for entry in data] + [entry['target'] for entry in data]))

# Create source, target, and value lists
sources = [nodes.index(entry['source']) for entry in data]
targets = [nodes.index(entry['target']) for entry in data]
values = [entry['value'] for entry in data]

# Create color list
colors = [entry['color'] for entry in data]

# Create a Sankey trace
sankey_trace = go.Sankey(
    node=dict(
        pad=10,
        thickness=10,
        line=dict(color="black", width=0.5),
        label=nodes,
        color=colors,
        x=[0.9],  # Position labels outside
    ),
    link=dict(
        source=sources,
        target=targets,
        value=values,
        color=colors
    ),
    arrangement="snap",
)

# Create layout
layout = go.Layout(
    title_text="Sankey Diagram with Colored Links",
    font_size=10,
)

# Create figure
fig = go.Figure(data=[sankey_trace], layout=layout)

    
# Set plot layout
fig.update_layout(
    title_text="Sankey Diagram",
    font=dict(size=12),
    font_family="Courier New",
    font_color="red",
    title_font_family="Times New Roman",
    title_font_color="black",
    width=1000,  # Update the width of the plot
    height=900  # Update the height of the plot
)

# Show plot[enter image description here][1]
fig.show()

and got the plot [1]: https://i.stack.imgur.com/Kdetv.png

how can i shift source code names to the outer part of the image. I got codes to move labels but not for individual node names.

Sajna v.h
  • 1
  • 1

0 Answers0