如何在只有 1 个中心节点的旭日图中正确使用“percentParent”?

所以我正在尝试构建一个 Plotly sunburst 图,percentParent为图中的每个元素显示。这适用于所有元素,除非我只有一个中央节点/环/其他选项(请参见下面的示例)

http://img3.mukewang.com/641ac3c90001f5db05560551.jpg

由于中心节点显然没有父节点,因此它似乎会出错并显示percentParent来自该texttemplate字段的括号内的调用。但是,如果有 2 个(或更多)中央节点,它会自动计算每个中央节点占两者总和的百分比。


我的问题是:当我只有 1 个中心节点时,我如何才能只为中心节点隐藏此字段或使其正确显示“100%”?


示例代码:


import pandas as pd

import plotly.graph_objects as go


df = pd.DataFrame({'node_names': ['Center', 'Yes', 'No'],

                   'node_parent': ['', 'Center', 'Center'],

                   'node_labels': ['Center', 'Center_Yes', 'Center_No'],

                   'node_counts': [1000, 701, 299]})



fig = go.Figure(

       data=go.Sunburst(

        ids=df["node_names"],

        labels=df["node_labels"], 

        parents=df["node_parent"],

        values=df["node_counts"],

        branchvalues="total",

        texttemplate = ('%{label}<br>%{percentParent:.1%}'),

    ),

)


fig.show()


慕后森
浏览 107回答 1
1回答

尚方宝剑之说

在这里我找到了阅读帮助的可能方法go.Sunburst.texttemplate?import pandas as pdimport plotly.graph_objects as godf = pd.DataFrame({'node_names': ['Center', 'Yes', 'No'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'node_parent': ['', 'Center', 'Center'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'node_labels': ['Center', 'Center_Yes', 'Center_No'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'node_counts': [1000, 701, 299]})fig=go.Figure(&nbsp; &nbsp; data=go.Sunburst(&nbsp; &nbsp; &nbsp; &nbsp; ids=df["node_names"],&nbsp; &nbsp; &nbsp; &nbsp; labels=df["node_labels"],&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; parents=df["node_parent"],&nbsp; &nbsp; &nbsp; &nbsp; values=df["node_counts"],&nbsp; &nbsp; &nbsp; &nbsp; branchvalues="total",&nbsp; &nbsp; &nbsp; &nbsp; texttemplate = ('%{label}',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '%{label}<br>%{percentParent:.1%}',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '%{label}<br>%{percentParent:.1%}',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '%{label}<br>%{percentParent:.1%}'),&nbsp; &nbsp; ),)fig.show()texttemplate您最终可以修改as中的第一个元素'%{label}<br>100%'。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python