一、准备
词云制作所必需的三个包:
1
2
3
|
安装:pip install matplotlib
安装:pip install jieba
安装:pip install wordcloud
|
(可能还需要其他包:pip install numpy等等,可以自行添加)
二、英文词云
test.txt文本内容:
1
2
3
4
5
6
7
8
9
10
|
python python3 is good well bestbast shell cool
Age has reached the end of the beginning of a word. May be guilty in his seems to passing a lot of different life became the appearance of the
same day; May be backto oneself the paranoid weird belief disillusionment, these days, my mind has been very messy, in my mind constantly. Always
feel oneself should go to do something, or write something. Twenty years of life trajectory deeply shallow, suddenly feel something, do it.The end
of our life, and can meet many things really do?During myhood, think lucky money and new clothes are necessary for New Year, but as the advance of
the age, will be more and more found that those things are optional; Junior high school, thought to have a crush on just means that the real growth,
but over the past three years later, his writing of alumni in peace, suddenly found that isn't really grow up, it seems is not so important; Then
in high school, think don't want to give vent to out your inner voice can be in the high school children of the feelings in a period, but was event
ually infarction when graduation party in the throat, later again stood on the pitch he has sweat profusely, looked at his thrown a basketball hoops
, suddenly found himself has already can't remember his appearance.
|
源代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
import matplotlib.pyplot as plt # 在任何绘图之前,我们需要一个figure对象,可以理解成我们需要一张画板才能开始绘图
import jieba # jieba库是中文分词的第三方库(中文文本需要通过分词获得单个的词语)
from wordcloud import WordCloud # 导入wordcloud库
text = open (r 'test.txt' , "r" ).read() # 读入txt文本数据,在字符串前面加上字符r或R之后表示原始字符串,字符串中的任意字符都不再进行转义,后一个r表示“只读”
cut_text = jieba.cut(text) # 结巴中文分词,生成字符串,默认精确模式,如果不通过分词,无法直接生成正确的中文词云
result = " " .join(cut_text) # 必须给个符号分隔开分词结果来形成字符串,否则不能绘制词云
# join函数的用法:'sep'.join(seq)参数说明:sep:分隔符。可以为空;seq:要连接的元素序列、字符串、元组、字典;即:以sep作为分隔符,将seq所有的元素合并成一个新的字符串
# 生成词云图,这里需要注意的是WordCloud默认不支持中文,所以这里需已下载好的中文字库
# 无自定义背景图:需要指定生成词云图的像素大小,默认背景颜色为黑色,统一文字颜色:mode='RGBA'和colormap='pink'
wc = WordCloud(
# 设置字体,不指定就会出现乱码
background_color = 'white' , # 设置背景色,默认为黑色
width = 500 , # 设置背景宽
height = 350 , # 设置背景高
max_font_size = 50 , # 最大字体
min_font_size = 10 , # 最小字体
mode = 'RGBA' # 当参数为“RGBA”并且background_color不为空时,背景为透明
)
wc.generate(result) # 根据分词后的文本产生词云
wc.to_file(r "wordcloud.png" ) # 保存绘制好的词云图
plt.imshow(wc) # 以图片的形式显示词云
plt.axis( "off" ) # 关闭图像坐标系,即不显示坐标系
plt.show() # plt.imshow()函数负责对图像进行处理,并显示其格式,但是不能显示。其后必须有plt.show()才能显示
|
效果展示:
三、中文词云
pkq.jpg模板内容:
poem.txt文本内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
君不见黄河之水天上来,奔流到海不复回。
君不见高堂明镜悲白发,朝如青丝暮成雪。
人生得意须尽欢,莫使金樽空对月。
天生我材必有用,千金散尽还复来。
烹羊宰牛且为乐,会须一饮三百杯。
岑夫子,丹丘生,将进酒,杯莫停。
与君歌一曲,请君为我侧耳听。
钟鼓馔玉不足贵,但愿长醉不复醒。
古来圣贤皆寂寞,惟有饮者留其名。
陈王昔时宴平乐,斗酒十千恣欢谑。
主人何为言少钱,径须沽取对君酌。
五花马,千金裘,
呼儿将出换美酒,与尔同销万古愁。
床前明月光,疑是地上霜。
举头望明月,低头思故乡。
海客谈瀛洲,烟涛微茫信难求。
越人语天姥,云霞明灭或可睹。
天姥连天向天横,势拔五岳掩赤城。
天台四万八千丈,对此欲倒东南倾。
我欲因之梦吴越,一夜飞度镜湖月。
湖月照我影,送我至剡溪。
谢公宿处今尚在,渌水荡漾清猿啼。
脚著谢公屐,身登青云梯。
半壁见海日,空中闻天鸡。
千岩万转路不定,迷花倚石忽已暝。
熊咆龙吟殷岩泉,栗深林兮惊层巅。
云青青兮欲雨,水澹澹兮生烟。
列缺霹雳,丘峦崩摧。洞天石扉,訇然中开。
青冥浩荡不见底,日月照耀金银台。
霓为衣兮风为马,云之君兮纷纷而来下。
虎鼓瑟兮鸾回车,仙之人兮列如麻。
忽魂悸以魄动,恍惊起而长嗟。
惟觉时之枕席,失向来之烟霞。
世间行乐亦如此,古来万事东流水。
别君去兮何时还,且放白鹿青崖间,
须行即骑访名山。安能摧眉折腰事权贵,
使我不得开心颜
|
源代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
import wordcloud
import numpy as np
from PIL import Image # Image模块是在Python PIL图像处理常用的模块
import jieba
pic = Image. open ( "pkq.jpg" ) # 打开图片路径,形成轮廓
shape = np.array(pic) # 图像轮廓转换为数组
wc = wordcloud.WordCloud(mask = shape, font_path = "simkai.ttf" , background_color = "white" ,
max_font_size = 100 ) # mask为图片背景,font_path为字体,若不设置可能乱码
text = open (r 'poem.txt' , "r" , encoding = 'UTF-8' ).read() # 对中文应该设置编码方式为utf—8
cut_text = jieba.cut(text)
result = " " .join(cut_text)
wc.generate(result)
wc.to_file( "cloud.jpg" )
|
效果图:
到此这篇关于只用Python就可以制作的简单词云的文章就介绍到这了,更多相关Python制作词云内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/qq_44111805/article/details/117513185