使用python生成云词图实现画红楼梦词云图
作者:梦魇762459510 时间:2022-07-19 00:21:56
在用Python绘制红楼梦词云图文章中讲到我们使用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生成云词图实现画红楼梦词云图的详细内容,更多关于python生成云词图的资料请关注脚本之家其它相关文章!
来源:https://blog.csdn.net/MC_XY/article/details/120135890
标签:python,词云图
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
如何把小写数字转换成英文?
2009-11-06 13:59:00
Python流程控制语句的深入讲解
2023-12-01 00:06:31
![](https://img.aspxhome.com/file/2023/6/101066_0s.png)
python利用urllib实现爬取京东网站商品图片的爬虫实例
2022-05-01 12:32:56
![](https://img.aspxhome.com/file/2023/7/69457_0s.jpg)
SQL 中主标识列IDENTITY使用技巧
2011-06-02 08:47:00
详解Django将秒转换为xx天xx时xx分
2023-06-14 22:52:12
利用python-pypcap抓取带VLAN标签的数据包方法
2021-03-15 04:46:20
![](https://img.aspxhome.com/file/2023/2/83892_0s.jpg)
Asp性能优化之Response.IsClientConnected属性及其应用示例
2008-09-18 12:13:00
一文教会你用Python获取网页指定内容
2023-01-01 09:15:05
![](https://img.aspxhome.com/file/2023/4/127774_0s.png)
Python OpenCV图像指定区域裁剪的实现
2021-12-30 01:41:35
tensorflow 大于某个值为1,小于为0的实例
2023-06-12 03:00:43
Python实现免费音乐下载器
2023-12-26 23:51:16
![](https://img.aspxhome.com/file/2023/3/97213_0s.gif)
详解phpMyAdmin的安装和配置
2007-06-15 09:56:00
pytorch教程之网络的构建流程笔记
2021-11-24 10:58:59
python 日志模块logging的使用场景及示例
2023-01-10 16:39:57
![](https://img.aspxhome.com/file/2023/9/121609_0s.png)
python设置环境变量的原因和方法
2023-05-12 18:31:35
![](https://img.aspxhome.com/file/2023/1/77651_0s.png)
使用Python的turtle模块画国旗
2021-10-22 12:31:10
![](https://img.aspxhome.com/file/2023/4/93724_0s.jpg)
Python中sorted()用法案例代码
2022-01-20 04:08:00
python http通信接口开发示例
2022-06-07 05:15:29
![](https://img.aspxhome.com/file/2023/9/96989_0s.png)
python读取txt文件并取其某一列数据的示例
2022-09-25 01:58:09
关于python环境变量如何配置
2023-06-02 14:23:09
![](https://img.aspxhome.com/file/2023/7/98937_0s.jpg)