词云是一种非常漂亮的可视化展示方式,正所谓一图胜过千言万语,词云在之前的项目中我也有过很多的使用,可能对于我来说,一种很好的自我介绍方式就是词云吧,就像下面这样的:

个人觉还是会比枯燥的文字语言描述性的介绍会更吸引人一点吧。

今天不是说要怎么用词云来做个人介绍,而是对工作中使用到比较多的词云计较做了一下总结,主要是包括三个方面:

1、诸如上面的简单形式矩形词云

2、基于背景图片数据来构建词云数据

3、某些场景下不想使用类似上面的默认的字体颜色,这里可以自定义词云的字体颜色

接下来对上面三种类型的词云可视化方法进行demo实现与展示,具体如下:

这里我们使用到的测试数据如下:

The Zen of Python, by Tim PetersBeautiful 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 text 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!

1、简单形式矩形词云实现如下:

def simpleWC1(sep=' ',back='black',freDictpath='data_fre.json',savepath='res.png'):'''词云可视化Demo'''try:with open(freDictpath) as f:data=f.readlines()data_list=[one.strip().split(sep) for one in data if one]fre_dict={}for one_list in data_list:fre_dict[unicode(one_list[0])]=int(one_list[1])except:fre_dict=freDictpathwc=WordCloud(font_path='font/simhei.ttf',#设置字体  #simheibackground_color=back, #背景颜色max_words=1300,# 词云显示的最大词数max_font_size=120, #字体最大值margin=3,  #词云图边距width=1800,  #词云图宽度height=800,  #词云图高度random_state=42)wc.generate_from_frequencies(fre_dict)  #从词频字典生成词云plt.figure()  plt.imshow(wc)plt.axis("off")wc.to_file(savepath)

图像数据结果如下:

2、 基于背景图像数据的词云可视化具体实现如下:

先贴一下背景图像:

这也是一个比较经典的图像数据了,下面来看具体的实现:

def simpleWC2(sep=' ',back='black',backPic='a.png',freDictpath='data_fre.json',savepath='res.png'):'''词云可视化Demo【使用背景图片】'''try:with open(freDictpath) as f:data=f.readlines()data_list=[one.strip().split(sep) for one in data if one]fre_dict={}for one_list in data_list:fre_dict[unicode(one_list[0])]=int(one_list[1])except:fre_dict=freDictpathback_coloring=imread(backPic)wc=WordCloud(font_path='simhei.ttf',#设置字体  #simheibackground_color=back,max_words=1300,mask=back_coloring,#设置背景图片max_font_size=120, #字体最大值margin=3,width=1800,height=800,random_state=42,)wc.generate_from_frequencies(fre_dict)  #从词频字典生成词云wc.to_file(savepath)

结果图像数据如下:

3、 自定义词云字体颜色的具体实现如下:

#自定义颜色列表
color_list=['#CD853F','#DC143C','#00FF7F','#FF6347','#8B008B','#00FFFF','#0000FF','#8B0000','#FF8C00','#1E90FF','#00FF00','#FFD700','#008080','#008B8B','#8A2BE2','#228B22','#FA8072','#808080']def simpleWC3(sep=' ',back='black',freDictpath='data_fre.json',savepath='res.png'):'''词云可视化Demo【自定义字体的颜色】'''#基于自定义颜色表构建colormap对象colormap=colors.ListedColormap(color_list)  try:with open(freDictpath) as f:data=f.readlines()data_list=[one.strip().split(sep) for one in data if one]fre_dict={}for one_list in data_list:fre_dict[unicode(one_list[0])]=int(one_list[1])except:fre_dict=freDictpathwc=WordCloud(font_path='font/simhei.ttf',#设置字体  #simheibackground_color=back,  #背景颜色max_words=1300,  #词云显示的最大词数max_font_size=120,  #字体最大值colormap=colormap,  #自定义构建colormap对象margin=2,width=1800,height=800,random_state=42,prefer_horizontal=0.5)  #无法水平放置就垂直放置wc.generate_from_frequencies(fre_dict)plt.figure()  plt.imshow(wc)plt.axis("off")wc.to_file(savepath)

结果图像数据如下:

上述三种方法就是我在具体工作中使用频度最高的三种词云可视化展示方法了,下面贴出来完整的代码实现,可以直接拿去跑的:

#!usr/bin/env python
#encoding:utf-8
from __future__ import division'''
__Author__:沂水寒城
功能: 词云的可视化模块
'''import os
import sys
import json
import numpy as np
from PIL import Image
from scipy.misc import imread
from matplotlib import colors
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
from wordcloud import WordCloud,ImageColorGenerator,STOPWORDSreload(sys)
sys.setdefaultencoding('utf-8')#自定义颜色列表
color_list=['#CD853F','#DC143C','#00FF7F','#FF6347','#8B008B','#00FFFF','#0000FF','#8B0000','#FF8C00','#1E90FF','#00FF00','#FFD700','#008080','#008B8B','#8A2BE2','#228B22','#FA8072','#808080']def simpleWC1(sep=' ',back='black',freDictpath='data_fre.json',savepath='res.png'):'''词云可视化Demo'''try:with open(freDictpath) as f:data=f.readlines()data_list=[one.strip().split(sep) for one in data if one]fre_dict={}for one_list in data_list:fre_dict[unicode(one_list[0])]=int(one_list[1])except:fre_dict=freDictpathwc=WordCloud(font_path='font/simhei.ttf',#设置字体  #simheibackground_color=back, #背景颜色max_words=1300,# 词云显示的最大词数max_font_size=120, #字体最大值margin=3,  #词云图边距width=1800,  #词云图宽度height=800,  #词云图高度random_state=42)wc.generate_from_frequencies(fre_dict)  #从词频字典生成词云plt.figure()  plt.imshow(wc)plt.axis("off")wc.to_file(savepath)def simpleWC2(sep=' ',back='black',backPic='a.png',freDictpath='data_fre.json',savepath='res.png'):'''词云可视化Demo【使用背景图片】'''try:with open(freDictpath) as f:data=f.readlines()data_list=[one.strip().split(sep) for one in data if one]fre_dict={}for one_list in data_list:fre_dict[unicode(one_list[0])]=int(one_list[1])except:fre_dict=freDictpathback_coloring=imread(backPic)wc=WordCloud(font_path='simhei.ttf',#设置字体  #simheibackground_color=back,max_words=1300,mask=back_coloring,#设置背景图片max_font_size=120, #字体最大值margin=3,width=1800,height=800,random_state=42,)wc.generate_from_frequencies(fre_dict)  #从词频字典生成词云wc.to_file(savepath)def simpleWC3(sep=' ',back='black',freDictpath='data_fre.json',savepath='res.png'):'''词云可视化Demo【自定义字体的颜色】'''#基于自定义颜色表构建colormap对象colormap=colors.ListedColormap(color_list)  try:with open(freDictpath) as f:data=f.readlines()data_list=[one.strip().split(sep) for one in data if one]fre_dict={}for one_list in data_list:fre_dict[unicode(one_list[0])]=int(one_list[1])except:fre_dict=freDictpathwc=WordCloud(font_path='font/simhei.ttf',#设置字体  #simheibackground_color=back,  #背景颜色max_words=1300,  #词云显示的最大词数max_font_size=120,  #字体最大值colormap=colormap,  #自定义构建colormap对象margin=2,width=1800,height=800,random_state=42,prefer_horizontal=0.5)  #无法水平放置就垂直放置wc.generate_from_frequencies(fre_dict)plt.figure()  plt.imshow(wc)plt.axis("off")wc.to_file(savepath)if __name__ == '__main__':text="""The Zen of Python, by Tim PetersBeautiful 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 text 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!"""word_list=text.split()fre_dict={}for one in word_list:if one in fre_dict:fre_dict[one]+=1else:fre_dict[one]=1simpleWC1(sep=' ',back='black',freDictpath=fre_dict,savepath='simpleWC1.png')simpleWC2(sep=' ',back='black',backPic='backPic/A.png',freDictpath=fre_dict,savepath='simpleWC2.png')simpleWC3(sep=' ',back='black',freDictpath=fre_dict,savepath='simpleWC3.png')

记录一下,欢迎交流学习。

python词云可视化方法总结记录【简单词云+背景图片词云+自定义字体颜色词云】相关推荐

  1. python定义字体颜色_windows print 自定义字体颜色【python】

    windows print 自定义字体颜色 import ctypes STD_INPUT_HANDLE = -10 STD_OUTPUT_HANDLE= -11 STD_ERROR_HANDLE = ...

  2. python绘制梅尔谱图_3D星空图V2版——添加背景图片和音乐

    阿黎逸阳   精选Python.SQL.R.MATLAB等相关知识,让你的学习和工作更出彩(可提供风控建模干货经验). 还记得之前发表的3D星空图吗?一直想给喜欢的星空图加上背景图片和音乐. 经过研究 ...

  3. Python 几种可视化方法随笔

    Python matplotlib可视化随笔 plt.figure plt.figure来设置窗口尺寸.其中figsize用来设置图形的大小,a为图形的宽, b为图形的高,单位为英寸. plt.fig ...

  4. java 生成纯色图片_canvas简单实现纯色背景图片抠图(示例代码)

    最近在研究html5 canvas的过程中,发现,canvas为前端对图像的处理开辟了一条新的道路,canvas可以做到很多事情,甚至可以做个类似于PhotoShop的东西,曾经本人在一家软件工作就做 ...

  5. canvas简单实现纯色背景图片抠图

    最近在研究html5 canvas的过程中,发现,canvas为前端对图像的处理开辟了一条新的道路,canvas可以做到很多事情,甚至可以做个类似于PhotoShop的东西,曾经本人在一家软件工作就做 ...

  6. python实战:分分钟上手爬虫获取风景与美女背景图片

    电脑桌面背景好久没换了,心血来潮突然想换一下,可系统自带的又没几张看中的,咋办?网上爬呀.论爬虫编程,应该没有比python更简洁的了,学编写软件为的就是应用,哪个快捷哪个高效就用哪个,实用主义. 于 ...

  7. 文本数据的词云可视化

    1.使用python实现文本数据的词云可视化,阐述词云原理,数据源可以自行选定: 2.实现文本数据的one-hot编码.Bag-of-word模型,将过程阐述清楚,数据源可以自行选定: 词云可视化 随 ...

  8. 下c语言实现wc_用 Python 实现词云可视化

    词云是一种非常漂亮的可视化展示方式,正所谓一图胜过千言万语,词云在之前的项目中我也有过很多的使用,可能对于我来说,一种很好的自我介绍方式就是词云吧,就像下面这样的: 个人觉还是会比枯燥的文字语言描述性 ...

  9. python英文词云代码_使用python实现个性化词云的方法

    先上图片 词云图 需要模板 pip install jieba pip install wordcloud 还需要安装另外两个东西这两个我也不太懂借鉴百度写上去的 pip install scipy ...

  10. python点云可视化工具_救命!点云可视化(不需配置PCL)

    分享一波简单的可视化bin格式点云的方法. 先大概讲下背景,目前激光雷达采集的点云文件大多基于ROS,然后转化为pcd.bin格式进行处理.(ros-pcd-bin格式之间相互转换格式有相应的工具,如 ...

最新文章

  1. 安装搭建kafka集群
  2. 敲诈英伟达的竟然是一群未成年???
  3. 机器学习-集成学习:随机森林(Random Forest)
  4. zkServer.cmd 闪退
  5. Codeforces Gym 100286I iSharp 模拟
  6. 经典卷积神经网络的学习(二)—— VGGNet
  7. win10今日热点弹窗怎么删除_“今日热点”怎么去掉,每次弹窗很烦!是哪个软件附带的,我想卸载了!谢谢有心人!~~...
  8. 考研助手软件制作记录
  9. 360无线wifi路由器连接到服务器,luyou.360.cn如何登录360路由器
  10. jdbc,基本数据库命令封装
  11. C#查找Excel重复值(多列)
  12. 亿起发网站主获取CPS数据接口说明
  13. signature=79628ed6ceb6a7f7dd3c2bec70171b45,Hvad kortene kan fortælle om kultur- og naturlandskabet
  14. wr720n刷成网络打印_wr720n v4 折腾笔记(一):安装Openwrt
  15. 一篇长文叙述Linux内核虚拟地址空间的基本概括
  16. 数据采集 - 笔记 2
  17. java地铁最短距离_地铁线路最短路径问题
  18. 一文讲清chatGPT的发展历程、能力来源和复现它的关键之处
  19. matlab disparity函数
  20. Stooge-sort

热门文章

  1. svn 迁移到git下全过程
  2. vue app准备学习工作
  3. 排序算法速度测试(插入排序、二分法插入、选择排序、快速排序、堆排序)js实现...
  4. shell编程-条件判断与流程控制
  5. 使用GDB进行系统调用过程简析
  6. hadoop之Combiner
  7. 怎么使用PVS stream Linux
  8. MySQL索引的使用及注意事项
  9. freemarker 标签循环list 以及获取下标值
  10. 最小上界sup(来自wiki)