Home > OS >  How to set line color with a given rgb value?
How to set line color with a given rgb value?

Time:12-09

I have a seaborn.lineplot(), and a list of rgb values such as [26,201,55].

I don't know which parameter I can set in sns.lineplot to change the color as I wish. Here is my wrong code:

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

CodePudding user response:

There is a parameter palette for defining colors, palette is just a list of colors defined as tuples with 3 values between 0 and 1.

If you have one color, you can use:

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

But for multiple colors:

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

And then use:

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

Random example:

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

Result:

enter image description here

  • Related