Python 在甜甜圈图表中隐藏低百分比

Python 在甜甜圈图表中隐藏低百分比,python,label,plotly,donut-chart,Python,Label,Plotly,Donut Chart,我有一个甜甜圈图表,其中有一些类的百分比值很低。我想从甜甜圈图表中隐藏这些标签。用这种方式,作为默认的绘图,我认为它们是混乱的 我的代码: 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", "a

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

我的代码:

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)
我们可以在数据中添加属性“textposition”,它同时接受字符串和数组。当“textposition”为“auto”时,标签显示为正常,当“none”时,标签隐藏

{
      "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)
参考:

事先对您的数据进行迭代,并将小于(x%)的数据段分组到“其他”类别中?感谢您的帮助。在我的脚本中有一些问题。我把列表(地图…)放进去,效果很好。
"textposition": calculatetextpositions(dado_app_nascente),