简体   繁体   English

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

[英]Matplotlib + Seaborn - two lines with the same color?

I'm stuck on what is likely a simple thing. 我被困在可能是一件简单的事情上。 I'm using the default seaborn color palette in Matplotlib. 我在Matplotlib中使用默认的seaborn调色板。 I want to plot two lines that have the same color AND I want to define that color. 我想绘制两条具有相同颜色的线,我想要定义该颜色。 I would like to use the colors that are part of the default seaborn palette, ie I'd like the seaborn red instead of the Matplotlib default red. 我想使用属于默认seaborn调色板的颜色,即我喜欢seaborn红色而不是Matplotlib默认红色。

Here's my code snippet: 这是我的代码片段:

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

If I use the above code, I get Matplotlib's default red (as expected). 如果我使用上面的代码,我得到Matplotlib的默认红色(如预期的那样)。 Is there any "easy" way to tell it seaborn's red? 是否有任何“简单”的方式来告诉它seaborn的红色? If I don't define the colors, the colors will cycle through seaborn's default color cycle. 如果我没有定义颜色,颜色将循环通过seaborn的默认颜色循环。 Thanks! 谢谢!

You can get the default seaborn colours using seaborn.color_palette() (there are a few different palettes you can get at through that function as well). 您可以使用seaborn.color_palette()获取默认的seaborn颜色seaborn.color_palette()您可以通过该功能获得一些不同的调色板)。 So you can do: 所以你可以这样做:

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

You do have to go through the default palette yourself and work out which value corresponds to which colour, there are no helpful names like 'red' attached to the RBG values from what I've seen. 你必须自己完成默认调色板并确定哪个值对应于哪种颜色,没有像我看到的RBG值附加的有用名称,如“红色”。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM