import jieba # 分词
from matplotlib import pyplot as plt # 绘图,数据可视化
from wordcloud import WordCloud # 词云
from PIL import Image # 图片处理
import numpy as np # 矩阵运算
import sqlite3 # 数据库
# jieba中文分词 numpy矩阵运算
cut = jieba.cut(text) # jieba库中的cut方法将字符串中的词分隔开来,默认是精确模式(cat_all=True 全模式)
string = " ".join(cut)
img = Image.open(r'遮罩图片路径') # 打开遮罩图片 PIL Image 图片处理 show方法显示图片
img_array = np.array(img) # 将图片转换成数据(数组)
#扩充:
"""
test = np.array(range(5))
test2 = np.array(range(6,11))
result = test2+test
output:
[ 6 8 10 12 14]
"""
wc = WordCloud(
background_color="white",
mask=img_array, # 遮罩图数组数据
font_path="msyhl.ttc" # 字体所在位置 C:\Windows\Fonts
).generate_from_text(string) # string为已经分词好的字符串
# 绘制图片
fig = plt.figure(1)
plt.imshow(wc)
plt.axis("off") # 是否显示坐标轴
# plt.show() # 显示生成的词云图片
plt.savefig(r".\word.jpg",dpi=450)
词云
最新推荐文章于 2022-07-14 16:28:34 发布