简体   繁体   English

当色块数量超过颜色数量时,seaborn会忽略自定义调色板

[英]seaborn ignores custom color palette when number of patches exceeds number of colors

I set up a palette containing just black and gray as follows. 我设置了一个仅包含黑色和灰色的调色板,如下所示。

import seaborn as sns
sns.set_palette(['#000000', '#ABABAB'])

This works fine if I just have two or less patches in my plot. 如果我的绘图中只有两个或更少的色块,则此效果很好。

sns.barplot([1, 2], [3, 4])

However, when I have more bars than colors, seaborn switches back to the default palette. 但是,当条形多于颜色时, seaborn切换回默认调色板。

sns.barplot([1, 2, 3], [4, 5, 6]) 

Can I make seaborn cycle through my custom color palette? 我可以在自定义调色板中进行seaborn循环吗? In the case above, the first bar should be black, the second grey and the third one black again. 在上述情况下,第一个条应为黑色,第二条应为灰色,第三个应为黑色。

My current workaround is 我目前的解决方法是

sns.set_palette(['#000000', '#ABABAB']*1000)

but I doubt this is the standard way to do it. 但我怀疑这是标准的做法。

It is quite simple as per the official docs. 根据官方文档,这非常简单。 Just set n_colors to be more than the number of colors in the palette. 只需将n_colors设置为大于调色板中的颜色数量即可。 Below is the relevant information (highlighted in bold ). 以下是相关信息( 以粗体突出显示 )。 Here you can just set n_colors to a large number. 在这里,您可以将n_colors设置为一个较大的数字。 Anything more than 2 will work in your case. 大于2的情况将适用于您的情况。

n_colors : int, optional Number of colors in the palette. n_colors:int,可选调色板中的颜色数。 If None , the default will depend on how palette is specified. 如果为None ,则默认值取决于如何指定palette Named palettes default to 6 colors, but grabbing the current palette or passing in a list of colors will not change the number of colors unless this is specified. 命名调色板默认为6种颜色,但是除非指定,否则获取当前调色板或传入颜色列表将不会更改颜色数量。 Asking for more colors than exist in the palette will cause it to cycle. 要求的颜色超过调色板中的颜色将导致其循环。

import seaborn as sns
sns.set_palette(['#000000', '#ABABAB'], n_colors=100)

sns.barplot([1, 2, 3], [4, 5, 6]) 

EDIT It seems that one has to have to n_colors also more than the number of patches (bars in this case). 编辑似乎必须要n_colors也要比补丁数(在这种情况下为条)还要多。 So if there are two colors in the palette and 4 patches, then n_colors should be also more than both (>4 in this case). 因此,如果调色板中有两种颜色和4个色块,则n_colors也应大于两者(在这种情况下为> 4)。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM