散点图标记基于值的不同颜色

时间:2021-04-19 12:54:38

标签: python plotly plotly-dash

我有两列数据框。一列表示CPU节点,另一列表示阈值。我需要为该数据框绘制散点图。我的问题是如何在阈值列上获得不同的颜色。例如,0-25% 绿色、25-50% 浅绿色、50-75% 黄色和 75% 以上的红色。到目前为止,我只得到一种颜色。

下面是数据框

cpu node  threshold
node 1      20
node 2      30
node 3      50
node 4      59
node 5      65
node 6      95

我为此编写的代码:

fig = go.Figure(go.Scatter(mode="markers", x=cpu node, y=threshold, marker_symbol='triangle-up',
                           marker_line_color="midnightblue", marker_color="lightskyblue",
                           marker_line_width=2, marker_size=15,
                           hovertemplate=cpu node))

enter image description here 上面只给出了带有三角形标记的单一颜色。

谁能告诉我如何使用自定义颜色获得看起来像这样的图形,即; ['green', 'light green', 'yellow', 'red'] 基于值的范围 enter image description here

1 个答案:

答案 0 :(得分:0)

在这种情况下,将标记颜色设置为 y 轴的值。

import plotly.graph_objects as go

fig = go.Figure(go.Scatter(mode="markers", x=df['cpu node'], y=df['threshold'], marker_symbol='triangle-up',
                           marker_line_color="midnightblue", marker_color=df['threshold'],
                           marker_line_width=2, marker_size=15,
                           hovertemplate=df['cpu node']))

fig.show()

enter image description here