Python数据可视化 -- Wordcloud

安装

启动命令行,输入:pip install wordcloud

word cloud 库介绍 及简单使用

wordcloud库,可以说是python非常优秀的词云展示第三方库。词云以词语为基本单位更加直观和艺术的展示文本
词云图,也叫文字云,是对文本中出现频率较高的“关键词”予以视觉化的展现,词云图过滤掉大量的低频低质的文本信息,使得浏览者只要一眼扫过文本就可领略文本的主旨。
基于Python的词云生成类库,很好用,而且功能强大。在做统计分析的时候有着很好的应用,比较推荐。

快速生成词云

#导入所需库
from wordcloud import WordCloud
f = open(r'C:\Users\JluTIger\Desktop\texten.txt','r').read()
wordcloud = WordCloud(background_color="white",
width=1000,
height=860,
margin=2).generate(f) # width,height,margin可以设置图片属性
# generate 可以对全部文本进行自动分词,但是对中文支持不好
# 可以设置font_path参数来设置字体集 添加一个中文字体文件,一般是.ttf或.otf格式
#background_color参数为设置背景颜色,默认颜色为黑色 import matplotlib.pyplot as plt
plt.imshow(wordcloud)
plt.axis("off")#不显示坐标轴
plt.show()#显示图片
wordcloud.to_file('test.png')#保存图片
# 保存图片,但是在第三模块的例子中 图片大小将会按照 mask 保存





from wordcloud import WordCloud
fontpath='SourceHanSansCN-Regular.otf' wc = WordCloud(font_path=fontpath, # 设置字体
background_color="white", # 背景颜色
max_words=1000, # 词云显示的最大词数
max_font_size=500, # 字体最大值
min_font_size=20, #字体最小值
random_state=42, #随机数
collocations=False, #避免重复单词
width=1600,height=1200,margin=10, #图像宽高,字间距,需要配合下面的plt.figure(dpi=xx)放缩才有效
)
wc.generate(cuted)

分词工具 -- jieba

import jieba
cut = jieba.cut(text) #text为你需要分词的字符串/句子
string = ' '.join(cut) #将分开的词用空格连接
print(string)
Building prefix dict from the default dictionary ...
Loading model from cache C:\Users\mengx7\AppData\Local\Temp\jieba.cache
这是 一个 简单 的 例子
Loading model cost 0.978 seconds.
Prefix dict has been built succesfully.

去除冗余单词

import jieba

removes =['熟悉', '技术', '职位', '相关', '工作', '开发', '使用','能力','优先','描述','任职']
for w in removes:
jieba.del_word(w) words = jieba.lcut(text)
cuted = ' '.join(words)
print(cuted[:100]) 或者
words = jieba.lcut(text)
words = [w for w in words if w not in removes]

区分中英文

如果我们只关注英文技术点,比如python,tensorflow等,那就忽略中文内容。

使用正则表达式来匹配提取哪些由az小写字母和AZ大写字母加上0~9数字组成的单词。

import jieba
words = jieba.lcut(text)
import re
pattern = re.compile(r'^[a-zA-Z0-1]+$')
words = [w for w in words if pattern.match(w)]
cuted = ' '.join(words)
print(cuted[:100])

分好词后就需要将词做成词云了,使用的是wordclould


from matplotlib import pyplot as plt
from wordcloud import WordCloud string = 'Importance of relative word frequencies for font-size. With relative_scaling=0, only word-ranks are considered. With relative_scaling=1, a word that is twice as frequent will have twice the size. If you want to consider the word frequencies and not only their rank, relative_scaling around .5 often looks good.'
font = r'C:\Windows\Fonts\FZSTK.TTF'
wc = WordCloud(font_path=font, #如果是中文必须要添加这个,否则会显示成框框
background_color='white',
width=1000,
height=800,
).generate(string)
wc.to_file('ss.png') #保存图片
plt.imshow(wc) #用plt显示图片
plt.axis('off') #不显示坐标轴
plt.show() #显示图片

例子

  1. 读取文件
  2. jieba分词
  3. 利用re正则表达式选出英文单词
  4. 生成词云对象,利用图片遮罩形状和改变颜色
  5. 使用Matplotlib来显示图片

#cell-1
text=''
with open('./lagou-job1000-ai-details.txt','r') as f:
text=f.read()
f.close()
print(text[:100]) #cell-2
import jieba
words = jieba.lcut(text)
import re
pattern = re.compile(r'^[a-zA-Z0-1]+$')
words = [w for w in words if pattern.match(w)]
cuted = ' '.join(words)
print(cuted[:500]) #cell-3
from wordcloud import WordCloud
from wordcloud import ImageColorGenerator #它是直接用来生成一个color_func颜色函数的,它括号里需要一个nd-array多维数组的图像
fontpath='SourceHanSansCN-Regular.otf' import numpy as np
from PIL import Image
aimask=np.array(Image.open("ai-mask.png")) #获取遮罩图片,这个数据应该是nd-array格式,这是一个多维数组格式(N-dimensional Array)。 genclr=ImageColorGenerator(aimask) wc = WordCloud(font_path=fontpath, # 设置字体
background_color="white", # 背景颜色
max_words=1000, # 词云显示的最大词数
max_font_size=100, # 字体最大值
min_font_size=5, #字体最小值
random_state=42, #随机数
collocations=False, #避免重复单词
mask=aimask, #造型遮盖
color_func=genclr,
width=1600,height=1200,margin=2, #图像宽高,字间距,需要配合下面的plt.figure(dpi=xx)放缩才有效
)
wc.generate(cuted) #cell-4
import matplotlib.pyplot as plt
plt.figure(dpi=150) #通过这里可以放大或缩小
plt.imshow(wc, interpolation='catrom',vmax=1000)
plt.axis("off") #隐藏坐标

官方例子

自定义字体颜色:

下段代码来自wordcloud官方的github。

#!/usr/bin/env python
"""
Colored by Group Example
======================== Generating a word cloud that assigns colors to words based on
a predefined mapping from colors to words
基于颜色到单次的映射,将颜色分配给单次,生成词云。
""" from wordcloud import (WordCloud, get_single_color_func)
import matplotlib.pyplot as plt class SimpleGroupedColorFunc(object):
"""Create a color function object which assigns EXACT colors
to certain words based on the color to words mapping
创建一个颜色函数对象,它根据颜色到单词的映射关系,为单词分配精准的颜色。 Parameters
参数
----------
color_to_words : dict(str -> list(str))
A dictionary that maps a color to the list of words. default_color : str
Color that will be assigned to a word that's not a member
of any value from color_to_words.
""" def __init__(self, color_to_words, default_color):
self.word_to_color = {word: color
for (color, words) in color_to_words.items()
for word in words} self.default_color = default_color def __call__(self, word, **kwargs):
return self.word_to_color.get(word, self.default_color) class GroupedColorFunc(object):
"""Create a color function object which assigns DIFFERENT SHADES of
specified colors to certain words based on the color to words mapping. Uses wordcloud.get_single_color_func Parameters
----------
color_to_words : dict(str -> list(str))
A dictionary that maps a color to the list of words. default_color : str
Color that will be assigned to a word that's not a member
of any value from color_to_words.
""" def __init__(self, color_to_words, default_color):
self.color_func_to_words = [
(get_single_color_func(color), set(words))
for (color, words) in color_to_words.items()] self.default_color_func = get_single_color_func(default_color) def get_color_func(self, word):
"""Returns a single_color_func associated with the word"""
try:
color_func = next(
color_func for (color_func, words) in self.color_func_to_words
if word in words)
except StopIteration:
color_func = self.default_color_func return color_func def __call__(self, word, **kwargs):
return self.get_color_func(word)(word, **kwargs) #text是要分析的文本内容
text = """The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!""" # Since the text is small collocations are turned off and text is lower-cased
wc = WordCloud(collocations=False).generate(text.lower()) # 自定义所有单词的颜色
color_to_words = {
# words below will be colored with a green single color function
'#00ff00': ['beautiful', 'explicit', 'simple', 'sparse',
'readability', 'rules', 'practicality',
'explicitly', 'one', 'now', 'easy', 'obvious', 'better'],
# will be colored with a red single color function
'red': ['ugly', 'implicit', 'complex', 'complicated', 'nested',
'dense', 'special', 'errors', 'silently', 'ambiguity',
'guess', 'hard']
} # Words that are not in any of the color_to_words values
# will be colored with a grey single color function
#不属于上述设定的颜色词的词语会用灰色来着色
default_color = 'grey' # Create a color function with single tone
# grouped_color_func = SimpleGroupedColorFunc(color_to_words, default_color) # Create a color function with multiple tones
grouped_color_func = GroupedColorFunc(color_to_words, default_color) # Apply our color function
# 如果你也可以将color_func的参数设置为图片,详细的说明请看 下一部分
wc.recolor(color_func=grouped_color_func) # 画图
plt.figure()
plt.imshow(wc, interpolation="bilinear")
plt.axis("off")
plt.show()

利用背景图片生成词云,设置停用词词集:

该段代码主要来自于wordcloud的github,你同样可以在github下载该例子以及原图片与效果图。wordcloud会把背景图中白色区域去除,只在有色区域进行绘制。

#!/usr/bin/env python
"""
Image-colored wordcloud
======================= You can color a word-cloud by using an image-based coloring strategy
implemented in ImageColorGenerator. It uses the average color of the region
occupied by the word in a source image. You can combine this with masking -
pure-white will be interpreted as 'don't occupy' by the WordCloud object when
passed as mask.
If you want white as a legal color, you can just pass a different image to
"mask", but make sure the image shapes line up.
"""
#导入必要的库
from os import path
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator # Read the whole text.
text = open(r'C:\Users\JluTIger\Desktop\texten.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(r"C:\Users\JluTIger\Desktop\alice.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()
  • 原图

  • 效果:



参考链接

  1. https://www.cnblogs.com/jlutiger/p/9176517.html
  2. https://www.jianshu.com/p/daa54db9045d
  3. https://blog.csdn.net/cy776719526/article/details/80171790
  4. https://www.jianshu.com/p/656c978764cb

Python数据可视化 -- Wordcloud的更多相关文章

  1. python --数据可视化(一)

    python --数据可视化 一.python -- pyecharts库的使用 pyecharts--> 生成Echarts图标的类库 1.安装: pip install pyecharts ...

  2. 【python可视化系列】python数据可视化利器--pyecharts

    学可视化就跟学弹吉他一样,刚开始你会觉得自己弹出来的是噪音,也就有了在使用python可视化的时候,总说,我擦,为啥别人画的图那么溜: [python可视化系列]python数据可视化利器--pyec ...

  3. Python数据可视化编程实战——导入数据

    1.从csv文件导入数据 原理:with语句打开文件并绑定到对象f.不必担心在操作完资源后去关闭数据文件,with的上下文管理器会帮助处理.然后,csv.reader()方法返回reader对象,通过 ...

  4. Python数据可视化——使用Matplotlib创建散点图

    Python数据可视化——使用Matplotlib创建散点图 2017-12-27 作者:淡水化合物 Matplotlib简述: Matplotlib是一个用于创建出高质量图表的桌面绘图包(主要是2D ...

  5. Python数据可视化-seaborn库之countplot

    在Python数据可视化中,seaborn较好的提供了图形的一些可视化功效. seaborn官方文档见链接:http://seaborn.pydata.org/api.html countplot是s ...

  6. Python数据可视化编程实战pdf

    Python数据可视化编程实战(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1vAvKwCry4P4QeofW-RqZ_A 提取码:9pcd 复制这段内容后打开百度 ...

  7. 【数据科学】Python数据可视化概述

    注:很早之前就打算专门写一篇与Python数据可视化相关的博客,对一些基本概念和常用技巧做一个小结.今天终于有时间来完成这个计划了! 0. Python中常用的可视化工具 Python在数据科学中的地 ...

  8. Python数据可视化的四种简易方法

    摘要: 本文讲述了热图.二维密度图.蜘蛛图.树形图这四种Python数据可视化方法. 数据可视化是任何数据科学或机器学习项目的一个重要组成部分.人们常常会从探索数据分析(EDA)开始,来深入了解数据, ...

  9. python 数据可视化

    一.基本用法 import numpy as np import matplotlib.pyplot as plt x = np.linspace(-1,1,50) # 生成-1到1 ,平分50个点 ...

随机推荐

  1. PaperRead - A Shader Framework for Rapid Prototyping of GPU-Based Volume Rendering

    PaperRead - A Shader Framework for Rapid Prototyping of GPU-Based Volume Rendering 目录 PaperRead - A ...

  2. python函数位置实参传参

    #!/usr/bin/python #coding=utf-8 #好好学习,天天向上 def describe_pet(type,name): print(f"i have a {type} ...

  3. Core 3.1 MVC 抛异常“InvalidOperationException: No service for type 'Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory' has been registered.”

    .NET Core 的版本是 3.1遇到的问题是 Action 中 return View() 的时候报错 An unhandled exception occurred while processi ...

  4. 命令行传参是否只能针对main方法

    先上结论 命令行传参只能针对main方法而言,而且格式严格 这里在原有主类test基础上创建了另一个类,名字为testCMD,但里面创建的方法并不是main方法,在cmd内编译后进行命令行传参,结果如 ...

  5. Spring系列8:bean的作用域

    本文内容 bean定义信息的意义 介绍6种bean的作用域 bean定义信息的意义 Spring中区分下类.类定义信息,类实例对象的概念?不容易理解,以餐馆中点炒饭为例. 类: 相当于你看到菜单上炒饭 ...

  6. 洛谷P2240 【深基12.例1】部分背包问题(经典模板)

    首先应该注意几个关键信息:装走尽多的金币.所有金币可以随意分割并且分割后金币的价值比不变; 我们可以抽象成一个买东西的场景,给你一定数目的钱,让你在这些钱内尽量在超市买更多的小零食,这就牵扯到了一个性 ...

  7. c#代码设计:子类和父类

    哭辽,事情是这样的 我想写个产品类用来放点相机参数,想类似这种的使用方式:(时间关系不改了,产品=Zoo,animals=相机) Zoo Zooxx= new Zoo (); Zoo.Animals ...

  8. Shell中退出状态码exit

    shell中运行的每个命令都使用退出状态码(exit status)来告诉shell它完成了处理.退出状态码是一个0~255之间的整数值,在命令结束时由命令传回shell. 1 .查看退出状态码 Li ...

  9. Android利用zxing生成二维码

    感谢大佬:https://blog.csdn.net/mountain_hua/article/details/80646089 **gayhub上的zxing可用于生成二维码,识别二维码 gayhu ...

  10. 【转】Python中自定义可迭代对象

    python 中内置的可迭代的对象有 list.tuple.set.dict 等,那么我们自己怎么定义一个可迭代的对象呢?先来段代码吧 import re import reprlib RE_WORD ...