go.scatterpolar:尝试以不同颜色渲染雷达图线未成功。

huangapple go评论74阅读模式
英文:

go.scatterpolar : trying to render radar graph with various lines color not working

问题

我尝试构建一个雷达图,其中每条线都有不同的颜色。

我觉得我已经仔细遵循了文档,但现在我面临一个我似乎无法解决的错误,尤其是因为没有任何错误输出!

这是我正在使用的一些虚拟数据:

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=colors[i],
        fillcolor='#d3d3d3',
        marker=dict(color=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
)

奇怪的是,当我悬停在每条线上时,会出现一个具有正确颜色和值的框架。

这里是一张图片。
go.scatterpolar:尝试以不同颜色渲染雷达图线未成功。
1: https://i.stack.imgur.com/Nn0G7.png

英文:

I am trying to build a radar chart where each line is of different color.

I feel like I have followed the doc closely and I am now facing an error I can't seem solve, especially because NO ERROR is output!

here is some dummy data I am working with :

r = [52,36,85]
theta = ["analytique", "analogique", "affectif"]

colors = ["blue", "red","yellow"]

Here is what I have for my graph:

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
)

what's strange its that when I hover each line, a frame with the right color and value shows up.

Here is a picture
go.scatterpolar:尝试以不同颜色渲染雷达图线未成功。

答案1

得分: 3

我没有完整的解决方案,但希望我的答案能指导您走在正确的方向上。

简单开始

首先,让我们使用默认颜色简化并绘制一个雷达/蜘蛛图:

import plotly.express as px
import pandas as pd

r = [52,36,85]
theta = ["analytique", "analogique", "affectif"]
types = ["one", "two","three"]
df = pd.DataFrame(dict(r=r, theta=theta, type=types))
df
r theta type
0 52 analytique one
1 36 analogique two
2 85 affectif three

使用plotly.express.line_polar绘制,得到:

fig = px.line_polar(df, r='r', theta='theta', line_close=True, markers=True)
fig.show()

go.scatterpolar:尝试以不同颜色渲染雷达图线未成功。

每个边有自己的颜色

现在,您希望每个边都有自己的颜色。在这个示例中,我假设您希望这个颜色基于我之前定义的type列。

直接绘制这个图不会奏效,它只会给您点,没有线:

fig = px.line_polar(df, r='r', theta='theta', line_close=True, color='type', markers=True)
fig.show()

go.scatterpolar:尝试以不同颜色渲染雷达图线未成功。

您需要复制行,并将顺序数据点分配给相同的type

# 首先将df追加到自身,但仅保留r和theta列
# 这将使追加行的type列为NaN
df2 = pd.concat([df, df[['r', 'theta']]]).sort_values(by=['r', 'theta'])
# 现在通过取下一行的type值来填充NaN type值
df2.type.fillna(method='bfill', inplace=True)
# 最后一个type值应该与第一个type值相同以关闭循环
# 这需要手动设置
df2.type.fillna(df2.type.iloc[0], inplace=True)
df2
r theta type
1 36 analogique two
1 36 analogique one
0 52 analytique one
0 52 analytique three
2 85 affectif three
2 85 affectif two

现在,如果您绘制它,您将得到一个三角形,每个边都有不同的颜色:

fig = px.line_polar(df2, r='r', theta='theta', color='type', line_close=True, markers=True)
fig.show()

go.scatterpolar:尝试以不同颜色渲染雷达图线未成功。

不确定为什么类别的顺序已更改,但您可以通过不同方式对df2 DataFrame 进行排序来解决这个问题。

文本标签

如果您希望在图中有文本标签,您将在文档中找到一个text参数:

fig = px.line_polar(df2, r='r', theta='theta', color='type', text='r', line_close=True, markers=True)
fig.update_traces(textposition='top center')

go.scatterpolar:尝试以不同颜色渲染雷达图线未成功。

英文:

I don't have a full solution for you, but I hope my answer leads you in the right way.

Simple start

First, let's simplify and plot a radar/spyder plot with default colors:

import plotly.express as px
import pandas as pd

r = [52,36,85]
theta = ["analytique", "analogique", "affectif"]
types = ["one", "two","three"]
df = pd.DataFrame(dict(r=r, theta=theta, type=types))
df
r theta type
0 52 analytique one
1 36 analogique two
2 85 affectif three

Plotting this with plotly.express.line_polar, gives:

fig = px.line_polar(df, r='r', theta='theta', line_close=True, markers=True)
fig.show()

go.scatterpolar:尝试以不同颜色渲染雷达图线未成功。

Every edge its own color

Now, you want every edge to have it's own color. For the sake of this example, I assume you want this color to be based on the column type which I defined earlier.

Simply plotting this straight away will not work, it will only give you the dots, no lines:

fig = px.line_polar(df, r='r', theta='theta', line_close=True, color='type', markers=True)
fig.show()

go.scatterpolar:尝试以不同颜色渲染雷达图线未成功。

You need to duplicate the rows, and assign sequential data points the same type.

# First append the df to itself, but only keep the r and theta columns
# This will make the type column NaN for the appended rows
df2 = pd.concat([df, df[['r', 'theta']]]).sort_values(by=['r', 'theta'])
# Now fill the NaN type value by taking the type value of the next row
df2.type.fillna(method='bfill', inplace=True)
# The last type value should be equal to the first type value to close the loop
# This needs to be set manually
df2.type.fillna(df2.type.iloc[0], inplace=True)
df2
r theta type
1 36 analogique two
1 36 analogique one
0 52 analytique one
0 52 analytique three
2 85 affectif three
2 85 affectif two

Now if you plot that, you will get a triangle with every edge having a separate color:

fig = px.line_polar(df2, r='r', theta='theta', color='type', line_close=True, markers=True)
fig.show()

go.scatterpolar:尝试以不同颜色渲染雷达图线未成功。

Not sure why the categories have changed order, but you can probably fix that by sorting the df2 DataFrame differently.

Text labels

If you would like to have text labels in your graph, you'll find in the docs that there is a text parameter:

fig = px.line_polar(df2, r='r', theta='theta', color='type', text='r', line_close=True, markers=True)
fig.update_traces(textposition='top center')

go.scatterpolar:尝试以不同颜色渲染雷达图线未成功。

huangapple
  • 本文由 发表于 2023年2月7日 00:54:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75364299.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定