# -*- coding:utf-8 -*- |
import matplotlib.pyplot as plt |
from wordcloud import WordCloud |
import jieba |
# 读取文件中的文本字符串 |
txt = open ( "file.txt" , "r" , encoding = "utf-8" ).read() |
# 结巴分词 |
wordlist = jieba.cut(txt) |
wordlist = ' ' .join(wordlist) |
# 生成WordCloud对象 |
wordcloud = WordCloud( |
font_path = r 'C:\Windows\Fonts\simfang.ttf' , # 设置字体,不然会出现口字乱码 |
background_color = "white" # 背景颜色 |
).generate(wordlist) |
# 展示词云图 |
plt.imshow(wordcloud, interpolation = 'bilinear' ) |
plt.axis( "off" ) |
plt.show() |
高级设计师
by: Python自学 发表于:2023-02-17 01:28:50 顶(0) | 踩(0) 回复
用wordcloud自带的汉字字体font_path参数,指定一个有字体文件的路径,比如simhei.ttf,再指定font_size参数设置字体的大小即可。
回复评论