在Seaborn中创建一个黑暗的反色调色板

Nin*_*non 10 python python-2.7 seaborn

我正在使用顺序调色板创建一个包含几个图的图形,如下所示:

import matplotlib.pyplot as plt
import seaborn as sns
import math

figure = plt.figure(1)
x = range(1, 200)
n_plots = 10

with sns.color_palette('Blues_d', n_colors=n_plots):
    for offset in range(n_plots):
        plt.plot(x, [offset + math.sin(float(i) / 10) for i in range(len(x))])

figure.show()
Run Code Online (Sandbox Code Playgroud)

但是,我想颠倒调色板.该教程声明我可以添加'_r'到调色板名称以反转它并'_d'使其"变暗".但我似乎没有能够做到这些结合在一起:'_r_d','_d_r','_rd''_dr'所有产生错误.如何创建黑暗的反转调色板?

Nin*_*non 9

我正在回答我自己的问题,发布我使用的解决方案的细节和解释,因为mwaskom的建议需要调整.运用

with reversed(sns.color_palette('Blues_d', n_colors=n_plots)):
Run Code Online (Sandbox Code Playgroud)

抛出AttributeError: __exit__,我相信因为with语句需要一个带有__enter____exit__方法的对象,reversed迭代器不满足.如果我使用sns.set_palette(reversed(palette))而不是with语句,则忽略绘图中颜色的数量(使用默认值6 - 我不知道为什么),即使遵循颜色方案.为了解决这个问题,我使用list.reverse()方法:

figure = plt.figure(1)
x = range(1, 200)
n_plots = 10
palette = sns.color_palette("Blues_d", n_colors=n_plots)
palette.reverse()

with palette:
    for offset in range(n_plots):
        plt.plot(x, [offset + math.sin(float(i) / 10) for i in range(len(x))])

figure.show()
Run Code Online (Sandbox Code Playgroud)

编辑:发现n_colors调用中忽略参数的原因set_palette是因为n_colors参数也必须在该调用中指定.因此另一个解决方案是

figure = plt.figure(1)
x = range(1, 200)
n_plots = 10

sns.set_palette(reversed(sns.color_palette("Blues_d", n_plots)), n_plots)

for offset in range(n_plots):
    plt.plot(x, [offset + math.sin(float(i) / 10) for i in range(len(x))])

figure.show()
Run Code Online (Sandbox Code Playgroud)