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

3

我有一个 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]))
1个回答

3

有一个参数palette用于定义颜色,palette只是由三个值为0到1的tuples定义的颜色列表。

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

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,
)

结果:

在此输入图片描述


(注:该内容为HTML标签,请勿删除或修改)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接