一、导入依赖包
# coding : utf-8
# author : xi_li
from os import path
from scipy.misc import imread
import matplotlib.pyplot as plt
import jieba
from wordcloud import WordCloud, ImageColorGenerator
from imageio import imread
二、 初始化相关数值,如背景图片路径、文本文件路径、自定义停用词文件路径等
# 初始化
img = "img.jpg"
file = "听听那冷雨.txt"
font_path = "microhei.ttc"
stopwords = "stopwords.txt"
my_word_list = ['听听那冷雨']
# 设置背景图片
backcolor = imread(path.join(img))
三、设置词云相关属性,如最大词语数量、字体大小等
#设置词云属性
wordcloud = WordCloud(
font_path = font_path,
background_color = "white",
max_words = 2000,
max_font_size = 100,
random_state = 50,
width = 1000, height = 1300, margin = 2
)
四、自定义分词并读取文件