wordcloud词云

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from wordcloud import WordCloud,ImageColorGenerator
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image

text= open("/Users/mintaoyu/Desktop/wordcloud/main.txt",encoding='utf-8').read()
font = '/Users/mintaoyu/Downloads/MFDingDing_Noncommercial-Regular.otf'
alice_coloring = np.array(Image.open('/Users/mintaoyu/Downloads/55.png'))
wc = WordCloud(background_color='black',collocations=False, font_path=font, width=1400, height=1400, margin=2,mask=alice_coloring).generate(text.lower())
image_colors = ImageColorGenerator(alice_coloring)
# 字体颜色与背景颜色自适应
plt.imshow(wc.recolor(color_func=image_colors),interpolation="bilinear")
plt.axis("off")
plt.figure()
plt.show()

注意

因为wordcloud默认不支持中文,所以需要指定字体,这里我是网上下的字体然后对他进行指定font = '/Users/mintaoyu/Downloads/MFDingDing_Noncommercial-Regular.otf'

赏个🍗吧
0%