47

红楼梦词云用Python还能这样画

 5 years ago
source link: http://www.10tiao.com/html/314/201807/2672941312/1.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

用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分析与挖掘)》系列视频课程

包含课程:

Python数据分析实战

数据挖掘理论入门

Python数据挖掘实战

Python爬虫数据抓取实战

PS:这是进入大数据分析必备的系列课程,想玩转大数据分析的朋友一定要学的,特别是准备进入或已经进入互联网行业的朋友。


现已有2800+学员加入学习数据分析进阶(Python分析与挖掘》,前3000名可享6折319元原价526元)优惠。


点“阅读原文”进入课程页面学习,需要点 参加全部课程 按钮,才可享受优惠


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK