Python seaborn color_palette 作为 matplotlib 颜色图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37902459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:05:59  来源:igfitidea点击:

seaborn color_palette as matplotlib colormap

pythonmatplotlibplotseaborn

提问by Dremet

Seaborn offers a function called color_palette, which allows you to easily create new color_palettes for plots.

Seaborn 提供了一个名为 color_palette 的函数,它允许您轻松地为绘图创建新的 color_palettes。

colors = ["#67E568","#257F27","#08420D","#FFF000","#FFB62B","#E56124","#E53E30","#7F2353","#F911FF","#9F8CA6"]

color_palette = sns.color_palette(colors)

I want to transform color_palette to a cmap, which I can use in matplotlib, but I don't see how I can do this.

我想将 color_palette 转换为 cmap,我可以在 matplotlib 中使用它,但我不知道如何做到这一点。

Sadly just functions like "cubehelix_palette","light_palette",… have an "as_cmap" paramater. "color_palette" doesn't, unfortunately.

可悲的是,只有像“cubehelix_palette”、“light_palette”这样的函数……有一个“as_cmap”参数。不幸的是,“color_palette”没有。

回答by Serenity

You have to convert a list of colors from seaborn palette to color map of matplolib (thx to @RafaelLopes for proposed changes):

您必须将 seaborn 调色板中的颜色列表转换为 matplolib 的颜色图(感谢@RafaelLopes 提出的更改):

import seaborn as sns
import matplotlib.pylab as plt
import numpy as np
from matplotlib.colors import ListedColormap

# construct cmap
flatui = ["#9b59b6", "#3498db", "#95a5a6", "#e74c3c", "#34495e", "#2ecc71"]
my_cmap = ListedColormap(sns.color_palette(flatui).as_hex())

N = 500
data1 = np.random.randn(N)
data2 = np.random.randn(N)
colors = np.linspace(0,1,N)
plt.scatter(data1, data2, c=colors, cmap=my_cmap)
plt.colorbar()
plt.show()

enter image description here

在此处输入图片说明

回答by Ramon Crehuet

Most seaborn methods to generate color palettes have an optional argument as_cmapwhich by default is False. You can use to directly get a Matplotlib colormap:

大多数生成调色板的 seaborn 方法都有一个可选参数as_cmap,默认情况下为False. 您可以使用直接获取 Matplotlib 颜色图:

import seaborn as sns
import matplotlib.pylab as plt
import numpy as np

# construct cmap
my_cmap = sns.light_palette("Navy", as_cmap=True)

N = 500
data1 = np.random.randn(N)
data2 = np.random.randn(N)
colors = np.linspace(0,1,N)
plt.scatter(data1, data2, c=colors, cmap=my_cmap)
plt.colorbar()
plt.show()

enter image description here

在此处输入图片说明

回答by Generic Wevers

The first answer is somehow correct but way too long with a lot of unnecessary information. The correct and short answer is:

第一个答案在某种程度上是正确的,但时间太长,有很多不必要的信息。正确而简短的答案是:

To convert any sns.color_palette()to a matplotlib compatible cmap you need two lines of code

要将 any 转换sns.color_palette()为与 matplotlib 兼容的 cmap,您需要两行代码

from matplotlib.colors import ListedColormap
cmap = ListedColormap(sns.color_palette())

回答by LauraD

Just an additional tip - if one wants a continuous colorbar/colormap, adding 256 as the number of colors required from Seaborn colorscheme helps a lot.

只是一个额外的提示 - 如果想要一个连续的颜色条/颜色图,添加 256 作为 Seaborn 颜色方案所需的颜色数量有很大帮助。

cmap = ListedColormap(sns.color_palette("Spectral",256))