-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwordFig.py
43 lines (36 loc) · 1.22 KB
/
wordFig.py
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
38
39
40
41
42
43
import matplotlib.pyplot as plt
import wordcloud as wc
class wordFig:
def __init__(self, text='', exclude=[]):
self.stopwords = wc.STOPWORDS
self.text = str(text)
self.loaded = False
for word in exclude:
self.stopwords.add(word)
if str(self.text) != '':
self.generate()
def generate(self):
self.fig = wc.WordCloud(background_color='white',
width=1500,
height=960,
margin=10,
stopwords=self.stopwords
).generate(self.text)
self.loaded = True
def addText(self, text):
self.text += text
def addExclude(self, exclude):
for word in exclude:
self.stopwords.add(word)
def showPng(self, title='WordCloud'):
plt.figure().canvas.set_window_title(title)
# plt.title(title)
plt.axis('off')
try:
plt.imshow(self.fig)
except Exception as e:
print(e)
print('图片加载失败!请尝试重新生成后加载。')
plt.show()
def savePng(self, name):
self.fig.to_file(name)