在 Seaborn 中创建深色反转调色板
Creating a dark, reversed color palette in 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()
但是,我想反转调色板。 The tutorial 声明我可以将 '_r'
添加到调色板名称以反转它,并添加 '_d'
使其成为 "dark"。但我似乎无法同时执行这些操作:'_r_d'
、'_d_r'
、'_rd'
和 '_dr'
都会产生错误。我怎样才能创建一个深色的反转调色板?
我正在回答我自己的问题 post 我使用的解决方案的详细信息和解释,因为 mwaskom 的建议需要进行调整。使用
with reversed(sns.color_palette('Blues_d', n_colors=n_plots)):
throws 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()
编辑: 我 discovered 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()
我正在使用顺序调色板创建一个包含多个图的图形,如下所示:
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()
但是,我想反转调色板。 The tutorial 声明我可以将 '_r'
添加到调色板名称以反转它,并添加 '_d'
使其成为 "dark"。但我似乎无法同时执行这些操作:'_r_d'
、'_d_r'
、'_rd'
和 '_dr'
都会产生错误。我怎样才能创建一个深色的反转调色板?
我正在回答我自己的问题 post 我使用的解决方案的详细信息和解释,因为 mwaskom 的建议需要进行调整。使用
with reversed(sns.color_palette('Blues_d', n_colors=n_plots)):
throws 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()
编辑: 我 discovered 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()