【问题标题】:How to set line color with a given rgb value?如何使用给定的 rgb 值设置线条颜色?
【发布时间】:2022-01-13 01:57:27
【问题描述】:

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

【问题讨论】:

    标签: python matplotlib seaborn


    【解决方案1】:

    有一个参数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,
    )
    

    结果:

    【讨论】:

      猜你喜欢
      • 2021-07-12
      • 2017-10-09
      • 2015-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-30
      • 2017-06-18
      相关资源
      最近更新 更多