Arpan Tamrakar
Arpan Tamrakar

Reputation: 1

Sunburst plot using plotly, Font size of all the text labels are not same

I want to print the numbers outside the circle for each corresponding pie segment. All content should use the same font size, including on the smaller pie segments.

The text labels should be placed well inside the circle to ensure clear interpretation, and longer labels can be accommodated on two lines.

Here's my code along with the data.

import plotly.graph_objects as go

data = {
    "Straight 288": {
        "Towards traffic ahead 65": 65, 
        "Towards forward direction 223": 223
    },
    "SlowDown/Stop 147": {
        "Towards action inducing object 147": 147
    },
    "U-Turn 39": {
        "Towards right side mirror 19": 19,
        "On left changes to right 20": 20
    },
    "Right Turn 134": {
        "Towards front right side 84": 84,
        "Towards left side to right 21": 21,
        "Towards right side 29": 29
    },
    "Right LC 143": {
        "Towards right side mirror 117": 117,
        "Towards front right side 26": 26
    },
    "Left Turn 225": {
        "Towards front left side 151": 151,
        "Towards right & then left 64": 64,
        "To left side 10": 10
    },
    "Left LC 121": {
        "Towards left side mirror 96": 96,
        "Towards front left side 25": 25
    }
}

labels = []
parents = []
values = []
marker_colors = []
custom_text = []

category_colors = {
    "Straight 288": "#FF6F61",
    "SlowDown/Stop 147": "#6A0DAD",
    "U-Turn 39": "#FFA500",
    "Right Turn 134": "#1CA3EC",
    "Right LC 143": "#FF914D",
    "Left Turn 225": "#4CAF50",  
    "Left LC 121": "#007ACC"
}

for parent, children in data.items():
    total_value = sum(children.values())

    labels.append(parent)
    parents.append("")
    values.append(total_value)
    marker_colors.append(category_colors.get(parent, "#CCCCCC")) 
    custom_text.append(str(total_value)) 

    for child, value in children.items():
        if child == "Towards action inducing object 147":
          labels.append("Towards action<br>induce object 147")  
        else:
          labels.append(child)
        parents.append(parent)
        values.append(value)
        marker_colors.append(category_colors.get(parent, "#CCCCCC"))  
        custom_text.append(str(value))

# Create sunburst plot
fig = go.Figure(go.Sunburst(
    labels=labels,
    parents=parents,
    values=values,
    branchvalues="total",
    insidetextorientation='radial', 
    textinfo="label",
    marker=dict(colors=marker_colors, line=dict(color="black", width=1)), 
    textfont=dict(size=18, color=["black" if "Left Turn" in label else "black" for      label in labels]), 
))

# Update layout for better readability
fig.update_layout(
    margin=dict(t=10, l=10, r=10, b=10),
    uniformtext=dict(mode="show", minsize=17),  
    width=1000,  
    height=1000
)

fig.show()

Methods which like uniformtext = dict(fontsize=14, mode='show/hide') is not working!

Upvotes: 0

Views: 20

Answers (0)

Related Questions