简体   繁体   中英

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. 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.

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). Is there any "easy" way to tell it seaborn's red? If I don't define the colors, the colors will cycle through seaborn's default color cycle. 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). 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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