python - seaborn husl 或 hsl 调色板不工作 : remains default black and white colors

标签 python matplotlib hsl seaborn

我需要一个圆形颜色图,遇到了 this answer其中描述了使用 seaborn 导入 husl 系统。我试图复制示例演示的简单用法,但我无法让我的图像以彩色显示。它总是以黑白显示(seaborn 默认调色板)。我在 ipython 中工作,但不在 ipython 笔记本中。 (一些 seaborn 函数只在 ipython notebook 中工作——我需要一个不依赖于它的答案。)特别是 python 2.7.3,ipython 1.1.0。

MWE:

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

fig = plt.figure()

im = np.random.random((100, 100))
with sns.color_palette("husl", 8):
    plt.imshow(im)

显示:

http://imgur.com/AC98hmp

最佳答案

另一个答案是(接近)正确的解决方案,但它可能有助于理解为什么会发生这种情况。 sns.set_palette 并在 with 语句中使用 sns.color_palette 控制 matplotlib 颜色循环, ( mpl.rcParams["axes.color_cycle"]),用于在使用 plt.plot 时设置绘图元素的样式。

相比之下,imshow 有一个默认的 colormap,它是两种不同类型的对象(一个是颜色列表,另一个是从标量变量到颜色)并具有不同的默认设置(mpl.rcParams["image.cmap"])。

正如@cphlewis 指出的那样,您可以使用 sns.color_palette 返回的颜色列表来制作颜色图对象,但我不会那样做。如果向图中添加颜色条,您就会明白为什么:

import numpy as np
from scipy.ndimage import gaussian_filter
import seaborn as sns
import matplotlib as mpl
import matplotlib.pyplot as plt

sns.set_style("dark")

img = np.random.normal(size=(100, 100))
img = gaussian_filter(img, 3, 2)

cmap1 = mpl.colors.ListedColormap(sns.color_palette("husl"))

plt.figure()
plt.imshow(img, cmap=cmap1)
plt.colorbar()

enter image description here

在这里,您只是制作了一个具有 6 个唯一值的颜色图,这将导致您丢失数据中的大量高频信息。最好使用更多的颜色; 256 是一个不错的数字:

cmap2 = mpl.colors.ListedColormap(sns.color_palette("husl", 256))

plt.figure()
plt.imshow(img, cmap=cmap2)
plt.colorbar()

enter image description here

您可能还想直接使用 sns.husl_palette 函数,这样您就可以控制循环从哪里开始以及用于亮度和饱和度的级别:

cmap3 = mpl.colors.ListedColormap(sns.husl_palette(256, .33, .85, .6))

plt.figure()
plt.imshow(img, cmap=cmap3)
plt.colorbar()

enter image description here

关于python - seaborn husl 或 hsl 调色板不工作 : remains default black and white colors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28892556/

相关文章:

python - 确定 matplotlib 轴大小(以像素为单位)

python - 为什么我的图表没有显示每个月? Facetgrid、Seaborn

html - Firefox 中的 hsl 颜色问题

python - 从 jwt 获取注册算法

python - 列出字典 : Even items as keys, 奇数项作为值

python - HackerRank 挑战 : Find total number of days Plants die

python - 彩条/绘图问题? "posx and posy should be finite values"

有人能明白为什么我这里有浮点异常吗?

objective-c - RGB 到 HSL 的转换似乎在数值上还可以,但在视觉上却不是这样

python - 通过 VB 运行 Python 脚本时遇到问题