wordcloud词云

两种方法:

1、使用pyecharts ,它是一个用于生成 Echarts 图表的类库

教程:访问pyechart教程

2、使用python的wordcloud类库

安装:

python -m pip install wordcloud

代码:

"""
Masked wordcloud
================
Using a mask you can generate wordclouds in arbitrary shapes.
"""

from os import path
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import os

from wordcloud import WordCloud, STOPWORDS,  ImageColorGenerator

# get data directory (using getcwd() is needed to support running example in generated IPython notebook)
d = path.dirname(__file__) if "__file__" in locals() else os.getcwd()

# Read the whole text.
text = open(path.join(d, 'alice.txt')).read()

# read the mask image
# taken from
# http://www.stencilry.org/stencils/movies/alice%20in%20wonderland/255fk.jpg
alice_mask = np.array(Image.open(path.join(d, "alice_mask.png")))

#设置需要屏蔽的词
stopwords = set(STOPWORDS)
stopwords.add("said")

#定义字体路径
#当前为中文simhei,默认不支持中文,字体可在C:\Windows\Fonts中寻找
fontpath = path.join(d,'simhei.ttf')
wc = WordCloud(background_color="white", max_words=2000, mask=alice_mask,
               stopwords=stopwords, contour_width=3, contour_color='steelblue',font_path=fontpath)

# generate word cloud
#该方法能先进行分词,再进行词频统计,最后生成词云,但是对中文的支持不太好
wc.generate(text)

#根据词频来生成词云,对中文分词,推荐使用该方法
#wc.generate_from_frequencies(dict)

# 从图像创建着色
#image_colors = ImageColorGenerator(alice_mask)

# store to file
wc.to_file(path.join(d, "alice.png"))

# 1、show
plt.imshow(wc, interpolation='bilinear')
# 2、着色词云并显示
#plt.imshow(wc.recolor(color_func=image_colors), interpolation="bilinear")
plt.axis("off")
plt.figure()
plt.imshow(alice_mask, cmap=plt.cm.gray, interpolation='bilinear')
plt.axis("off")
plt.show()

同路径下的文件:

alice_mask.png

alice_mask.png

猜你喜欢

转载自blog.csdn.net/SteveForever/article/details/81783457