900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 【Python实战】--词云制作

【Python实战】--词云制作

时间:2019-10-08 16:33:38

相关推荐

【Python实战】--词云制作

系列文章目录

文章目录

系列文章目录前言一、准备工作1.模块2.字体3. 背景图(词云形状)4. 文本二、制作词云1.最简单的词云(中文)2.最简单的词云(英文)3.中文词云(分词效果)4.使用背景蒙版5.使用背景蒙版(蒙版提取颜色)6.使用背景蒙版(自定义颜色)7.使用背景蒙版(精细控制)三、本文资源总结

前言

一、准备工作

1.模块

#pip install 模块wordcloud matplotlib #数据可视化jieba #分词库pillownumpy

可能出现的问题:

wordcloud安装需要visual C++14.0 whl安装

wordcloud()参数解释:

2.字体

1、可以在电脑自带的字体中选择,复制到运行脚本路径下,也可指定路径;

C:\Windows\Fonts

2、下载其它喜欢的字体放到指定路径下;

注:这里提供了一些字体,可自行下载;

3. 背景图(词云形状)

4. 文本

中文:

与你交流,就像与自己的灵魂在耳语,自由,让心跳的声音呼之欲出,真想伸出双臂与你进行一次心贴心的拥抱窗外寂静无声。唯独冬雨拍打台阶的声音犹如琴音围炉,已经是梦想中的奢望甚至无须。有金骏眉陪,足矣浪漫隐藏在烟圈之中,以一个思想者的姿态静坐,思绪感觉已经和你水乳交融。唇齿生香不仅仅来源于杯中的香茗仿佛是等待一场期待已久的约会琐碎的日子也被润泽得云蒸霞蔚实际上,你读着我的书心迷神醉你的灵魂已经变成奔向我的异乡人我是终究要牵你手的。这也许就是江湖。温馨,让忧伤与我无缘我也不会让疼痛,在你心里暗藏一场雪已经送来彼此的关切从洼得不能再洼的地方起步。像一个坐标轴上的负数,更像一块会走动的棱角分明的石头。生活是刀,削铁如泥它切割着我,我磨砺着它。终于有一天我走到了坐标轴上的零点。我还是没有圆滑。而生活也没有更柔软。我把目光,投向看不见的远方。我把背影,留给回不去的故乡。铿锵的步履犹如骏马的蹄声一度成为故乡的传奇。而正道,从来就不是一条直线。九曲十八弯的路上。我的泪水可以被路边一朵小花引出。泰山压顶却改变不了我平视的眼神。妥协深藏我的脊梁怎么也弯不下来。这就是我半生的经历。我已经没有余力去改变。夕阳晚照里唯一的预见就是回归。回归成那个洼得不能再洼的地方的一块石头这些,你不知道。你不知道,我不怪你。

英文:

Three passions, simple but overwhelmingly strong, have governed my life: the longing for love, the search for knowledge, and unbearable pity for the suffering of mankind. These passions, like great winds, have blown me hither and thither, in a wayward course, over a deep ocean of anguish, reaching to the verge of despair. I have sought love, first, because it brings ecstasy --- ecstasy so great that I would have sacrificed all the rest of life for a few hours of this joy. I have sought it, next,because it relieves loneliness --- that terrible loneliness in which one shivering consciousness looks over the rim of the world into cold unfathomable lifeless abyss. I have sought it, finally, because in the union of love I have seen, in a mystic miniature, the prefiguring vision of the heaven that saints and poets have imagined. This is what I sought, and though it might seem too good for human life, this is what --- at last --- I have found. With equal passion I have sought knowledge. I have wished to understand the hearts of men, I have wished to know why the stars shine. And I have tried to apprehend the Pythagoreanpower by which number holds away above the flux. A little of this, but not much, I have achieved. Love and knowledge, so far as they were possible, led upward toward the heavens. But always pity brought me back to earth. Echoes of cries of pain reverberated in my heart. Children in famine, victims tortured by oppressors, helpless old people a hated burden to their sons, and the whole world of loneliness, poverty, and pain make a mockery of what human life should be. I long to alleviate the evil, but I cannot, and I too suffer. This has been my life. I have found it worth living, and I would gladly live it again if the chance were offered to me.

二、制作词云

1.最简单的词云(中文)

效果如下:

代码如下:

##chinesefrom wordcloud import WordCloudimport matplotlib.pyplot as plt# 打开文本text = open("bulletchinese.txt", encoding="utf-8").read()# 生成对象wc = WordCloud(font_path="msyh.ttc", width=800, height=600, mode="RGBA", background_color=None).generate(text)# 显示词云plt.imshow(wc, interpolation='bilinear')plt.axis("off")plt.show()# 保存到文件wc.to_file("bulletchinese.png")

2.最简单的词云(英文)

效果如下:

代码如下

from wordcloud import WordCloudimport matplotlib.pyplot as plt# 打开文本text = open("bulletEnglish.txt", encoding="utf-8").read()# print(text)# print(type(text)) # <class 'str'># 生成对象#wc = WordCloud().generate(text)wc = WordCloud(font_path="msyh.ttc", width=800, height=600, mode="RGBA", background_color=None).generate(text)# 显示词云plt.imshow(wc, interpolation='bilinear') # interpolation设置插值,设置颜色、排列等plt.axis("off") # 关闭坐标轴plt.show()# 将词云图片保存到文件wc.to_file("bulletEnglish.png")

3.中文词云(分词效果)

效果如下:

代码如下:

from wordcloud import WordCloudimport matplotlib.pyplot as pltimport jieba# 打开文本text = open("bulletchinese.txt", encoding="utf-8").read()# 中文分词text = ' '.join(jieba.cut(text)) # 利用jieba进行分词形成列表,将列表里面的词用空格分开并拼成长字符串。print(text[:10000]) # 打印前100个字符# 生成对象wc = WordCloud(font_path="msyh.ttc", width=800, height=600, mode="RGBA", background_color=None).generate(text)# 显示词云plt.imshow(wc, interpolation="bilinear")plt.axis("off")plt.show()# 保存到文件wc.to_file("bulletchinese2.png")

4.使用背景蒙版

效果如下:

代码如下:

from wordcloud import WordCloudfrom PIL import Imageimport numpy as npimport matplotlib.pyplot as pltimport jieba# 打开文件text = open("bulletchinese.txt", encoding="utf-8").read()# 中文分词text = ' '.join(jieba.cut(text))print(text[:1000])# 生成对象mask = np.array(Image.open("background.png")) # 使用蒙版图片wc = WordCloud(mask=mask, font_path="msyh.ttc", width=800, height=600, mode="RGBA", background_color=None).generate(text)# 显示词云plt.imshow(wc, interpolation="bilinear")plt.axis("off")plt.show()#保存文件wc.to_file("bulletchinese3.png")

5.使用背景蒙版(蒙版提取颜色)

效果如下:

代码如下:

from wordcloud import WordCloud, ImageColorGeneratorfrom PIL import Imageimport numpy as npimport matplotlib.pyplot as pltimport jieba# 打开文件text = open("bulletchinese.txt", encoding="utf-8").read()# 中文分词text = ' '.join(jieba.cut(text))print(text[:1000])# 生成对象mask = np.array(Image.open("background1.png"))wc = WordCloud(mask=mask, font_path="msyh.ttc", width=1000, height=800,mode="RGBA", background_color=None).generate(text)# 从图片中生成颜色image_colors = ImageColorGenerator(mask)wc.recolor(color_func=image_colors)# 显示词云plt.imshow(wc, interpolation="bilinear")plt.axis("off")plt.show()# 保存文件wc.to_file("bulletchinese4.png")

6.使用背景蒙版(自定义颜色)

效果如下:

代码如下:

from wordcloud import WordCloudfrom PIL import Imageimport numpy as npimport matplotlib.pyplot as pltimport randomimport jieba# 打开文本text = open("bulletchinese.txt", encoding="utf-8").read()# 中文分词text = ' '.join(jieba.cut(text))print(text[:1000])# 颜色函数:(词,词的大小,词的位置,词的朝向,词的路径,随机的状态)def random_color(word, font_size, position, orientation, font_path, random_state):s = 'hsl(0, %d%%, %d%%)' % (random.randint(60, 80), random.randint(60, 80))# hsl代表色相(Hue)、饱和度(saturation)、亮度(luminance)print(s)return s# 生成对象mask = np.array(Image.open("background.png"))wc = WordCloud(color_func=random_color, mask=mask, font_path="msyh.ttc", mode="RGBA", background_color=None).generate(text) # color_func=random_color即用函数来指定词的颜色# 显示词云plt.imshow(wc, interpolation="bilinear")plt.axis("off")plt.show()# 保存到文件wc.to_file("bulletchinese5.png")

7.使用背景蒙版(精细控制)

效果如下:

代码如下:

from wordcloud import WordCloud, ImageColorGeneratorfrom PIL import Imageimport numpy as npimport matplotlib.pyplot as pltimport jieba.analyse # 用于提取关键词# 打开文本text = open("bulletchinese.txt", encoding="utf-8").read()# 提取关键词和权重freq = jieba.analyse.extract_tags(text, topK=200, withWeight=True) # 提取文件中的关键词,topK表示提取的数量,withWeight=True表示会返回关键词的权重。print(freq[:200])freq = {i[0]: i[1] for i in freq} # 字典# 生成对象mask = np.array(Image.open("background2.png"))wc = WordCloud(mask=mask, font_path="msyh.ttc", mode="RGBA", background_color=None).generate_from_frequencies(freq)# 从图片中生成颜色image_colors = ImageColorGenerator(mask)wc.recolor(color_func=image_colors)# 显示词云plt.imshow(wc, interpolation="bilinear")plt.axis("off")plt.show()# 保存文件wc.to_file("bulletchinese6.png")

三、本文资源

资源下载

总结

分享:

在孤独中有时饱含着屈辱和忍耐,又酝酿着愤怒和抗争。此时的沉寂往往蕴藏着强大的爆发力。生命中最孤独的时刻,往往会成为人生的催化剂。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。