Python 如何为雷达或极坐标图中的不同范围指定背景色?
我试图以某种方式改变雷达图的背景色,不同的值范围得到不同的颜色,例如,雷达图的范围为1-5,其中1-3得到红色背景色,3-5得到绿色背景色。 可以更改颜色,但仅适用于整个圆圈 你有什么想法吗 编辑 这是我使用的示例代码,我发现它是唯一可以添加颜色的代码Python 如何为雷达或极坐标图中的不同范围指定背景色?,python,plotly,plotly-python,Python,Plotly,Plotly Python,我试图以某种方式改变雷达图的背景色,不同的值范围得到不同的颜色,例如,雷达图的范围为1-5,其中1-3得到红色背景色,3-5得到绿色背景色。 可以更改颜色,但仅适用于整个圆圈 你有什么想法吗 编辑 这是我使用的示例代码,我发现它是唯一可以添加颜色的代码 import plotly.graph_objects as go categories = ['processing cost','mechanical properties','chemical stability',
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()
跟踪的正确组合来实现:
代码:
# 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()
# 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()
编辑:多个记录道
您的示例代码包含多个跟踪。这将很快成为我最初的方法的一个烂摊子。下面是一个包含多个跟踪的片段。为了让它看起来更好,我将类别名称放在圆圈的边缘,在它自己的轨迹中,使用不同的文本位置,使名称落在圆圈的外侧。然后,我为每个产品a和B添加一个跟踪。我希望这会有用
绘图2:
# 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()
# 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()
代码2:
# 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()
# 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()
你能分享一下你的编码尝试吗?请花点时间阅读和分享。通过遵循这些文章的提示,您将获得更好的结果。我这里有一个基本的示例:我在更新版面中添加了背景色function@Bene_91您的代码段在
showlegdend=False
@vesland处引发了一个错误您是对的,它引发了一个错误。但是我从bgcolor=“red”
得到错误。我会更新上面的代码。谢谢你这个好主意!这就是我要找的。