plotly:更改极坐标图区域的背景颜色

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

有一个关于如何为极坐标图的着色的问题。我想根据类别添加不同的背景颜色。

考虑一个技能评级,我想在总体概述中强调技能的种类

import plotly.graph_objects as go

categories = [
    "[lang] English", "[lang] Spanish", "[lang] German",
    "[dev] JS", "[dev] Python", "[dev] Go", "[dev] C#",
    "[skill] leadership", "[skill] creativity"
]
# remove prefix, this should be reflected by the background color
categories = [c.split("] ")[1] for c in categories]

ratings = {
    "Alice": [4,5,0,3,4,5,2,3,5],
    "Bob":   [3,1,4,2,3,1,1,3,2],
}

fig = go.Figure()

for name in ratings.keys():
    fig.add_trace(go.Scatterpolar(
        r=ratings[name] + [ratings[name][0]],
        theta=categories + [categories[0]],
        name=name,
    ))


fig.show()

是否可以通过更改背景颜色来突出显示类别的kind,使其看起来与此(伪造的)示例类似?

我在网上搜索并寻找不同的选项,但我发现的只是我链接的另一个问题。

python plotly plotly-python
1个回答
0
投票

您可以通过向当前图形添加

go.Barpolar
迹线来实现此目的,其中类别沿角轴放置,方式与 Scatterpolar 迹线相同,并且在径向轴上的幅度与所有条形,即
ratings
的最大值(以便彩色区域均匀地分布在极坐标图上)。使用
marker_color
属性指定每个类别切片的颜色。

categories = [
    "[lang] English", "[lang] Spanish", "[lang] German",
    "[dev] JS", "[dev] Python", "[dev] Go", "[dev] C#",
    "[skill] leadership", "[skill] creativity"
]

kinds = [re.search('^\[(.+)\]', c).group(1) for c in categories]
categories = [c.split("] ")[1] for c in categories]

ratings = {
    "Alice": [4,5,0,3,4,5,2,3,5],
    "Bob":   [3,1,4,2,3,1,1,3,2],
}

fig = go.Figure()

for name in ratings.keys():
    fig.add_trace(go.Scatterpolar(
        r=ratings[name] + [ratings[name][0]],
        theta=categories + [categories[0]],
        name=name,
    ))

# Pick a color palette and give a color to each kind of category
colors = px.colors.qualitative.D3
colormap = { kind: colors[i] for i, kind in enumerate(set(kinds)) }

fig.add_trace(go.Barpolar(
    theta=categories,
    r=len(categories)*[max(x for v in ratings.values() for x in v)],
    marker_color=list(map(lambda kind: colormap[kind], kinds)),
    opacity=0.6,
    name='Kind'
))

fig.update_polars(bargap=0)

fig.show()
© www.soinside.com 2019 - 2024. All rights reserved.