如何让Python Plotly.Express旭日图默认选中指定节点?
在Plotly Express旭日图中默认选中指定节点(Seth)
要实现默认选中Seth节点的效果,你可以通过修改旭日图的selectedpoints属性指定初始选中的节点索引,具体实现如下:
修改后的完整代码
import plotly.express as px from plotly.offline import plot data = dict( character=["Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"], parent=["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve" ], value=[10, 14, 12, 10, 2, 6, 6, 4, 4]) fig = px.sunburst( data, names='character', parents='parent', values='value' ) # 获取Seth在character列表中的索引 seth_index = data['character'].index('Seth') # 设置初始选中该节点 fig.update_traces(selectedpoints=[seth_index], selector=dict(type='sunburst')) plot(fig)
关键说明
selectedpoints参数接受索引列表,传入单个索引即可实现单选效果,若需多选可传入多个节点索引- 该方法利用Plotly图表的交互选中机制,提前设置初始选中状态,无需额外配置复杂参数
内容的提问来源于stack exchange,提问作者Alex
相关产品推荐
相关产品推荐

