【Python】使用wordcloud绘制词云

from wordcloud import WordCloud,STOPWORDS
from PIL import Image
import numpy as np

#准备遮罩图片(蒙版图像、词云形状)
img=Image.open("images/苏轼.png")
img_array=np.array(img)

#设置字体
#font=r'C:\Windows\Fonts\simkai.ttf' #需要在你电脑上确认字体文件的存在
font=r'./fonts/AlibabaPuHuiTi-2-65-Medium.ttf'
#使用阿里巴巴普惠字体2.0,下载地址为https://www.alibabafonts.com/#/font

#准备停用词
stopwords=set(STOPWORDS)
stopwords.add("惟有")

wc=WordCloud(width=600,                #图片宽度(单位:像素)
             height=600,               #图片高度(单位:像素)
             background_color='white', #背景颜色
             max_font_size=80,         #词云最大字号
             min_font_size=16,         #词云最小字号
             font_path=font,           #中文分词必须有中文字体设置
             min_word_length=2,        #最小单词长度
             mask=img_array,           #设置遮罩图片;若没有该项则为默认设置
             stopwords=stopwords       #设置停用词
)

word_list=' '.join(res)
wc.generate_from_text(word_list) #绘制词云

#设置保存的png文件名
wc.to_file("output/wordcloud词云.png")

参考资料:
https://www.bilibili.com/cheese/play/ep845561?csource=common_myclass_purchasedlecture_null&spm_id_from=333.874.selfDef.mine_paid_list

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值