用Python读红楼梦之——二、词云美化
2016-07-08 13:23
246 查看
原文连接
咱们书接上回,讲到我们使用Python把红楼梦中的核心词汇给绘画出来了,但是,红楼梦这么唯美的书,给我们乌漆麻黑的搞了一张词云,宝宝们肯定接受不了。
我们先来找照片,这张照片不错,不过还是要处理一下,把背景处理称为白色,这个时候,PS工具可以简单地达到这个效果。
接着,我们就要来写程序了,老规矩,我们先来导入包:
from
scipy.misc import imread
import
matplotlib.pyplot as plt
from
wordcloud import WordCloud, ImageColorGenerator
把图片读取进去,然后重新设置一下WordCloud对象,设置mask为我们的图片。
bimg
= imread("D:\\贾宝玉.png")
wordcloud
= WordCloud(
background_color="white",
mask=bimg,
font_path='D:\\simhei.ttf'
)
接着,我们就可以美美地绘制词云了。
wordcloud
= wordcloud.fit_words(segStat.head(1000).itertuples(index=False))
bimgColors
= ImageColorGenerator(bimg)
plt.axis("off")
plt.imshow(wordcloud.recolor(color_func=bimgColors))
plt.show()
长腿欧巴虽然好,但是画起来,很明显我们的词汇分成两大部分了,这样子会分散我们的注意力,因此,我们使用人物作为背景绘制词云的时候,最好只是使用上半身,使用头像就更好了。因此,长腿欧巴拜拜。
bimg
= imread("D:\\贾宝玉2.png")
wordcloud
= WordCloud(
background_color="white",
mask=bimg,
font_path='D:\\simhei.ttf'
)
wordcloud
= wordcloud.fit_words(segStat.head(1000).itertuples(index=False))
bimgColors
= ImageColorGenerator(bimg)
plt.axis("off")
plt.imshow(wordcloud.recolor(color_func=bimgColors))
plt.show()
这样子画出来的词云,是不是就唯美了很多了呢?希望你们喜欢,欲知后事如何,欢迎微博关注@大数据分析实战,我们下回再见。
咱们书接上回,讲到我们使用Python把红楼梦中的核心词汇给绘画出来了,但是,红楼梦这么唯美的书,给我们乌漆麻黑的搞了一张词云,宝宝们肯定接受不了。
我们先来找照片,这张照片不错,不过还是要处理一下,把背景处理称为白色,这个时候,PS工具可以简单地达到这个效果。
接着,我们就要来写程序了,老规矩,我们先来导入包:
from
scipy.misc import imread
import
matplotlib.pyplot as plt
from
wordcloud import WordCloud, ImageColorGenerator
把图片读取进去,然后重新设置一下WordCloud对象,设置mask为我们的图片。
bimg
= imread("D:\\贾宝玉.png")
wordcloud
= WordCloud(
background_color="white",
mask=bimg,
font_path='D:\\simhei.ttf'
)
接着,我们就可以美美地绘制词云了。
wordcloud
= wordcloud.fit_words(segStat.head(1000).itertuples(index=False))
bimgColors
= ImageColorGenerator(bimg)
plt.axis("off")
plt.imshow(wordcloud.recolor(color_func=bimgColors))
plt.show()
长腿欧巴虽然好,但是画起来,很明显我们的词汇分成两大部分了,这样子会分散我们的注意力,因此,我们使用人物作为背景绘制词云的时候,最好只是使用上半身,使用头像就更好了。因此,长腿欧巴拜拜。
bimg
= imread("D:\\贾宝玉2.png")
wordcloud
= WordCloud(
background_color="white",
mask=bimg,
font_path='D:\\simhei.ttf'
)
wordcloud
= wordcloud.fit_words(segStat.head(1000).itertuples(index=False))
bimgColors
= ImageColorGenerator(bimg)
plt.axis("off")
plt.imshow(wordcloud.recolor(color_func=bimgColors))
plt.show()
这样子画出来的词云,是不是就唯美了很多了呢?希望你们喜欢,欲知后事如何,欢迎微博关注@大数据分析实战,我们下回再见。
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法