17 wordcloud库的使用

一、wordcloud库基本介绍

1.wordcloud库概述

wordcloud是优秀的词云展示第三方库

2.wordcloud库的安装 

(cmd命令行)pip install wordcloud

3.wordcloud库的使用说明 

4.wordcloud库常规方法

 5.wordcloud应用示例

EG1:英文

import wordcloud
txt=open("hamlet.txt","r").read()
txt=txt.lower()
for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~':
    txt=txt.replace(ch," ")

w=wordcloud.WordCloud(background_color="white",width=600,height=400,min_font_size=10,max_font_size=100,max_words=30)
w.generate(txt)
w.to_file("hamlet.png")

 

 EG2:中文

import jieba
import wordcloud
txt=open("C:\\Users\\吉祥瑞\\Desktop\\python\\UNIT 6\\threekingdoms.txt","r",\
         encoding="UTF-8").read()
txts=jieba.lcut(txt)
w=wordcloud.WordCloud(background_color="white",width=600,height=400,\
                      min_font_size=10,max_font_size=100,max_words=100,\
                      font_path="msyh.ttc")
w.generate(" ".join(txts))
w.to_file("threekingdoms.png")

 

 去掉一些词汇

import jieba
import wordcloud
txt=open("C:\\Users\\吉祥瑞\\Desktop\\python\\UNIT 6\\threekingdoms.txt","r",\
         encoding="UTF-8").read()
txt=jieba.lcut(txt)#txt是list
a=[]
w=wordcloud.WordCloud(background_color="white",width=600,height=400,\
                      min_font_size=10,max_font_size=100,max_words=100,\
                      font_path="msyh.ttc",\
                      stopwords={"我","了","到","曰","又","吾","去","来","为","将"\
                                 "将军","却说","荆州","二人","不可","不能","如此"})
w.generate(" ".join(txt))
w.to_file("threekingdoms.png")

 

 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值