Matplotlib + Seaborn - 两条线颜色相同?

Matplotlib + Seaborn - two lines with the same color?

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

这是我的代码片段:

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 的默认红色(如预期的那样)。有什么 "easy" 方法可以告诉它 seaborn 是红色的吗?如果我不定义颜色,颜色将在 seaborn 的默认颜色循环中循环。谢谢!

您可以使用 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])

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

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