I have one donut chart that there are classes with low values in percent. I would like to hide these label of them from donut chart. With this way, as default plot, I think that they are messy.
My code:
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode()
#Dados (values)
dado_app_nascente = groupped_app_veg.loc[groupped_app_veg["app"] == "Entorno nascente", "area_ha"]
dado_app_rio = groupped_app_veg.loc[groupped_app_veg["app"] == "Margem de curso d'água", "area_ha"]
#Legenda ("labels")
label_app_nascente = groupped_app_veg.loc[groupped_app_veg["app"] == "Entorno Nascente", "class_name"].str.title()
label_app_rio = groupped_app_veg.loc[groupped_app_veg["app"] == "Margem de curso d'água", "class_name"].str.title()
#Cores ("marker": {"color"})
c_app_nascente = list(groupped_app_veg.loc[groupped_app_veg["app"] == "Entorno nascente", "color"])
c_app_rio = list(groupped_app_veg.loc[groupped_app_veg["app"] == "Margem de curso d'água", "color"])
fig = {
"data": [
{
"values": dado_app_nascente,
"labels": label_app_nascente,
"domain": {"x": [0, .48]},
"name": "area_entorno_nascente",
#"hoverinfo":"label+percent+name",
"textposition":"inside",
"hole": .4,
"type": "pie",
"marker": {'colors': c_app_nascente},
"showlegend": False
},
{
"values":dado_app_rio,
"labels": label_app_rio,
"textposition":"inside",
"domain": {"x": [.52, 1]},
"name": "area_margem_rio",
#"hoverinfo":"label+percent+name",
"hole": .4,
"type": "pie",
"marker": {'colors': c_app_rio},
}],
"layout": {
"title":"PROPORÇÃO DE CLASSES: VEGETAÇÃO/USO DO SOLO",
"width": 1000,
"height": 500,
"annotations": [
{
"font": {
"size": 18
},
"showarrow": False,
"text": "NASCENTE",
"x": 0.175,
"y": 0.5
},
{
"font": {
"size": 18
},
"showarrow": False,
"text": "RIO",
"x": 0.785,
"y": 0.5
},
{
"font": {
"size": 14
},
"showarrow": False,
"text": "Área Preservação Permanente",
"x": 0.555,
"y": 1.135
},
]
}
}
plotly.offline.iplot(fig)
We can add attribute "textposition" to the data, it accepts both string and array. When "textposition" is "auto", the label displays as normal, and when it's "none", the label hides.
{
"values": dado_app_nascente,
"labels": label_app_nascente,
"domain": {"x": [0, .48]},
"name": "area_entorno_nascente",
"textposition":"inside",
"hole": .4,
"type": "pie",
"marker": {'colors': c_app_nascente},
"showlegend": False,
# vvv Here vvv
"textposition": ['auto','auto','auto','auto','none','none','none']
},
And we can generate the textposition array by calculating the percentage before hand:
def calculateTextpositions(values):
total = sum(values)
# Do not display percentages < 5%
return map(lambda v: 'none' if float(v)/total < 0.05 else 'auto', values)
Then just use
"textposition": calculatetextpositions(dado_app_nascente),
Reference: https://plot.ly/python/reference/#pie-textposition