go.Scatterpolar雷达图多线条颜色不生效问题求助
Plotly Scatterpolar雷达图线条颜色不显示问题解决
问题描述
使用Plotly的go.Scatterpolar组件绘制每条线条颜色不同的雷达图,按文档操作后无报错,但线条颜色未正常显示;hover每条线条时,能正常显示对应颜色和数值。
测试数据
r = [52,36,85] theta = ["analytique", "analogique", "affectif"] colors = ["blue", "red","yellow"]
原问题代码
for i in range(len(theta)): fig_reception.add_trace(go.Scatterpolar( mode='lines+text', theta=[theta[i]], r=[r[i]], , line_color=text_colors[i], fillcolor='#d3d3d3', marker=dict(color=text_colors), )) fig_reception.update_layout(autosize=False, height=305, polar=dict(radialaxis = dict(range=[0,100],visible = False), angularaxis=dict(rotation=180,direction="clockwise") ) ) fig_reception.update_layout( template=None, polar = dict(bgcolor = "rgba(255, 255, 255, 0.2)"),) fig_reception.update_layout( font=dict( size=16, color="black", family="Courier New, monospace", ), title="Réception", title_font_family="Courier New, monospace", showlegend=False )
问题分析
- 单数据点无法形成线条:每个
Scatterpolartrace仅传入单个theta和r值,mode='lines+text'下单个点无法渲染出可见线条,自然看不到线条颜色。 - 变量名不一致:代码中使用
text_colors但定义的变量是colors,存在变量名错误。 - marker颜色设置错误:
marker=dict(color=text_colors)传入了整个颜色列表,而非当前trace对应的单个颜色。
修正后的代码
import plotly.graph_objects as go fig_reception = go.Figure() r = [52,36,85] theta = ["analytique", "analogique", "affectif"] colors = ["blue", "red","yellow"] for i in range(len(theta)): fig_reception.add_trace(go.Scatterpolar( mode='lines+markers+text', theta=[theta[i], theta[i]], # 重复theta,形成从中心到目标点的线条 r=[0, r[i]], # 从径向轴0点出发到目标值 line_color=colors[i], line_width=3, # 增加线条宽度更易观察 marker=dict(color=colors[i], size=8), text=[f"{r[i]}"], # 显示数值文本 textposition="middle right" )) fig_reception.update_layout(autosize=False, height=305, polar=dict(radialaxis = dict(range=[0,100],visible = False), angularaxis=dict(rotation=180,direction="clockwise") ) ) fig_reception.update_layout( template=None, polar = dict(bgcolor = "rgba(255, 255, 255, 0.2)"),) fig_reception.update_layout( font=dict( size=16, color="black", family="Courier New, monospace", ), title="Réception", title_font_family="Courier New, monospace", showlegend=False ) fig_reception.show()
效果说明
修正后每个trace会绘制一条从雷达图中心到对应角度目标值的线条,线条颜色按colors列表正常显示,hover时也能看到对应颜色和数值。

内容的提问来源于stack exchange,提问作者Murcielago
相关产品推荐
相关产品推荐

