Home > Backend Development > Python Tutorial > How to use python to implement sample code sharing of personalized word clouds

How to use python to implement sample code sharing of personalized word clouds

黄舟
Release: 2018-05-25 09:39:56
Original
3193 people have browsed it

I recently saw a visual word cloud, and there are many such tools on the Internet, but they are not perfect. Some do not support Chinese, some Chinese word frequency statistics are inexplicable, some do not support custom shapes, and all of them are The color cannot be customized, so I searched online and decided to use python to draw the word cloud

First upload the picture


Word cloud diagram

Need template

pip install jieba
pip install wordcloud
Copy after login

Also needInstallationI don’t know much about the other two things written by Baidu

pip install scipy
pip install matplotlib
Copy after login

Because using the Ubuntu system is not as troublesome as Windows, and there are not so many errors

Do you feel excited when you see so many people making their own word clouds? If you are excited, go immediately Doing it, impulse is the first creativity.

jieba is a very easy to use Chinese word segmentation template

jeiba Chinese document

As for wordcloud, there is no Chinese document but we If you know how to guess, if your English is not good, we can guess. Check the source code to guess.

Contribute all my code first

#-*-coding:utf-8—-*-
import jieba.analyse
from wordcloud import WordCloud,ImageColorGenerator
from scipy.misc import imread
import matplotlib.pyplot as plt

class wc:
  def init(self,txt_file,img_file,font_file):
   self.f = open(txt_file,'r')
  self.txt = self.f.read()
   self.f.close()
  self.tags = jieba.analyse.extract_tags(self.txt,topK=100)
  #topK说白了就是返回几个关键词
  self.text = ' '.join(self.tags) #把分词链接起来,加空格因为英文靠空格分词
  self.img = imread(img_file)
  self.wc = WordCloud(font_path=font_file,background_color='white',max_words=100,mask=self.img,max_font_size=80)
###直接在这里进行猜###
#font_path指的是字体文件路径,因为wordcloud自带的字体不支持中文所以我们要指定一个字体文件,否者输出的图片全是框框
#background_color 默认是黑色 我设置成白色
#max_words最大显示的词数
#mask 背景图片
#max_font_size 最大字体字号
  self.word_cloud = self.wc.generate(self.text)

  def show_wc(self):
  #img_color = ImageColorGenerator(self.img)
    plt.imshow(self.word_cloud)
#可以通过 plt.imshow(self.wc.recolor(color_func=img_color))使图片颜色跟字体颜色一样
    plt.axis("off")
    plt.show()


if name=='main':
  mywc = wc('sanwen.txt','out.png','font.ttc')
  mywc.show_wc()
Copy after login


Use wc.recolor(color_func=img_color)

The above is the detailed content of How to use python to implement sample code sharing of personalized word clouds. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template