Plotly雷达图使用数组值无法显示问题咨询
解决Plotly雷达图无法显示numpy数组数值的问题
这问题我之前调试雷达图时也踩过坑!核心原因是你传给r参数的列表里混了numpy单元素数组(比如aa是array([0.38570075]))和普通数值(ee=0),Plotly没法直接解析这些数组对象,得把它们转换成单个的数值才行。
下面给你两种简单的解决方法:
方法1:用索引提取数组元素
因为你的每个数组都只有一个元素,直接用[0]索引取出里面的数值就行:
import plotly.graph_objects as go import numpy as np # 假设你之前导入了numpy,不然不会生成array类型 categories = ['A', 'B', 'C', 'D', 'E'] fig = go.Figure() fig.add_trace(go.Scatterpolar( r=[aa[0], bb[0], cc[0], dd[0], ee], # 给数组加[0]提取单个数值 theta=categories, fill="toself", name="Egress & Access" )) fig.update_layout( polar=dict( radialaxis=dict( visible=True, range=[0, 1] )), showlegend=True ) fig.show()
方法2:用numpy数组的.item()方法
这是更稳妥的方式,不管数组是整数还是浮点数类型,.item()都能把单元素数组转换成Python原生数值:
import plotly.graph_objects as go import numpy as np categories = ['A', 'B', 'C', 'D', 'E'] fig = go.Figure() fig.add_trace(go.Scatterpolar( r=[aa.item(), bb.item(), cc.item(), dd.item(), ee], # 用.item()提取数值 theta=categories, fill="toself", name="Egress & Access" )) fig.update_layout( polar=dict( radialaxis=dict( visible=True, range=[0, 1] )), showlegend=True ) fig.show()
额外说明
ee=0没问题是因为它本身就是Python的整数类型,不需要转换;- 如果以后你的数组有多个元素,要确保
r列表的长度和categories的长度完全匹配,不然雷达图还是会出问题。
内容的提问来源于stack exchange,提问作者ROAR.L
相关产品推荐
相关产品推荐

