问题描述
Seaborn提供了一个称为color_palette的功能,该功能使您可以轻松地为绘图创建新的color_palettes.
Seaborn offers a function called color_palette, which allows you to easily create new color_palettes for plots.
colors = ["#67E568","#257F27","#08420D","#FFF000","#FFB62B","#E56124","#E53E30","#7F2353","#F911FF","#9F8CA6"]
color_palette = sns.color_palette(colors)
我想将color_palette转换为cmap,可以在matplotlib中使用它,但是我不知道如何做到这一点.
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.
可悲的是,像"cubehelix_palette","light_palette"之类的函数具有"as_cmap"参数.不幸的是,"color_palette"没有.
Sadly just functions like "cubehelix_palette","light_palette",… have an "as_cmap" paramater. "color_palette" doesn't, unfortunately.
推荐答案
您必须将颜色列表从海洋调色板转换为matplolib的颜色映射(建议更改为@RafaelLopes):
You have to convert a list of colors from seaborn palette to color map of matplolib (thx to @RafaelLopes for proposed changes):
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()
这篇关于seaborn color_palette作为matplotlib颜色图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!