“Python:词云图”的版本间的差异
跳到导航
跳到搜索
(未显示同一用户的2个中间版本) | |||
第1行: | 第1行: | ||
− | [https://amueller.github.io/word_cloud/auto_examples/single_word.html | + | [[File:Python-词云图-demo.png|词云图]] |
+ | |||
+ | |||
+ | [https://amueller.github.io/word_cloud/auto_examples/single_word.html 官方demo文档] | ||
第7行: | 第10行: | ||
====== 中文分词 ====== | ====== 中文分词 ====== | ||
pip install jieba | pip install jieba | ||
− | ====== | + | ====== 获取遮罩图====== |
[https://amueller.github.io/word_cloud/auto_examples/colored.html demo] | [https://amueller.github.io/word_cloud/auto_examples/colored.html demo] | ||
2019年4月28日 (日) 13:32的最新版本
安装
pip install wordcloud
中文分词
pip install jieba
获取遮罩图
import numpy as np alice_mask = np.array(Image.open("xxx.png"))
或
from scipy.misc import imread alice_mask = imread('xxx.png')
如果报:_tkinter.TclError: no display name and no $DISPLAY environment variable
在“import matplotlib.pyplot as plt”之前,加入:
import matplotlib as mpl mpl.use('Agg')
实例:
#!/usr/bin/python # -*- coding: utf-8 -*- import wordcloud from wordcloud import WordCloud import jieba # from PIL import Image # import numpy as np import matplotlib as mpl mpl.use('Agg') import matplotlib.pyplot as plt from scipy.misc import imread with open('论语.txt', 'r') as f: text = " ".join(jieba.cut(f.read())) # alice_mask = np.array(Image.open("遮罩图.png")) alice_mask = imread('遮罩图.png') wordCloud = WordCloud(font_path='msyh.ttf',background_color='white', max_words=2000, max_font_size=80, random_state=40, mask=alice_mask) wordCloud.generate(text) plt.imshow(wordCloud, interpolation='bilinear') plt.axis("off") plt.show() # plt.savefig('path to output.png') wordCloud.to_file('path to output.png')