python分词并绘制词云

import sqlite3

import jieba                             #分词
from matplotlib import pyplot as plt    #数据可视化
import wordcloud
from PIL import Image               #图片处理
import numpy as np                    #矩阵运算
import sqlite3
from openpyxl import load_workbook



#准备数据
wb = load_workbook('F:\jjj.xlsx')
ws = wb.active
text = ""
for row in ws.rows:
    for cell in row:
        print(cell.value)
        text = text + cell.value

#分词
cut = jieba.cut(text)
string = ' '.join(cut)
print(len(string))
# print(string)
# list = string.split()
# for x in list:
#     print(x)



#图像制作
img = Image.open(r'tree3.png')
img_array = np.array(img)   #图片转换为数组
wc = wordcloud.WordCloud(
    background_color='white',   #背景色
    mask = img_array,       #遮罩层
    font_path='STKAITI.TTF' #字体位置 c:\windows\fonts
)
wc.generate_from_text(string)

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

plt.show() #显示正常的词云图片

# plt.savefig('F:\jdos.jpg', dpi=500)

你可能感兴趣的:(Python,python,开发语言,后端)