【问题标题】:Matplotlib + Seaborn - two lines with the same color?Matplotlib + Seaborn - 两条颜色相同的线?
【发布时间】:2016-02-04 12:27:53
【问题描述】:

我被困在可能很简单的事情上。我在 Matplotlib 中使用默认的 seaborn 调色板。我想绘制两条具有相同颜色的线并且我想定义该颜色。我想使用默认 seaborn 调色板中的颜色,即我想要 seaborn red 而不是 Matplotlib 默认红色。

这是我的代码 sn-p:

import pylab as plot
import seaborn

t = np.arange(0.0, 2.0, 0.01)
s = np.sin(2*np.pi*t)
plt.plot(t, s, 'r')
plt.plot(t, 2*s, 'r')

如果我使用上面的代码,我会得到 Matplotlib 的默认红色(如预期的那样)。有什么“简单”的方法可以告诉它seaborn的红色吗?如果我不定义颜色,颜色将在 seaborn 的默认颜色循环中循环。谢谢!

【问题讨论】:

    标签: python matplotlib colors seaborn


    【解决方案1】:

    在seaborn 0.6或更高版本中,您可以调用seaborn.set_color_codes()seaborn.set(color_codes=True)"r"将被解释为默认的seaborn red。

    【讨论】:

    • 这是一个不错的选择。感谢分享!
    【解决方案2】:

    您可以使用seaborn.color_palette() 获得默认的seaborn 颜色(您也可以通过该功能获得一些不同的调色板)。所以你可以这样做:

    t = np.arange(0.0, 2.0, 0.01)
    s = np.sin(2*np.pi*t)
    plt.plot(t, s, c=seaborn.color_palette()[2])
    plt.plot(t, 2*s, c=seaborn.color_palette()[2])
    

    您必须自己检查默认调色板并确定哪个值对应于哪种颜色,根据我所见,没有像“红色”这样有用的名称附加到 RBG 值。

    【讨论】:

    • 这正是我想要的。非常感谢!
    • 查看我的答案,了解在以后版本的 seaborn 中执行此操作的更好方法。
    猜你喜欢
    • 2016-02-07
    • 1970-01-01
    • 2023-03-29
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    • 2018-11-26
    • 2016-10-19
    相关资源
    最近更新 更多