Plotly treemap - How can I format as a percentage label and change the color of the root?

Hello Community,

I am having some issues formatting a plotly express treemap. Basically I just want to remove dark grey space and format the decimals as a percent.

See code:

fig = px.treemap(df, path=[df['Asset'],df['Price'], df['Change']],
                 color=df['Change'],
                 color_continuous_scale=colors
                 )
fig.update_layout(
    title=format_title("<b>Top Large-Cap Movers<b>", "Past 24 Hours"),
    #treemapcolorway = colors,
    margin=dict(l=40, r=40, t=90, b=60),
    autosize=False,
    hovermode = False,
    width=1000,
    height=600,
    font=dict(
    size=14,
    color="#120B2C", # Blockworks Darker Blue
    )
)
fig.show()

See image:

Thanks,

Hi @ailv36,
To change the root color perform this update:

fig.update_traces(root_color="your_preferred_color")

To get help on how to display decimals as percents, print(fig.data[0]), and identify the key whose value is the list of values to witch you want to add % as suffix.

Example:

import plotly.express as px
df = px.data.tips()
fig = px.treemap(df, path=['day', 'time', 'tip'],
                 values='total_bill')
fig.show()

Let us say we want to append a % to numerical labels.

print(fig.data[0].labels)

We are noticing that excepting the last 10 labels all are numerical. Hence run these lines:

old_labels=fig.data[0].labels
new_labels=[f"{ol}%"  for ol  in old_labels[:-10]]
new_labels.extend(old_labels[-10:])
fig.update_traces(labels=new_labels)
1 Like

Thank you so much!