安装模块
pip install wordcloud
导入模块
import numpy as np
from wordcloud import WordCloud
from PIL import Image
定义函数
def wordCloudImage(wordlist,width,height,bgcolor,savepath):
# 可以打开你喜欢的词云展现背景图
# cloud_mask = np.array(Image.open('nezha.png'))
# 定义词云的一些属性
wc = WordCloud(
width=width, # 图幅宽度 900
height=height, # 图幅高度 3000
background_color=bgcolor, # 背景图分割颜色为白色 "black"
# mask=cloud_mask, # 背景图样
max_words=300, # 显示最大词数
font_path='./fonts/simhei.ttf', # 显示中文
collocations=False,
# min_font_size=5, # 最小尺寸
# max_font_size=100, # 最大尺寸
)
# wordfile是分词后的词汇列表
x = wc.generate(wordlist)
# 生成词云图片
image = x.to_image()
# 展示词云图片
image.show()
# savepath是图片保存地址,保存词云图片
wc.to_file(savepath)
使用
wordCloudImage(str(msg),400,800,'black',r"D:\Users\Administrator\Desktop\test\static\images\wc.png")
需要注意的事 第一个参数传入要求是字符串型 如果报错,可以强转为字符串型
第二种方式
引入模块
import jieba
import jieba.analyse
import pandas as pd
import pyecharts.options as opts
from pyecharts.charts import WordCloud
内容分词
txt = ""
with open(r"射雕英雄传.txt", encoding="utf-8") as f:
txt = f.read()
提取关键字
word_weights = jieba.analyse.extract_tags(txt, topK=50, withWeight=True)
制作词云图
word_cloud = WordCloud().add(series_name="高频词语", data_pair=word_weights, word_size_range=[10,50]).set_global_opts(title_opts=opts.TitleOpts(
title="射雕英雄传-高频词云图",
title_textstyle_opts=opts.TextStyleOpts(font_size=23),
pos_left="center"))
渲染后在html文件打开
word_cloud.render()