赞
踩
WordCloud 官方文档:https://amueller.github.io/word_cloud/index.html
WordCloud GitHub 地址:https://github.com/amueller/word_cloud
Python非常重要的一个可视化库,wordcloud词云库了解一下!:https://www.bilibili.com/video/av26266917
一个免费的生成词云(word cloud)的在线工具:https://segmentfault.com/a/1190000016827687
python词云 wordcloud 入门 :https://blog.csdn.net/tanzuozhev/article/details/50789226
Python第三方库wordcloud(词云)快速入门与进阶:https://blog.csdn.net/qq_34337272/article/details/79552929
词云可视化:安装模块 wordcloud: pip install wordcloud
词云又叫文字云,是对文本数据中出现频率较高的“关键词”在视觉上的突出呈现,形成关键词的渲染形成类似云一样的彩色图片,从而一眼就可以领略文本数据的主要表达意思。
python开发环境、wordcloud、jieba、matplotlib、numpy 、PIL 等库文件安装好。
安装完成以后 ( 命令行使用方式 )
wordcloud_cli --text in.txt --imagefile out.png --mask in.png
text 是词云来源,mask 是背景框架 ,imagefile 输出的文件
wordcloud_cli --help 查看所有支持的命令参数
wordcloud生成词云的原理简介
wordcloud生成词云的原理其实并不复杂,大体分成5步(具体可自行查看源码):
wordcloud.WordCloud 类 的 参数 说明
class wordcloud.WordCloud(font_path=None, width=400, height=200, margin=2, ranks_only=None, prefer_horizontal=0.9,mask=None, scale=1, color_func=None, max_words=200, min_font_size=4, stopwords=None, random_state=None,background_color='black', max_font_size=None, font_step=1, mode='RGB', relative_scaling=0.5, regexp=None, collocations=True,colormap=None, normalize_plurals=True)
参数
font_path : string //字体路径,需要展现什么字体就把该字体路径+后缀名写上,如:font_path = '黑体.ttf’如果不指定字体中文字的显示不出来
width : int (default=400) //输出的画布宽度,默认为400像素
height : int (default=200) //输出的画布高度,默认为200像素
prefer_horizontal : float (default=0.90) //词语水平方向排版出现的频率,默认 0.9 (所以词语垂直方向排版出现频率为 0.1 )
mask : nd-array or None (default=None) //如果参数为空,则使用二维遮罩绘制词云。如果 mask 非空,设置的宽高值将被忽略,遮罩形状被 mask 取代。
scale : float (default=1) //按照比例进行放大画布,如设置为1.5,则长和宽都是原来画布的1.5倍。
min_font_size : int (default=4) //显示的最小的字体大小
font_step : int (default=1) //字体步长,如果步长大于1,会加快运算但是可能导致结果出现较大的误差。
max_words : number (default=200) //要显示的词的最大个数
stopwords : set of strings or None //设置需要屏蔽的词,如果为空,则使用内置的STOPWORDS
background_color : color value (default=”black”) //背景颜色,如background_color=‘white’,背景颜色为白色。
max_font_size : int or None (default=None) //显示的最大的字体大小
mode : string (default=”RGB”) //当参数为“RGBA”并且background_color不为空时,背景为透明。
relative_scaling : float (default=.5) //词频和字体大小的关联性
color_func : callable, default=None //生成新颜色的函数,如果为空,则使用 self.color_func
regexp : string or None (optional) //使用正则表达式分隔输入的文本
collocations : bool, default=True //是否包括两个词的搭配
colormap : string or matplotlib colormap, default=”viridis” //给每个单词随机分配颜色,若指定color_func,则忽略该方法。
示例 :
- #!/usr/bin/python3
- # -*- coding: utf-8 -*-
- # @Author :
- # @File : test.py
- # @Software : PyCharm
- # @description : XXX
-
-
- import jieba
- from os import path
- import matplotlib.pyplot as plt
- import numpy as np
- from PIL import Image
- from scipy.misc import imread
- from wordcloud import WordCloud, ImageColorGenerator, STOPWORDS
-
-
- def test_1():
- """
- 不是用背景图片
- :return:
- """
- f = open("e:/shijing_guanju.txt", "r")
- t = f.read()
- f.close()
- ls = jieba.lcut(t)
-
- txt = " ".join(ls)
- w = WordCloud(
- width=1000, height=700,
- background_color="white",
- # font_path="MSYH.ttc" # 没有设置字体可能出现,词云的结果均为方框。建议设置MSYH.ttc(微软雅黑)
- font_path="simsun.ttc" # 设置微软雅黑报错,这里设置 宋体
- )
- w.generate(txt)
- w.to_file("e:/word_cloud_1.png") # 在程序当前目录,word_cloud_1.png
-
-
- def test_2():
- """
- 使用背景图片
- :return:
- """
- mask = imread("e:/china_map.jpg") # 设置背景图片china_map.jpg
- excludes = {}
- f = open("e:/shijing_guanju.txt", "r")
-
- t = f.read()
- f.close()
- ls = jieba.lcut(t)
- txt = " ".join(ls)
-
- w = WordCloud(
- width=550, height=500,
- background_color="white",
- font_path="simsun.ttc", # 没有设置字体可能出现,词云的结果均为方框。建议设置MSYH.ttc(微软雅黑)
- mask=mask
- )
-
- w.generate(txt)
- w.to_file("e:/word_cloud_2.png") # 在程序当前目录,word_cloud_1.png
-
-
- def test_3():
-
- # 读入背景图片
- abel_mask = np.array(Image.open("/home/djh/PycharmProjects/source/test.jpg"))
-
- # 读取要生成词云的文件
- text_from_file_with_apath = open('/home/djh/PycharmProjects/source/a.txt').read()
-
- # 通过jieba分词进行分词并通过空格分隔
- word_list_after_jieba = jieba.cut(text_from_file_with_apath, cut_all=True)
- wl_space_split = " ".join(word_list_after_jieba)
- # my_word_cloud = WordCloud().generate(wl_space_split) 默认构造函数
- my_word_cloud = WordCloud(
- background_color='white', # 设置背景颜色
- mask=abel_mask, # 设置背景图片
- max_words=200, # 设置最大现实的字数
- stopwords=STOPWORDS, # 设置停用词
- font_path='/home/djh/win_font/simkai.ttf', # 设置字体格式,如不设置显示不了中文
- max_font_size=50, # 设置字体最大值
- random_state=30, # 设置有多少种随机生成状态,即有多少种配色方案
- scale=.5
- ).generate(wl_space_split)
-
- # 根据图片生成词云颜色
- image_colors = ImageColorGenerator(abel_mask)
- # my_word_cloud.recolor(color_func=image_colors)
-
- # 以下代码显示图片
- plt.imshow(my_word_cloud)
- plt.axis("off")
- plt.show()
-
-
- def test_4():
- """
- 简易版
- :return:
- """
- f = open(u'txt/AliceEN.txt', 'r').read()
- wordcloud = WordCloud(background_color="white", width=1000, height=860, margin=2).generate(f)
-
- # width,height,margin可以设置图片属性
- # 你可以通过font_path参数来设置字体集
- # background_color参数为设置背景颜色,默认颜色为黑色
-
- plt.imshow(wordcloud)
- plt.axis("off")
- plt.show()
-
- wordcloud.to_file('test.png')
- # 保存图片,但是在第三模块的例子中 图片大小将会按照 mask 保存
-
-
- def test_5():
- """
- 进阶版
- :return:
- """
- d = path.dirname(__file__)
-
- # Read the whole text.
- text = open(path.join(d, 'alice.txt')).read()
-
- # read the mask / color image taken from
- # http://jirkavinse.deviantart.com/art/quot-Real-Life-quot-Alice-282261010
- alice_coloring = np.array(Image.open(path.join(d, "alice_color.png")))
-
- # 设置停用词
- stopwords = set(STOPWORDS)
- stopwords.add("said")
-
- # 你可以通过 mask 参数 来设置词云形状
- wc = WordCloud(background_color="white", max_words=2000, mask=alice_coloring,
- stopwords=stopwords, max_font_size=40, random_state=42)
- # generate word cloud
- wc.generate(text)
-
- # create coloring from image
- image_colors = ImageColorGenerator(alice_coloring)
-
- # show
- # 在只设置mask的情况下,你将会得到一个拥有图片形状的词云
- plt.imshow(wc, interpolation="bilinear")
- plt.axis("off")
- plt.figure()
- # recolor wordcloud and show
- # we could also give color_func=image_colors directly in the constructor
- # 我们还可以直接在构造函数中直接给颜色
- # 通过这种方式词云将会按照给定的图片颜色布局生成字体颜色策略
- plt.imshow(wc.recolor(color_func=image_colors), interpolation="bilinear")
- plt.axis("off")
- plt.figure()
- plt.imshow(alice_coloring, cmap=plt.cm.gray, interpolation="bilinear")
- plt.axis("off")
- plt.show()
-
-
- if __name__ == '__main__':
- test_1()
- # test_2()
- # test_3()
- # test_4()
- # test_5()
- pass
-

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。