Python day2--生成词云

先看下效果:

代码如下:

自己准备一个txt文件,写入单词即可


# import
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import csv

# 打开文件
file = open(r"C:\Users\lsy\Desktop\1234.txt")

# reader object 
reader = csv.reader(file)

# contents of reader object is stored .
# data is stored in list of list  format.
reader_contents = list(reader)

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()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值