如何使用给定的 rgb 值设置线条颜色?

How to set line color with a given rgb value?

我有一个 seaborn.lineplot(),以及一个 rgb 值列表,例如 [26,201,55]

不知道可以在sns.lineplot中设置哪个参数来随意改变颜色。 这是我的错误代码:

sns.lineplot(x='Episode',y='Mean Reward',sizes=(.25,.25),hue='Agent', data=df[0], palette=[r_rl/255,g_rl/255,b_rl/255]))

有一个参数palette用于定义颜色,palette只是定义为tuples的颜色列表,其中3个值介于0和1之间。

如果你有一种颜色,你可以使用:

palette=[(r_rl/255, g_rl/255, b_rl/255)]

但对于多种颜色:

rgb = [(66, 135, 245), (255, 25, 0)]                # first blue, second red
colors = [tuple(t / 255 for t in x) for x in rgb]

然后使用:

sns.lineplot(
    x="Episode",
    y="Mean Reward",
    sizes=(0.25, 0.25),
    hue="Agent",
    data=df[0],
    palette=colors,
)

随机示例:

import seaborn as sns
import matplotlib.pyplot as plt

rgb = [(66, 135, 245), (255, 25, 0)]
colors = [tuple(t / 255 for t in x) for x in rgb]

sns.set()
fmri = sns.load_dataset("fmri")
ax = sns.lineplot(
    x="timepoint",
    y="signal",
    hue="event",
    data=fmri,
    palette=colors,
)

结果: