python输出字体的大小_好玩又使用,使用Python生成漂亮的词云

词云是一种数据可视化技术,用于表示文本数据,其中每个单词的大小表示其出现的频率或重要性。可以使用词云突出显示重要的文本数据点。词云被广泛用于分析来自社交网络网站的数据。 为了在Python中生成词云,需要的模块是– matplotlib,pandas和wordcloud。要安装这些软件包,请运行以下命令: pip install matplotlib
pip install pandas
pip install wordcloud 代码1:字数 可以设置要在tagcloud上显示的最大单词数。为此,请使用WordCloud()函数的max_words关键字参数。
# importing the necessery modulesfrom wordcloud import WordCloudimport matplotlib.pyplot as pltimport 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 rowsfor row in reader_contents :# iterating through words in the rowfor 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()
输出如下图: cd96a9cd1c1cf3edbe26fecf08cef8ff.png 代码2:删除一些单词 可以删除一些我们不想显示的词。为此,请将这些单词传递给WordCloud()函数的停用词列表参数。
# importing the necessery modulesfrom wordcloud import WordCloudimport matplotlib.pyplot as pltimport 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 rowsfor row in reader_contents :# iterating through words in the rowfor 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()
输出效果如下: 23d843edccded6bcaab4281833ec3955.png 代码3:更改背景 我们可以更改wordcloud背景的颜色。为此,请使用WordCloud()函数的background_color关键字参数。
# importing the necessery modulesfrom wordcloud import WordCloudimport matplotlib.pyplot as pltimport 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 rowsfor row in reader_contents :# iterating through words in the rowfor 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()
输出效果如下: 71c35b75a99b097ef8fe256ecc44ea7b.png 代码4:更改单词的颜色 我们可以使用WordCloud()函数的colormap关键字参数来更改单词的颜色。
# importing the necessery modulesfrom wordcloud import WordCloudimport matplotlib.pyplot as pltimport 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 rowsfor row in reader_contents :# iterating through words in the rowfor 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()
输出效果如下: 1e82f12a031ac35c2ea8ee42277e8f20.png 代码5:设置最大和最小字体 我们可以控制wordcloud的最小和最大字体大小。为此,请使用WordCloud()函数的max_font_size和min_font_size关键字参数。
# importing the necessery modulesfrom wordcloud import WordCloudimport matplotlib.pyplot as pltimport 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 rowsfor row in reader_contents :# iterating through words in the rowfor 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()
8b68dbe7150f63bfc32705e1fcbc6ff8.png OK,暂时先这样,中文乱码解决等请继续关注我们Linux公社的Python专题栏目,谢谢阅读。 304c5fdc93cd416598f19e4b92496efc.png 商务合作联系:root@linuxidc.net长按或扫描左图识别二维码关注Linux公社公众微信号 更多Python相关信息见Python 专题页面 https://www.linuxidc.com/topicnews.aspx?tid=17
Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx

本文永久更新链接地址:https://www.linuxidc.com/Linux/2019-12/161681.htm

42da6755e108a0f24cf60b7951c775d9.gif 2e525c76908d5b8f56bbaccead0e21de.png 支持就点下在看并 转发朋友圈吧 42da6755e108a0f24cf60b7951c775d9.gif
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值