前言: 没接触Python就感觉词云还挺好玩的,所以现在刚学完词云制作就来和大家分享一波,英文词云制作比较简单,所以在这里直接分享中文词云制作方法,完整代码放在最后自取。 一、效果展示
制作步骤 1.先导入我们所需要依赖的相关库
import jieba
import wordcloud
import matplotlib.pyplot as plt
import imageio
2.定义我们词云背景图片(颜色对比度越深越好,最好为纯色)
mask = imageio.imread(r'#图片路径')
3.指定文本位置以及打法方式
with open('文本位置','r',encoding='utf-8')as f:
words = f.read()
4.对文本进行分词
string = words
seg = jieba.cut(string,cut_all=False)
word_list = [word for word in seg if len(word.strip())>1]
str = '.'.join(word_list)
5.对词云内字体大小、颜色、词数进行调整
wcloud = wordcloud.WordCloud(
font_path='./zt.ttf',
background_color='white',
max_words=500,
max_font_size=150,
mask = mask
)
6.生成并展示词云
wcloud = wcloud.generate(str)
plt.imshow(wcloud)
plt.savefig('eng-map.jpg')
plt.axis('off')
plt.show()
三、词云完整代码
import jieba
import wordcloud
import matplotlib.pyplot as plt
import imageio
mask = imageio.imread(r'./china.png')
with open('./zgg.txt','r',encoding='utf-8')as f:
words = f.read()
string = words
seg = jieba.cut(string,cut_all=False)
word_list = [word for word in seg if len(word.strip())>1]
str = '.'.join(word_list)
wcloud = wordcloud.WordCloud(
font_path='./zt.ttf',
background_color='white',
max_words=500,
max_font_size=150,
mask = mask
)
wcloud = wcloud.generate(str)
plt.imshow(wcloud)
plt.savefig('eng-map.jpg')
plt.axis('off')
plt.show()
|