程序员文章、书籍推荐和程序员创业信息与资源分享平台

网站首页 > 技术文章 正文

三步编写一个Python词云制作小工具

hfteth 2025-07-06 12:16:11 技术文章 1 ℃

步骤1:配置对象参数

import wordcloud# pip install wordcloud
w=wordcloud.WordCloud()#生成一个词云对象

步骤2:加载词云文本

txt="Python and WordCloud"
w.generate(txt)#向WordCloud对象w中加载文本txt

步骤3:输出词云文件

w.to_file("outfile.png")


词云应用案例

英文用法

import wordcloud 
txt = "life is short, you need python" 
w = wordcloud.WordCloud(background_color = "white")
w.generate(txt) 
w.to_file("pywcloud.png")#保存的文件名



中文用法(需安装jieba库进行分词处理)

import jieba 
import wordcloud 
txt = "人生苦短,我学Python" 
w = wordcloud.WordCloud( width=1000,height=700,font_path="MSYHL.TTC")
w.generate(" ".join(jieba.lcut(txt))) 
w.to_file("./image/pywcloud.png")

Tags:

最近发表
标签列表