[Python] How to remove glow on node labels in Plotly Sankey diagram in Streamlit?

Discussão em 'Python' iniciado por Stack, Maio 13, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to get rid of the "glow" or shadow effect around the node labels of this Plotly Sankey diagram. Is this possible?

    import pandas as pd
    import plotly.graph_objects as go
    import streamlit as st

    # Data
    data = {
    "source": ["Total Orders", "Total Orders", "Total Orders"],
    "target": ["Kit", "Other", "Rx"],
    "value": [138000, 34500, 28000]
    }

    # Create Pandas DataFrame
    sankey_table = pd.DataFrame(data)

    # Create a list of unique labels
    labels = pd.concat([sankey_table["source"], sankey_table["target"]]).unique().tolist()

    # Create a dictionary to map labels to indices
    label_indices = {label: idx for idx, label in enumerate(labels)}

    # Apply mapping to source and target columns
    sankey_table["source_index"] = sankey_table["source"].apply(lambda x: label_indices[x])
    sankey_table["target_index"] = sankey_table["target"].apply(lambda x: label_indices[x])

    # Extract data for the Sankey diagram
    source_indices = sankey_table["source_index"].tolist()
    target_indices = sankey_table["target_index"].tolist()
    values = sankey_table["value"].tolist()

    # set colors
    node_colors = ["grey", "#213A51", "#32526E", "#BEA069"]
    link_colors = ["rgba(33, 58, 81, 0.7)", "rgba(50, 82, 110, 0.7)", "rgba(190, 160, 105, 0.7)"] # Colors for the links from 'Total Orders' to 'Kit', 'Other', 'Rx'

    # Create Sankey diagram
    sankey_fig = go.Figure(data=[go.Sankey(
    node=dict(
    pad=15,
    thickness=20,
    line=dict(color="black", width=0.5),
    label=labels,
    color=node_colors
    ),
    link=dict(
    source=source_indices,
    target=target_indices,
    value=values,
    color=link_colors
    )
    )])

    sankey_fig.update_layout(
    title_text="Product Subtotals Sankey Diagram",
    font=dict(
    family="Courier New",
    size=14,
    color="black"
    )
    )

    st.plotly_chart(sankey_fig)


    So far I've tried using:

    • update_layout to change font family, color, and size
    • CSS to manually change the font style (this didn't apply directly since I'm working within Plotly and Streamlit, and custom CSS wasn't easy to inject in this case)
    • node and link options by specifying settings inside the go.Sankey trace

    Comments from this post suggest using dark mode, but I can't do that because that wouldn't match my client's required aesthetic/branding.

    Here's a picture of what I currently have. The font coloring and glow make it difficult to read the labels.

    [​IMG]

    Continue reading...

Compartilhe esta Página