2017-03-02 72 views
0

我有一个甜甜圈图表,有低百分比值的类。我想从甜甜圈图表中隐藏这些标签。用这种方式,作为默认情节,我认为他们很混乱。在甜甜圈图中隐藏低百分比

我的代码:

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) 

Donut Chart with

+1

迭代您的数据事先并将小于(x%)的分组分组为“其他”类别RY? – Aaron

回答

0

我们可以添加属性 “textposition” 的数据,它接受字符串和数组。当“textposition”为“auto”时,标签显示为正常,当“无”时,标签隐藏。

{ 
     "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'] 
    }, 

而且我们可以通过计算前手的比例生成textposition阵列:

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) 

就用

"textposition": calculatetextpositions(dado_app_nascente), 

参考: https://plot.ly/python/reference/#pie-textposition

+0

感谢您的帮助。在我的脚本中有一些问题。我把名单(地图...),它运作良好。 – dogosousa