wordcloud设置字体颜色与图标颜色一致
时间: 2023-11-30 14:04:47 浏览: 189
您好!要设置WordCloud字体颜色与图标颜色一致,可以使用WordCloud中的`color_func`参数来实现。首先,您需要安装`matplotlib`库。
以下是示例代码:
```python
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
text = "Hello World"
mask = np.array(Image.open("mask.png"))
def color_func(word, font_size, position, orientation, random_state=None, **kwargs):
return tuple(mask[position[1], position[0]])
wordcloud = WordCloud(background_color="white", mask=mask, color_func=color_func).generate(text)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()
```
在上面的代码中,`color_func`函数返回了与图标颜色相同的颜色元组。`mask`参数指定了图标的形状。您可以将`mask.png`替换为您的图标文件名。最后,使用`imshow`和`axis`函数显示词云图。
希望这能帮到您!
阅读全文
相关推荐

















