python制作词云时出现figure1 figure2_用Python生成词云
词云是一种数据可视化技术,用于表示文本数据,其中每个单词的大小表示其出现的频率或重要性。 可以使用词云突出显示重要的文本数据点。 词云被广泛用于分析来自社交网络网站的数据。
为了在Python中生成词云,需要的模块是– matplotlib,pandas和wordcloud。 要安装这些软件包,请运行以下命令:
pip install matplotlib
pip install pandas
pip install wordcloud
代码1:字数
可以设置要在tagcloud上显示的最大单词数。 为此,请使用WordCloud()函数的max_words关键字参数。
# importing the necessery modules
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import csv
# file object is created
file_ob = open(r"linuxidc.com.csv")
# reader object is created
reader_ob = csv.reader(file_ob)
# contents of reader object is stored .
# data is stored in list of list format.
reader_contents = list(reader_ob)
# empty string is declare
text = ""
# iterating through list of rows
for row in reader_contents :
# iterating through words in the row
for word in row :
# concatenate the words
text = text + " " + word
# show only 10 words in the wordcloud .
wordcloud = WordCloud(width=480, height=480, max_words=10).generate(text)
# plot the WordCloud image
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.margins(x=0, y=0)
plt.show()
输出如下图:
代码2:删除一些单词
可以删除一些我们不想显示的词。 为此,请将这些单词传递给WordCloud()函数的停用词列表参数。
# importing the necessery modules
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import csv
# file object is created
file_ob = open(r"linuxidc.com.csv")
# reader object is created
reader_ob = csv.reader(file_ob)
# contents of reader object is stored .
# data is stored in list of list format.
reader_contents = list(reader_ob)
# empty string is declare
text = ""
# iterating through list of rows
for row in reader_contents :
# iterating through words in the row
for word in row :
# concatenate the words
text = text + " " + word
# remove Python , Matplotlib , Geeks Words from WordCloud .
wordcloud = WordCloud(width=480, height=480,
stopwords=["Python", "Matplotlib","Geeks"]).generate(text)
# plot the WordCloud image
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.margins(x=0, y=0)
plt.show()
输出效果如下:
代码3:更改背景
我们可以更改wordcloud背景的颜色。 为此,请使用WordCloud()函数的background_color关键字参数。
# importing the necessery modules
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import csv
# file object is created
file_ob = open(r"linuxidc.com.csv")
# reader object is created
reader_ob = csv.reader(file_ob)
# contents of reader object is stored .
# data is stored in list of list format.
reader_contents = list(reader_ob)
# empty string is declare
text = ""
# iterating through list of rows
for row in reader_contents :
# iterating through words in the row
for word in row :
# concatenate the words
text = text + " " + word
wordcloud = WordCloud(width=480, height=480, background_color="pink").generate(text)
# plot the WordCloud image
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.margins(x=0, y=0)
plt.show()
输出效果如下:
代码4:更改单词的颜色
我们可以使用WordCloud()函数的colormap关键字参数来更改单词的颜色。
# importing the necessery modules
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import csv
# file object is created
file_ob = open(r"linuxidc.com.csv")
# reader object is created
reader_ob = csv.reader(file_ob)
# contents of reader object is stored .
# data is stored in list of list format.
reader_contents = list(reader_ob)
# empty string is declare
text = ""
# iterating through list of rows
for row in reader_contents :
# iterating through words in the row
for word in row :
# concatenate the words
text = text + " " + word
wordcloud = WordCloud(width=480, height=480, colormap="Oranges_r").generate(text)
# plot the WordCloud image
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.margins(x=0, y=0)
plt.show()
输出效果如下:
代码5:设置最大和最小字体
我们可以控制wordcloud的最小和最大字体大小。 为此,请使用WordCloud()函数的max_font_size和min_font_size关键字参数。
# importing the necessery modules
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import csv
# file object is created
file_ob = open(r"linuxidc.com.csv")
# reader object is created
reader_ob = csv.reader(file_ob)
# contents of reader object is stored .
# data is stored in list of list format.
reader_contents = list(reader_ob)
# empty string is declare
text = ""
# iterating through list of rows
for row in reader_contents :
# iterating through words in the row
for word in row :
# concatenate the words
text = text + " " + word
wordcloud = WordCloud(width=480, height=480, max_font_size=20, min_font_size=10).generate(text)
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.margins(x=0, y=0)
plt.show()
OK,暂时先这样,中文乱码解决等请继续关注我们Linux公社的Python专题栏目,谢谢阅读。
python制作词云时出现figure1 figure2_用Python生成词云相关推荐
- python制作词云时出现figure1 figure2_「中文版」如何在Python中生成任何形状的词云...
有机会介绍一些有趣的项目,向更多人甚至是非技术人员推广Python和人工智能的东西还是蛮有意义的.话不多说,本文将介绍如何将你喜欢的文章或者歌词做一个炫丽的词云图片. 什么是词云呢?简单来说就是,将提 ...
- Python + wordcloud + jieba 十分钟学会用任意中文文本生成词云
前述 本文需要的两个Python类库 jieba:中文分词分词工具 wordcloud:Python下的词云生成工具 写作本篇文章用时一个小时半,阅读需要十分钟,读完该文章后你将学会如何将任意中文 ...
- Python爬取唐人街探案3豆瓣短评并生成词云
爬取唐人街探案3短评过程 要爬取的URL: https://movie.douban.com/subject/27619748/comments?start=20&limit=20&s ...
- 爬虫数据云词图片怎么做?小姐姐教你用python做B站弹幕爬虫,并进行数据分析生成词云
hello大家好,我是你们的可爱丸,大家平时在B站看视频时有没有开弹幕的习惯呢?如果不把视频从头看到尾,那么多弹幕,我们怎么快速的知道大家都说了些什么并且持有什么观点呢? 今天小姐姐就教你做一个简单的 ...
- python爬取网易云音乐薛之谦歌词数据,生成词云
老薛最近频频上热搜,因为老薛的歌大部分是自己作词,所以感觉他的歌词和他应该有某种情感表达和联系吧. 于是用python爬了网易云音乐中老薛的歌词数据,并简单用wordcloud写了个词云统计. 难过, ...
- Python单线程爬取QQ空间说说存入MySQL并生成词云(超详细讲解,踩坑经历)
利用python爬取好友说说并分析 看了网上的许多博客,基本上都是一个样,基本的知识也没详细解释,我这次也想跟大家仔细分析一下,自己还是要有一定爬虫基础,本人技术有限,如果本文哪有错误或不够准确的地方 ...
- matlab可以使用词云分析吗,利用豆瓣短评数据生成词云
在之前的文章中,我们获得了豆瓣爬取的短评内容,汇总到了一个文件中,但是,没有被利用起来的数据是没有意义的. 前文提到,有一篇微信推文的关于词云制作的一个实践记录,准备照此试验一下. 思路分析 读文件 ...
- 如何用Python 制作词云-对1000首古诗做词云分析
主页: https://codeshellme.github.io 今天来介绍一下如何使用 Python 制作 词云 . 词云又叫文字云,它可以统计文本中频率较高的词,并将这些词可视化,让我们可以直观 ...
- 使用python生成词云——聆心云心理健康服务平台数据可视分析和可视化
实验题目:聆心云心理健康服务平台数据可视分析和可视化 实验目的和要求:统计出在聆心云平台做沙盘游戏的次数.根据各次沙盘游戏所使用的沙具和进行的操作数据进行词云可视化,掌握Python词云制作方法 实验 ...
最新文章
- cdr放大后内容消失了_放大镜+定时器+画笔,582KB软件就能做到,上网课的你一定能用上...
- kernel vim阅读 设置tags的标签
- 大年30还多少天_2020余额不足30天!年初许下的愿望你实现了多少?
- IBM在人工智能方面的新进展,理解谈话情景和感知情绪
- 剑指offer:约瑟夫环的问题
- 递归函数实现二分查找法
- 《架构之美》阅读笔记四
- creator图片循环显示_Cocos creator 游戏背景循环滚动--Javascript编写
- Spring 源码解析 -- SpringWeb请求映射解析
- 使用 .Net Memory Profiler 诊断 .NET 应用内存泄漏(方法与实践)
- 新手上路,Python黑帽子学习笔记从此开篇
- mysql jemalloc_MySQL性能测试--jemalloc内存管理
- 怎样启动本地mysql服务_启动本地mysql服务
- 产品设计:Material Design 学习笔记一
- 应届生面试自我介绍该怎么说?
- Flixel横板游戏制作教程(九)—SquashingthePlayer(挤压Player)
- Android获取WIFI 的ssid 方法适配Android9.0
- 重磅!腾讯优图29篇论文入选顶会ECCV 2022
- 【Python 数据科学】Numpy和Pandas基础
- ps把白底图片改为透明
热门文章
- 在CentOS 6.3 64bit下yum安装MySQL后重置root密码的方法
- ceph-kvstore-tool 工具使用详解
- Springboot+mybatisplus搭建新闻管理系统
- springboot整合Quartz实现动态配置定时任务
- [转载]SSH框架搭建详细图文教程
- jQuery中$(function(){})与(function($){})(jQuery)的区别
- Hide the common top menu in Ubuntu 12.04
- python利用wx.grid网格显示数据
- OpenCV 【十八】图像平滑处理/腐蚀与膨胀(Eroding and Dilating)/开闭运算,形态梯度,顶帽,黑帽运算
- Flink在美团的应用与实践听课笔记