2017-03-02 71 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