python词云统计WordCloud

python词云统计WordCloud,第1张


一、代码实现

import jieba       #分词
from matplotlib import pyplot as plt   #绘图,数据可视化
from wordcloud import WordCloud        #词云
from PIL import Image                  #图片处理

import numpy as np                      #矩阵运算
import sqlite3                          #数据库

con = sqlite3.connect('movie.db')
cur = con.cursor()
sql = 'select info from movie25'
data = cur.execute(sql)
text = ""
for item in data:
    text = text + item[0]
    #print(text)
cur.close()
con.close()

#分词
cut = jieba.cut(text)
string = ''.join(cut)
print(len(string))

#绘图
img = Image.open(r'./static/assets/image/img.png')
img_array = np.array(img) #将图片转换为wordcloud
wc = WordCloud(
    background_color='white',
    mask=img_array,
    font_path="PingFang.ttc"  #位置在/system/Library/fonts  微软雅黑字体是msyh.ttc
)
wc.generate_from_text(string)

#绘制图片
fig = plt.figure(1)
plt.imshow(wc)
plt.axis('off')   #是否显示坐标轴

plt.show() #显示生成的词云图片

#plt.savefig(r'./static/assets/image/word.png', dpi=500)


二、原始图片


三、词云图片

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/571432.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-09
下一篇 2022-04-09

发表评论

登录后才能评论

评论列表(0条)

保存