-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathword_cloud_portrait.py
More file actions
37 lines (27 loc) · 878 Bytes
/
word_cloud_portrait.py
File metadata and controls
37 lines (27 loc) · 878 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"""
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
from wordcloud import WordCloud, STOPWORDS
# get data directory (using getcwd() is needed to support running example in generated IPython notebook)
d = path.dirname(path.dirname(__file__))
# Read the whole text.
text = open(path.join(d, 'data/english_words.txt')).read()
alice_mask = np.array(Image.open(path.join(d, "data/alice_mask.png")))
stopwords = set(STOPWORDS)
stopwords.add("said")
wc = WordCloud(background_color="white", max_words=2000, mask=alice_mask,stopwords=stopwords)
# generate word cloud
wc.generate(text)
# store to file
wc.to_file(path.join(d, "data/alice.png"))
# show
plt.imshow(wc, interpolation='bilinear')
plt.axis("off")
plt.figure()
plt.show()