雷达图中的不同背景色

问题描述 投票:2回答:1

我正在尝试以某种方式更改雷达图的背景颜色,即不同的值范围将获得不同的颜色,例如范围为1-5的雷达图,其中1-3为红色背景色,而3-5为绿色背景色。可以更改颜色,但只能更改整个圆圈。

你有什么想法吗?

编辑

这是示例代码,我发现添加颜色的唯一可能性。

import plotly.graph_objects as go

categories = ['processing cost','mechanical properties','chemical stability',
              'thermal stability', 'device integration']

fig = go.Figure()

fig.add_trace(go.Scatterpolar(
      r=[1, 5, 2, 2, 3],
      theta=categories,
      fill='toself',
      name='Product A'
))
fig.add_trace(go.Scatterpolar(
      r=[4, 3, 2.5, 1, 2],
      theta=categories,
      fill='toself',
      name='Product B'
))

fig.update_layout(
  paper_bgcolor="red",
  polar=dict(
    radialaxis=dict(
      color="red",
      visible=True,
      range=[0, 5]
    )),
  showlegend=False
)

fig.show()
python plotly plotly-python
1个回答
0
投票

没有直接的方法可以为图表的不同部分指定不同的背景颜色。但是,如果我正确理解了您的目标,则可以使用go.Barpolar()go.Scatterpolar()迹线的正确组合来做到这一点:

enter image description here

代码:

# imports
import plotly.graph_objects as go
import numpy as np

# categories:
categories = ['processing cost','mechanical properties','chemical stability',
              'thermal stability', 'device integration']

# values:
rVars1=[1, 5, 2, 2, 3]
rVars2=[4, 3, 2.5, 1, 2]

# colors
values = [3,5]
colors = ['rgba(0, 255, 0, 0.8)', 'rgba(255, 0, 0, 0.8)']

# some calcultations to place all elements
slices=len(rVars1)
fields=[max(rVars1)]*slices
circle_split = [360/slices]*(slices)
theta= 0
thetas=[0]
for t in circle_split:
    theta=theta+t
    thetas.append(theta)
thetas

# plotly 
fig = go.Figure()

# "background"
for t in range(0, len(colors)):
    fig.add_trace(go.Barpolar(
        r=[values[t]],
        width=360,
        marker_color=[colors[t]],
        opacity=0.6,
        name = 'Range ' + str(t+1)
        #showlegend=False,
    ))
    t=t+1

# trace 1
fig.add_trace(go.Scatterpolar(
       text = categories,
        r = rVars1,
        mode = 'lines+text+markers',
        fill='toself',
        fillcolor='rgba(0, 0, 255, 0.4)',
        textposition='bottom center',
        marker = dict(color = 'blue'),
        name = 'Product A'))

# adjust layout
fig.update_layout(
    template=None,
    polar = dict(radialaxis = dict(gridwidth=0.5,
                               range=[0, max(fields)], 
                              showticklabels=True, ticks='', gridcolor = "grey"),
                 angularaxis = dict(showticklabels=False, ticks='',
                               rotation=45,
                               direction = "clockwise",
                               gridcolor = "white")))

fig.update_yaxes(showline=True, linewidth=2, linecolor='white')
fig.show()
© www.soinside.com 2019 - 2024. All rights reserved.