Hiding % labels in px.pie chart python

basically the same question like here, but it is for javascript

when i try textinfo None as recommended there, i get a key error

how do i hide it?

ok, i have found it. it just needs to be fig_pie.update_traces(textinfo='none'), but it hides all. now i wonder if there is a way to CODITIONALY hide the % labels.

lets say i have a pie chart like this
pie hide
and i would like to hide all that does not fit into the the pie, or i want to leave only the first ten biggest… or something like that. just whatever based on a contiditon.

how would i do that?

2 Likes

Hi @nirvikalpa, you can impose that the text fits inside pie sectors with textposition, and then impose a minimum font size so that labels which don’t fit won’t be displayed. This is explained in the documentation example on pie charts and uniformtext

import plotly.express as px
df = px.data.gapminder().query("continent == 'Asia'")
fig = px.pie(df, values='pop', names='country')
fig.update_traces(textposition='inside')
fig.update_layout(uniformtext_minsize=12, uniformtext_mode='hide')
fig.show()
5 Likes

pretty neat trick, thank you

Sorry to get back to this but in

fig_pie = px.pie(df,names="Tipificação Incidente ", values="Index", hole=0.5, color_discrete_sequence=px.colors.qualitative.Vivid)

fig_pie.update_layout(showlegend=False)

fig_pie.update_traces(textposition='inside')

fig_pie.update_layout(uniformtext_minsize=12, uniformtext_mode='hide')

returns

ValueError: Invalid properties specified for object of type plotly.graph_objs.Layout: ('uniformtext_minsize', 'uniformtext_mode')

Any idea of what I’m doing wrong?

Thank you in advance!

This worked for me.

# fig is a variable containing your px.pie() chart
fig.update_traces(textinfo='none')
2 Likes

@pbadeer welcome to the community and indeed, that works!
Thank you!