Plotly:如何将背景颜色分配给雷达或极坐标图中的不同范围?

Plotly: How to assign background colors to different ranges in a radar or polar chart?

我正在尝试以某种方式更改雷达图的背景颜色,使不同的值范围获得不同的颜色,例如范围为 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()

没有直接的方法可以为绘图的不同部分指定不同的背景颜色。但是如果我正确理解你在这里的目标,你可以通过一些 go.Barpolar()go.Scatterpolar() traces 的正确组合来做到这一点:

代码:

# 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(255, 0, 0, 0.8)', 'rgba(0, 255, 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()

编辑:多条轨迹

您的示例代码包含多个跟踪。这很快就会让我最初的方法变得一团糟。下面是一个涵盖多个跟踪的片段。为了让它看起来不错,我将类别名称放在圆圈的边缘,在它自己的轨迹中使用不同的文本位置,使名称落在圆圈的外面。然后我为每个产品 A 和 B 添加一个跟踪。我希望这会有用。

情节 2:

代码2:

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

# 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]

rAllMax = max(rVars1+rVars2)

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

# 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

# set up label positions
df_theta=pd.DataFrame({'theta':thetas, 'positions':['middle right', 'middle right',
                                                    'bottom center', 'middle left',
                                                    'middle left', 'middle left']})


# 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

for r, cat in enumerate(categories):
    #print(r, cat)
    fig.add_trace(go.Scatterpolar(
            text = cat,
            r = [rAllMax],
            theta = [thetas[r]],
            mode = 'lines+text+markers',
            fill='toself',
            fillcolor='rgba(255, 255, 255, 0.4)',
            line = dict(color='black'),
            #textposition='bottom center',
            textposition=df_theta[df_theta['theta']==thetas[r]]['positions'].values[0],
            marker = dict(line_color='white', color = 'black'),
            marker_symbol ='circle',
            name = cat,
            showlegend = False))



# 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'),
        marker_symbol ='square',
        name = 'Product A'))

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


# 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()