词云是一种数据可视化技术,用于表示文本数据,其中每个单词的大小表示其出现的频率或重要性。 可以使用词云突出显示重要的文本数据点。 词云被广泛用于分析来自社交网络网站的数据。

为了在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生成词云相关推荐

  1. python制作词云时出现figure1 figure2_「中文版」如何在Python中生成任何形状的词云...

    有机会介绍一些有趣的项目,向更多人甚至是非技术人员推广Python和人工智能的东西还是蛮有意义的.话不多说,本文将介绍如何将你喜欢的文章或者歌词做一个炫丽的词云图片. 什么是词云呢?简单来说就是,将提 ...

  2. Python + wordcloud + jieba 十分钟学会用任意中文文本生成词云

    前述 本文需要的两个Python类库  jieba:中文分词分词工具  wordcloud:Python下的词云生成工具 写作本篇文章用时一个小时半,阅读需要十分钟,读完该文章后你将学会如何将任意中文 ...

  3. Python爬取唐人街探案3豆瓣短评并生成词云

    爬取唐人街探案3短评过程 要爬取的URL: https://movie.douban.com/subject/27619748/comments?start=20&limit=20&s ...

  4. 爬虫数据云词图片怎么做?小姐姐教你用python做B站弹幕爬虫,并进行数据分析生成词云

    hello大家好,我是你们的可爱丸,大家平时在B站看视频时有没有开弹幕的习惯呢?如果不把视频从头看到尾,那么多弹幕,我们怎么快速的知道大家都说了些什么并且持有什么观点呢? 今天小姐姐就教你做一个简单的 ...

  5. python爬取网易云音乐薛之谦歌词数据,生成词云

    老薛最近频频上热搜,因为老薛的歌大部分是自己作词,所以感觉他的歌词和他应该有某种情感表达和联系吧. 于是用python爬了网易云音乐中老薛的歌词数据,并简单用wordcloud写了个词云统计. 难过, ...

  6. Python单线程爬取QQ空间说说存入MySQL并生成词云(超详细讲解,踩坑经历)

    利用python爬取好友说说并分析 看了网上的许多博客,基本上都是一个样,基本的知识也没详细解释,我这次也想跟大家仔细分析一下,自己还是要有一定爬虫基础,本人技术有限,如果本文哪有错误或不够准确的地方 ...

  7. matlab可以使用词云分析吗,利用豆瓣短评数据生成词云

    在之前的文章中,我们获得了豆瓣爬取的短评内容,汇总到了一个文件中,但是,没有被利用起来的数据是没有意义的. 前文提到,有一篇微信推文的关于词云制作的一个实践记录,准备照此试验一下. 思路分析 读文件 ...

  8. 如何用Python 制作词云-对1000首古诗做词云分析

    主页: https://codeshellme.github.io 今天来介绍一下如何使用 Python 制作 词云 . 词云又叫文字云,它可以统计文本中频率较高的词,并将这些词可视化,让我们可以直观 ...

  9. 使用python生成词云——聆心云心理健康服务平台数据可视分析和可视化

    实验题目:聆心云心理健康服务平台数据可视分析和可视化 实验目的和要求:统计出在聆心云平台做沙盘游戏的次数.根据各次沙盘游戏所使用的沙具和进行的操作数据进行词云可视化,掌握Python词云制作方法 实验 ...

最新文章

  1. 架构师之路 — 分布式系统 — CAP 定理
  2. 简书显示服务器错误,openfire服务器异常处理
  3. 拓扑学+计算机,吴国平: 拓扑学到底有多重要? 在数学中占据多高的地位?
  4. JavaScript命名空间
  5. 成为Google认证机器学习工程师,零基础只需要2步?
  6. html css做网页总结,学习CSS制作网页总结的一些经验
  7. 问题:自定义Appender输出DCMTK的oflog
  8. C#-WebForm-JS知识:基础部分、BOM部分、DOM部分、JS事件
  9. 3d ListView翻译
  10. 手机linux服务器控制,手机上如何远程控制Linux服务器?
  11. python绘制国内生产总值散点图_Python学习第90课-数据可视化之散点图绘制
  12. 详解torch.view()的-1参数是什么意思
  13. 解决IE浏览器低版本兼容性问题的最快方法
  14. 2417. Loan Repayment
  15. 阿里云服务器优惠活动中的突发性/共享型/计算型/通用型实例怎么选
  16. 清华大学海洋大数据分析管理平台解读
  17. QQ 不也有语音功能吗,为什么微信会有市场?
  18. 阿里云服务器通用型实例g5 怎么样好不好?性能配置分析
  19. FinancesOnline 2022/2023 10大IT趋势预测
  20. 使用HuggingFace实现 DiffEdit论文的掩码引导语义图像编辑

热门文章

  1. 基于C++CUDA实现全景图(2:1 Equirectangular Image)转换为天空盒图像
  2. 基于Sobel计算图像梯度图
  3. Debevec方法计算相机响应函数CRF
  4. c++ make_pairpair
  5. 【微信小程序】之如何创建底部菜单?tabBar、mp-tabbar
  6. JS实现每隔几个字符添加字符(串):实现每间隔10个字就换行一次,多用于echarts横坐标的显示文本拥挤换行;实现间隔8个字符就添加❤❤
  7. 计算机网络基础 1.0 -- 概述
  8. 项目微管理29 - 转正
  9. BigdCIMAL类型数据的使用选择
  10. idea崩溃导致的svn插件丢失问题, maven dependencies视图丢失问题