在3D散点图中忽略了我的自定义颜色

发布于 2025-02-09 05:57:49 字数 2523 浏览 1 评论 0原文

我正在渲染一个3D空间,看起来像一半的球体,使用图中的3D散点图。我想根据熊猫数据框架中的值更改每个点的颜色。

我的数据框的行看起来像这样:

{
   "x": 1,
   "y": -31,
   "z": -63,
   "M": -1,
   "E": -1,
   "F": -1,
   "CE": -1,
   "MA": -1,
   "R": -1
}

这样,为了构建我使用的图表:

fig = px.scatter_3d(json_df, x="x", y="y", z="z", custom_data=["M", "E", "F", "CE", "MA", "R"])
fig.update_traces(
    hovertemplate="<br>".join([
        "M: %{customdata[0]}",
        "E: %{customdata[1]}",
        "F: %{customdata[2]}",
        "CE: %{customdata[3]}",
        "MA: %{customdata[4]}",
        "R: %{customdata[5]}"
    ])
)
fig.show()

它会产生此图像(如果您也悬停了每个点,则在HoverTemplate中获得信息,

现在我想根据这些值m,e,f等更改每个点的颜色,对于非valid点具有“默认颜色”(其中m,e,f的那些等。是-1)。目前,我想对非浮游点使用颜色,而有效的颜色则是随机色。

灰色

grey_colour = 'rgb(84, 84, 84)'
def row_to_color(df_row):
    if df_row["M"] == -1:
        # non-valid colour
        return grey_colour 
    else:
        #randomly selected colour
        return 'rgb(' + str(0) + ', ' + str(np.random.randint(0,255)) + ', ' + str(np.random.randint(0,255)) + ')'

# Almost 2000-elements-long array, with a large part of it being that grey default color. 
custom_color = json_df.apply(row_to_color, axis=1)
fig = px.scatter_3d(json_df, x="x", y="y", z="z", custom_data=["M", "E", "F", "CE", "MA", "R"], color=custom_color)

但是,绘制忽略了该非valid颜色(无论是什么),然后返回到默认的颜色,只尊重与默认的颜色不同的颜色。

我已经进行了一些测试,如果我使用其他条件(因此,Custom_color数组中的“非valid颜色”项目少得多),则该图似乎正确地呈现,但是颜色为仍然不正确。例如:

def row_to_color(df_row):
    if df_row["x"] > 10: #less strict condition
        return 'rgb(84, 84, 84)' #grey
    else:
        return 'rgb(' + str(0) + ', ' + str(np.random.randint(0,255)) + ', ' + str(np.random.randint(0,255)) + ')'
...

fig = px.scatter_3d(...)

浅橙色????

对正在发生的事情有任何想法吗?

I'm rendering a 3D space that looks like half a sphere using a 3D Scatter plot in Plotly. I want to change the colour of each point according to the values in a Pandas DataFrame.

The rows of my DataFrame look like this:

{
   "x": 1,
   "y": -31,
   "z": -63,
   "M": -1,
   "E": -1,
   "F": -1,
   "CE": -1,
   "MA": -1,
   "R": -1
}

This way, in order to build my diagram I use:

fig = px.scatter_3d(json_df, x="x", y="y", z="z", custom_data=["M", "E", "F", "CE", "MA", "R"])
fig.update_traces(
    hovertemplate="<br>".join([
        "M: %{customdata[0]}",
        "E: %{customdata[1]}",
        "F: %{customdata[2]}",
        "CE: %{customdata[3]}",
        "MA: %{customdata[4]}",
        "R: %{customdata[5]}"
    ])
)
fig.show()

Which produces this image (if you also hover of each point, you get the info especified in hovertemplate):

enter image description here

Now I want to change the colour of each point according to those values M, E, F, etc., having a "default colour" for non-valid points (those where M, E, F, etc. is -1). Right now, I want to use a colour for non-valid points and a random-colour for valid ones.

Grey colour
enter image description here

grey_colour = 'rgb(84, 84, 84)'
def row_to_color(df_row):
    if df_row["M"] == -1:
        # non-valid colour
        return grey_colour 
    else:
        #randomly selected colour
        return 'rgb(' + str(0) + ', ' + str(np.random.randint(0,255)) + ', ' + str(np.random.randint(0,255)) + ')'

# Almost 2000-elements-long array, with a large part of it being that grey default color. 
custom_color = json_df.apply(row_to_color, axis=1)
fig = px.scatter_3d(json_df, x="x", y="y", z="z", custom_data=["M", "E", "F", "CE", "MA", "R"], color=custom_color)

However, Plotly ignores that non-valid colour (no matter what it is) and goes back to the default one, only respecting the colours that are different from the default one.

enter image description here

I've done some testing and if I use another condition (so there are far less "non-valid colour" items in the custom_color array), the plot seems to render correctly, but the colour is still incorrect. For instance:

def row_to_color(df_row):
    if df_row["x"] > 10: #less strict condition
        return 'rgb(84, 84, 84)' #grey
    else:
        return 'rgb(' + str(0) + ', ' + str(np.random.randint(0,255)) + ', ' + str(np.random.randint(0,255)) + ')'
...

fig = px.scatter_3d(...)

Light orange ????

enter image description here

Any idea of what is going on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奈何桥上唱咆哮 2025-02-16 05:57:49

Plotly的正式文档,不幸的是,我已经误读了),如果您想为最终图像中要绘制的每个项目使用特定颜色,则只需使用color_discrete_map =“ Identity”时,您的情节功能。

如果您不使用它,则Plotly试图使用color数组中指定的颜色作为要分配给类别的颜色的颜色。如果您没有类别,则在绘图过程中,情节只是发疯了。

通过将该参数添加到呼叫中,我们会告诉图片分别处理color数组中的每个项目,并将其设置为所提供数据中相应项目的颜色。

fig = px.scatter_3d(json_df, x="x", y="y", z="z", 
    custom_data=["M", "E", "F", "CE", "MA", "R"], 
    color=custom_color,
    color_discrete_map="identity")

According to the main maintainer of Plotly, Nicolas Kruchten, (and the official documentation of Plotly, that unfortunately I had misread), if you want to use a specific colour for every item that is going to be plotted in the final image, you just need to use the color_discrete_map="identity" argument when calling your plot function.

If you don't use it, Plotly tries to use the colours specified in the color array as colors to be assigned to categories of data. If you don't have categories, Plotly just goes nuts during the plotting.

By adding that parameter to the call, we tell Plotly to treat every item in the color array separately and set it as the colour for the corresponding item in the data provided.

fig = px.scatter_3d(json_df, x="x", y="y", z="z", 
    custom_data=["M", "E", "F", "CE", "MA", "R"], 
    color=custom_color,
    color_discrete_map="identity")

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文