博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python利用WordCount实现词云
阅读量:4230 次
发布时间:2019-05-26

本文共 2350 字,大约阅读时间需要 7 分钟。

前言

构造词云的方法很多,python提供了一个方便的WordCount库来实现了词云以及数据可视化的方法,我们可以方便的得到以下的结果。

参考文档:
- 关键词的视觉化描述;
- 图形可视化,标签化。
- 用于汇总用户生成的标签或一个网站的文字内容; 重要程度能通过改变字体大小或颜色来表现
- 大多数标签本身就是超级链接,直接指向与标签相联的一系列条目。

附:

提供安装各种三方库的方法
1 安装pip
参考网址
2 下载 python 三方库 .whl文件
并将文件放置在Scripts目录下
3 dos界面
Scripts目录下
pip install wheel
pip install xxx.whl

基础词云的产生Minimal Example

# 导入相关的库from os import pathimport matplotlib.pyplot as pltfrom wordcloud import WordCloud# 获取当前文件路径d = path.dirname('.')# path.dirname(__file__) 可以输出该脚本的完整路径,在ide中运行此行会报错# Read the whole text.# os.path.join(当下路径,文件名字) 作用是将路径和文件名合成一个路径,适合源代码和元数据放置一起text = open(path.join(d, 'wordcount_test.txt')).read()# 若文件另外放置,则看可以直接Open到它的路径text = open('F:/wordcount_test.txt',"r").read()# Generate a word cloud imagewc = WordCloud().generate(text)# 绘图# matplotlib.pyplot.imshow(X, cmap=None)# X:图片   cmap:颜色图谱,默认绘制RGB颜色空间plt.imshow(wc)# 不显示坐标尺寸plt.axis("off")# 显示窗口,只有窗口关闭才执行后续的工作plt.show()# 保存图片wc.to_file("test_1.png")   #存储在源代码处wc.to_file("F:/python/test_soures/test_2.png")

这里写图片描述

Image-colored wordcloud

采用基于图像的着色策略为词云着色,通过图片词云形状

from os import pathfrom scipy.misc import imreadimport matplotlib.pyplot as pltfrom wordcloud import WordCloud,STOPWORDS,ImageColorGenerator# Read the pathd = path.dirname('.')# Read the whole text.# os.path.join(当下路径,文件名字) 作用是将路径和文件名合成一个路径,适合源代码和元数据放置一起text = open(path.join(d, 'wordcount_test.txt')).read()# 若文件另外放置,则看可以直接Open到它的路径text = open('F:/wordcount_test.txt',"r").read()# 加载背景图片RGB_coloring = imread(path.join(d, 'F:/python/111.jpg'))# 设置参数:背景颜色,词云显示的最大词数,设置背景图片,字体最大值wc = WordCloud(background_color="white",  # 设置词云背影色,               mask=RGB_coloring,         # 设置词云轮廓               max_words=2000,            # 设置最大现实的字数               stopwords=STOPWORDS.add("said"),                 # 设置停用词,就是屏蔽的词               max_font_size=40,       # 设置字体最大值               random_state=42)                                 # 设置有多少种随机生成状态,即有多少种配色方案# generate word cloud 产生词云wc.generate(text)# 输出原图plt.figure(0)plt.imshow(RGB_coloring)plt.axis("off")plt.show()# 绘制一号词云plt.figure(1)   # plt.figure() 多图标号plt.imshow(wc)plt.axis("off")plt.show()# 绘制2号词云# 从背景图片生成颜色值 create coloring from imageimage_colors = ImageColorGenerator(RGB_coloring)# give color_func=image_colors directly in the constructor to recolor wordcloudplt.figure(2)plt.imshow(wc.recolor(color_func=image_colors))plt.axis("off")plt.show()

输出结果:

这里写图片描述

这里写图片描述

这里写图片描述

你可能感兴趣的文章
Beginning SharePoint with Excel: From Novice to Professional
查看>>
eBay Business All-in-One Desk Reference For Dummies (For Dummies (Business & Personal Finance))
查看>>
Beginning Java Objects: From Concepts To Code, Second Edition
查看>>
Quality of Service Control in High-Speed Networks
查看>>
OFDM Wireless LANs: A Theoretical and Practical Guide
查看>>
Computer Science Handbook, Second Edition
查看>>
TCP/IP Analysis and Troubleshooting (Spiral-bound)
查看>>
Designing Embedded Hardware [ILLUSTRATED]
查看>>
An Introduction to Network Programming with Java
查看>>
C++ Programming for the Absolute Beginner
查看>>
Excel 2007 Bible
查看>>
Logical Foundations for Rule-Based Systems
查看>>
TCP/IP Unleashed (3rd Edition)
查看>>
Linux Database Bible (Bible (Wiley))
查看>>
Quality Measures in Data Mining
查看>>
Visual Basic .NET Black Book
查看>>
Cisco IOS Cookbook
查看>>
Linux Administrator Street Smarts: A Real World Guide to Linux Certification Skills
查看>>
Rapid Web Applications with TurboGears: Using Python to Create Ajax-Powered Sites
查看>>
The Book of JavaScript, 2nd Edition: A Practical Guide to Interactive Web Pages
查看>>