Python 更改生成的word cloud的字体颜色

Python 更改生成的word cloud的字体颜色,python,word-cloud,Python,Word Cloud,我目前正在使用生成单词云。当前,生成的图片如下所示: 但是,我只能用黑白打印机打印出来,因此字体颜色会变浅,我想把它们改成黑色。不幸的是,这是我能找到的唯一一个示例,代码如下: def grey_color_func(word, font_size, position, orientation, random_state=None,**kwargs): return "hsl(0, 0%%, %d%%)" % random.randint(60, 100) wc = WordCloud

我目前正在使用生成单词云。当前,生成的图片如下所示:

但是,我只能用黑白打印机打印出来,因此字体颜色会变浅,我想把它们改成黑色。不幸的是,这是我能找到的唯一一个示例,代码如下:

def grey_color_func(word, font_size, position, orientation, random_state=None,**kwargs):
    return "hsl(0, 0%%, %d%%)" % random.randint(60, 100)
wc = WordCloud(max_words=1000, mask=mask, stopwords=stopwords, margin=10,random_state=1).generate(text)
# store default colored image
default_colors = wc.to_array()
plt.title("Custom colors")
plt.imshow(wc.recolor(color_func=grey_color_func, random_state=3),
interpolation="bilinear")
我不知道在这种情况下,
grey\u color\u func
会做什么,也不知道如何更改它以生成黑色


欢迎任何建议

这与在这个尺度上代表什么有很大关系。由于色调和饱和度都设置为零,因此亮度是控制灰度部分的因素。也就是说,这个特定的随机值决定了灰度:

random.randint(60, 100)

实际上,您需要将此随机整数的边界更改为0到30之间的值,或者更改为0到之间的值才能使用打印机。

HSL表示色调饱和度亮度()。您还可以更改返回的字符串中的h(即色调)值,以获得不同的颜色:

def grey_color_func(word, font_size, position, orientation, random_state=None,
                    **kwargs):
    return "hsl(203, 100%%, %50d%%)" % random.randint(60, 100)  
# hsla(282, 100%, 50%, 1) PINK
# hsla(203, 100%, 50%, 1) BLUE 
这很有趣。我使用site获取hsla颜色值(然后我忽略了a(即alpha)值)


还有底部奇怪的语法:
“hsl(203100%,%50d%)%random.randint(60100)
,是某种c风格。randint()调用返回的内容进入到%d处的字符串中(或我的代码中的%50d)。我不确定确切的原因/方式,但使用该前缀数字会稍微改变颜色).

您知道HSL是什么以及它与RGB或hex的区别吗?请参阅
类WordCloud的文档字符串第220行的
color\u func
参数。不知道。这就是问题所在。hsl到底是什么?谢谢。我试试看。