Python词云图 WordCloud

WordCloud官网

官方最简单的例子

from os import path
from wordcloud import WordCloud

d = path.dirname(__file__)

# Read the whole text.
text = open(path.join(d, 'constitution.txt')).read()

# Generate a word cloud image
wordcloud = WordCloud().generate(text)

# Display the generated image:
# the matplotlib way:
import matplotlib.pyplot as plt
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")

# lower max_font_size
wordcloud = WordCloud(max_font_size=40).generate(text)
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.show()

# The pil way (if you don't have matplotlib)
# image = wordcloud.to_image()
# image.show()

查看官方所有例子

需求:

  1. 文字统一颜色。
  2. 透明背景。
  3. 按照指定样式显示。

要求所有颜色为#FFF0D0,使用在线工具将RGB转换为hsl格式:hsl(40.9,100%,90.8%),因为WordCloud API hsl值为浮点数时异常,因此改为整数hsl(41,100%,91%)

统一文字颜色

WordCloud(mode='RGBA', colormap='pink')
mode设置为时RGBA时,使用colormap色值,参数的值可以从文档中或者报错信息中得知。

背景透明

WordCloud(background_color=None)


参考:

Using custom colors

Python词云 wordcloud 十五分钟入门与进阶

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容