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

scatter plot markers different colors based on value

我有两列数据框。一列表示 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))

以上仅给出了带有三角形标记的单一颜色。

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

在这种情况下,将标记颜色设置为 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()